CreateProcessWithLogonW And RunAS

  • Thread starter Thread starter Wayne Gibson
  • Start date Start date
W

Wayne Gibson

Hi,
Was wondering if anybody could help..

I'm try to launch an application as an administrator from with inside my
application.
At first this appeared to be working, until I tried launching a different
application..

I am launching the application using the following command..

app = "test.exe";
CreateProcessWithLogonW( "Administrator",".", "password",
LOGON_WITH_PROFILE, app, null, 0, IntPtr.Zero, CurrentDirectory, ref si, out
pi) ;

The problem is that if run the same application via the command prompt with
runas.exe it works!!!!
Can see what I'm missing!!
Anybody got any ideas?
Are there any hidden flags for the LOGON flags..

Thanks

Wayne Gibson
 
Hi,

Is "test.exe" in your executable's path. Here's (part of) the documentation
of the lpApplicationName parameter:

"The string can specify the full path and file name of the module to execute
or it can specify a partial name. In the case of a partial name, the
function uses the current drive and current directory to complete the
specification. The function will not use the search path."

Also, why don't you get the last error, or just throw "$ERR,hr" in the watch
window to see what's wrong?

HTH,
Stoyan Damov
 
Hi Wayne,
I agree with Stoyan, you should give the full path to your application if
it isn't in the current directory of your program. CurrentDirectory is not
for that purpose, This feature is provided primarily for shells that need
to start an application and specify its initial drive and working directory.
I tested the API on my system using the following statement , and it works
fine.
Also it seems you are using This API .NET via PInvoke, I'm not sure if
there is something wrong in the delcarationg of your API and related
structures. Here is my definition, I hope it will be helpful to you.
If you still have problem on this issue, please follow up this issue in
windowsform group, I'll be glad to help you.
Thanks!

<code>
int res;
res = NativeMethod.CreateProcessWithLogonW(
"Administrator",//lpUserName
"Stardusts",//lpDomain
"Password01!",//lpPassword
NativeMethod.LOGON_WITH_PROFILE,//dwLogonFlags
@"C:\Interop_CreateProcessWithLogonW\bin\test.exe",//lpApplicationname
null,//lpCommandline
0,//dwCreationFlags
null,//lpEnvironment
null,//lpCurrentDirectory
ref si,//lpStartupInfo
out pi//lpProcessInfo
);
</code>
<code>
class NativeMethod
{
public const int LOGON_WITH_PROFILE = 0x00000001;
public const int CREATE_DEFAULT_ERROR_MODE = 0x04000000;
/*
* BOOL CreateProcessWithLogonW(
LPCWSTR lpUsername,
LPCWSTR lpDomain,
LPCWSTR lpPassword,
DWORD dwLogonFlags,
LPCWSTR lpApplicationName,
LPWSTR lpCommandLine,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInfo
);
* */
[DllImport("Advapi32.Dll",CharSet = CharSet.Unicode)]
public extern static int CreateProcessWithLogonW(
string lpUsername,
string lpDomain,
string lpPassword,
uint dwLogonFlags,
string lpapplicationName,
string lpCommandLine,
uint dwCreationFlags,
string lpEnvironment,
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInfo
);
/*
*
*typedef struct _PROCESS_INFORMATION {
HANDLE hProcess;
HANDLE hThread;
DWORD dwProcessId;
DWORD dwThreadId;
} PROCESS_INFORMATION;
*/
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}
/*
* typedef struct _STARTUPINFO {
* DWORD cb;
* LPTSTR lpReserved;
* LPTSTR lpDesktop;
* LPTSTR lpTitle;
* DWORD dwX;
* DWORD dwY;
* DWORD dwXSize;
* DWORD dwYSize;
* DWORD dwXCountChars;
* DWORD dwYCountChars;
* DWORD dwFillAttribute;
* DWORD dwFlags;
* WORD wShowWindow;
* WORD cbReserved2;
* LPBYTE lpReserved2;
* HANDLE hStdInput;
* HANDLE hStdOutput;
* HANDLE hStdError;
*} STARTUPINFO, *LPSTARTUPINFO;
* */

