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
| >
| >
|
|
|