Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LogonUser and CreateProcessAsUser

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
0
0
SE
Does anybody have a working example of this? I can't use CreateProcessWithLogonW because it is not allowed for the SYSTEM account in XP SP2 and Windows 2003 SP1.

thanks

--- neteject.com - Internet Solutions ---
 
I don't have any trouble with XP SP2. Be certain that you aren't executing the program file from a network resource. There are different security rules for that than if you are executing from your hard drive.

This works for me:
Code:
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool CreateProcessWithLogonW(
		string strUser, string strDomain, string strPassword, int intLogon, string strApplication, 
		string strCommand, int intCreation, int intEnvironment, string strDirectory, 
		int intStartup, PROCESSINFORMATION infProcess);

[StructLayout(LayoutKind.Sequential)]
private class PROCESSINFORMATION {
	public IntPtr Process;
	public IntPtr Thread;
	public int ProcessId;
	public int ThreadId;
}

//Excerpt from a private void in which I run a Windows
//Installer update as the domain admin

//If the computer is part of the MyDomain domain, the
//update must be run with administrative privileges
PROCESSINFORMATION prc = new PROCESSINFORMATION();

//I have found you must provide the full filename and path
//to the executable
if (Maintenance.CreateProcessWithLogonW(
   "Administrator", "MYDOMAIN", "mypassword", 0, 
   @"C:\Windows\System32\msiexec.exe", strArgument, 
   0, 0, "", 0, prc)) {
				
   Maintenance.CloseHandle(prc.Process);
   Maintenance.CloseHandle(prc.Thread);
}
 
I also see that my command line arguments start off with a space:

string strArgument = " /p";

NOT

string strArgument = "/p";

You might want to check that too.
 
Yeah, I'm running it as a WinForm, not a service.

You might not even need to bother with CreateProcessWithLogonW as the SYSTEM account. I'd try running netsh without using CreateProcessWithLogonW and see if you have any trouble. I'd be curious to see if netsh shows up as running under the SYSTEM account under the task manager.

Also, you've probably tried this already but I'd make sure you can still do what you're trying to do from the command prompt logged in as a privileged user under XP SP2. You might get an error message that sheds more light on the security "enhancement".
 
ok, but I want to reach the profiles network files. thanks anyway!

--- neteject.com - Internet Solutions ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top