[StructLayout(LayoutKind.Sequential,Pack = 2)]
public struct STARTUPINFO
{
public int cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public ushort wShowWindow;
public ushort cbReserved2;
public string lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
}
</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| Reply-To: "Stoyan Damov" <[email protected]_NOSPAM>
| From: "Stoyan Damov" <[email protected]_NOSPAM>
| References: <[email protected]>
| Subject: Re: CreateProcessWithLogonW And RunAS
| Date: Thu, 16 Oct 2003 18:19:57 +0300
| Lines: 48
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups:
microsoft.public.dotnet.framework.windowsforms,microsoft.public.platformsdk.
security,microsoft.public.vc.utilities,microsoft.public.win2000.developer,mi
crosoft.public.win2000.security
| NNTP-Posting-Host: 212.124.71.176
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.platformsdk.security:3092
microsoft.public.vc.utilities:16227 microsoft.public.win2000.developer:2172
microsoft.public.win2000.security:13180
microsoft.public.dotnet.framework.windowsforms:54596
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Hi,
|
| Is "test.exe" in your executable's path. Here's (part of) the
documentation
| of the lpApplicationName parameter:
|
| "The string can specify the full path and file name of the module to
execute
| or it can specify a partial name. In the case of a partial name, the
| function uses the current drive and current directory to complete the
| specification. The function will not use the search path."
|
| Also, why don't you get the last error, or just throw "$ERR,hr" in the
watch
| window to see what's wrong?
|
| HTH,
| Stoyan Damov
|
| | > Hi,
| > Was wondering if anybody could help..
| >
| > I'm try to launch an application as an administrator from with inside my
| > application.
| > At first this appeared to be working, until I tried launching a
different
| > application..
| >
| > I am launching the application using the following command..
| >
| > app = "test.exe";
| > CreateProcessWithLogonW( "Administrator",".", "password",
| > LOGON_WITH_PROFILE, app, null, 0, IntPtr.Zero, CurrentDirectory, ref si,
| out
| > pi) ;
| >
| > The problem is that if run the same application via the command prompt
| with
| > runas.exe it works!!!!
| > Can see what I'm missing!!
| > Anybody got any ideas?
| > Are there any hidden flags for the LOGON flags..
| >
| > Thanks
| >
| > Wayne Gibson
| >
| >
|
|
|
 
Thanks Ying..

I do specify the full path for the application name.

Have the CreateProcessWithLogonW working....
Except one of the applications that I am attempt too executing is not be
executed correctly.. Not sure why, maybe because it is spawning another
application/process not sure..

But the reason I posted a message here was because when you invoke the same
application using runas.exe (Supplied with windows 2000/XP) it runs fine!!
So I was wondering if there were any other hidden flags or something that I
was missing..

Here is the code that I have been using

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Security.Principal;
using System.Security.Permissions;

class RunCommandShellAsAdministrator
{
static void Main(string[] args)
{
StartupInfo si = new StartupInfo();
si.cb = Marshal.SizeOf(typeof(StartupInfo));
si.title = "This command prompt is running as Alice";

ProcessInfo pi = new ProcessInfo();
string CurrentDirectory = "C:\\Disk Images";
string parameters = "gdrive E: \"C:\\Disk
Images\\THPS3.VCD\\THPS3.vcd.d00\" /I";
string app = "C:\\Disk Images\\gdrive.exe";

if(CreateProcessWithLogonW( "Administrator",
".",
"password",

LogonFlags.LOGON_WITH_PROFILE,
app,
parameters,
0,
//CreationFlags
IntPtr.Zero,
// Envirnoment..

CurrentDirectory, // Current Directory
ref si,
out pi) )
{
Console.WriteLine("Process Started!!");
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
Console.WriteLine("CPWL failed with error code: {0}",
Marshal.GetLastWin32Error());
}
}

[Flags]
enum LogonFlags
{
LOGON_WITH_PROFILE = 0x00000001,
LOGON_NETCREDENTIALS_ONLY = 0x00000002
}


[Flags]
enum CreationFlags
{
CREATE_SUSPENDED = 0x00000004,
CREATE_NEW_CONSOLE = 0x00000010,
CREATE_NEW_PROCESS_GROUP = 0x00000200,
CREATE_UNICODE_ENVIRONMENT = 0x00000400,
CREATE_SEPARATE_WOW_VDM = 0x00000800,
CREATE_DEFAULT_ERROR_MODE = 0x04000000,
}


[StructLayout(LayoutKind.Sequential)]
struct ProcessInfo
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}


[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
struct StartupInfo
{
public int cb;
public string reserved1;
public string desktop;
public string title;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public ushort wShowWindow;
public short reserved2;
public int reserved3;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}


[DllImport("advapi32.dll", CharSet=CharSet.Unicode, ExactSpelling=true,
SetLastError=true)]
static extern bool CreateProcessWithLogonW( string principal, string
authority, string password, LogonFlags logonFlags, string appName,

string cmdLine, CreationFlags creationFlags, IntPtr environmentBlock, string
currentDirectory,

ref StartupInfo startupInfo, out ProcessInfo processInfo);

[DllImport("kernel32.dll")]
static extern bool CloseHandle(IntPtr h);

}

Thanks

Wayne Gibson
 
Hi Wayne,
Thanks for your reply,
You mean your program could run most applications successfully except for
one application?
Could you tell me more about your program and that app?
Is your program a win32 Gui app or console app?
and how about that app?
Is there any error message when attempting to run that app?
you may use GetLastError API to get the error code if
CreateProcessWithLogonW fails.
If you have any question or update on this issue, please let me know!
Thanks!


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending, Thanks!

--------------------
| From: "Wayne Gibson" <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Subject: Re: CreateProcessWithLogonW And RunAS
| Date: Fri, 17 Oct 2003 09:50:24 +0100
| Lines: 137
| Message-ID: <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
| NNTP-Posting-Host: 62.49.252.243
| X-Trace: news.demon.co.uk 1066380641 19679 62.49.252.243 (17 Oct 2003
08:50:41 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Fri, 17 Oct 2003 08:50:41 +0000 (UTC)
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| X-Priority: 3
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MSMail-Priority: Normal
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!kibo.news.demon.net!news.demon.co.uk!demon!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:54653
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Thanks Ying..
|
| I do specify the full path for the application name.
|
| Have the CreateProcessWithLogonW working....
| Except one of the applications that I am attempt too executing is not be
| executed correctly.. Not sure why, maybe because it is spawning another
| application/process not sure..
|
| But the reason I posted a message here was because when you invoke the
same
| application using runas.exe (Supplied with windows 2000/XP) it runs fine!!
| So I was wondering if there were any other hidden flags or something that
I
| was missing..
|
| Here is the code that I have been using
|
| using System;
| using System.Runtime.InteropServices;
| using System.IO;
| using System.Security.Principal;
| using System.Security.Permissions;
|
| class RunCommandShellAsAdministrator
| {
| static void Main(string[] args)
| {
| StartupInfo si = new StartupInfo();
| si.cb = Marshal.SizeOf(typeof(StartupInfo));
| si.title = "This command prompt is running as Alice";
|
| ProcessInfo pi = new ProcessInfo();
| string CurrentDirectory = "C:\\Disk Images";
| string parameters = "gdrive E: \"C:\\Disk
| Images\\THPS3.VCD\\THPS3.vcd.d00\" /I";
| string app = "C:\\Disk Images\\gdrive.exe";
|
| if(CreateProcessWithLogonW( "Administrator",
| ".",
|
"password",
|
| LogonFlags.LOGON_WITH_PROFILE,
| app,
|
parameters,
| 0,
| //CreationFlags
|
IntPtr.Zero,
| // Envirnoment..
|
| CurrentDirectory, // Current Directory
| ref si,
| out pi) )
| {
| Console.WriteLine("Process Started!!");
| CloseHandle(pi.hProcess);
| CloseHandle(pi.hThread);
| }
| else
| {
| Console.WriteLine("CPWL failed with error code: {0}",
| Marshal.GetLastWin32Error());
| }
| }
|
| [Flags]
| enum LogonFlags
| {
| LOGON_WITH_PROFILE = 0x00000001,
| LOGON_NETCREDENTIALS_ONLY = 0x00000002
| }
|
|
| [Flags]
| enum CreationFlags
| {
| CREATE_SUSPENDED = 0x00000004,
| CREATE_NEW_CONSOLE = 0x00000010,
| CREATE_NEW_PROCESS_GROUP = 0x00000200,
| CREATE_UNICODE_ENVIRONMENT = 0x00000400,
| CREATE_SEPARATE_WOW_VDM = 0x00000800,
| CREATE_DEFAULT_ERROR_MODE = 0x04000000,
| }
|
|
| [StructLayout(LayoutKind.Sequential)]
| struct ProcessInfo
| {
| public IntPtr hProcess;
| public IntPtr hThread;
| public uint dwProcessId;
| public uint dwThreadId;
| }
|
|
| [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
| struct StartupInfo
| {
| public int cb;
| public string reserved1;
| public string desktop;
| public string title;
| public uint dwX;
| public uint dwY;
| public uint dwXSize;
| public uint dwYSize;
| public uint dwXCountChars;
| public uint dwYCountChars;
| public uint dwFillAttribute;
| public uint dwFlags;
| public ushort wShowWindow;
| public short reserved2;
| public int reserved3;
| public IntPtr hStdInput;
| public IntPtr hStdOutput;
| public IntPtr hStdError;
| }
|
|
| [DllImport("advapi32.dll", CharSet=CharSet.Unicode,
ExactSpelling=true,
| SetLastError=true)]
| static extern bool CreateProcessWithLogonW( string principal,
string
| authority, string password, LogonFlags logonFlags, string appName,
|
| string cmdLine, CreationFlags creationFlags, IntPtr environmentBlock,
string
| currentDirectory,
|
| ref StartupInfo startupInfo, out ProcessInfo processInfo);
|
| [DllImport("kernel32.dll")]
| static extern bool CloseHandle(IntPtr h);
|
| }
|
| Thanks
|
| Wayne Gibson
|
|
|
 
Back
Top