c# - Why is CreatProcessAsUser failing with "Cannot create a file when that file already exists" -
i have class attempting create process within service using credentials provided.
when launching process seeing error cannot create file when file exists. causing this?
i've tried calling types of executables, know it's not process being created throwing this.
public class processasuser { static processasuser() { usertoken = intptr.zero; } private static intptr usertoken { get; set; } public int startprocess(processstartinfo processstartinfo) { usertoken = intptr.zero; loginotheruser(processstartinfo); var startupinfo = new native.startupinfo(); uint creationflags = 0; if (processstartinfo.createnowindow) creationflags |= native.create_no_window; var processinfo = new native.process_information(); bool processstarted = native.createprocessasuser( usertoken, // user token processstartinfo.filename, // application name processstartinfo.arguments, // commandline intptr.zero, // process attributes intptr.zero, // thread attributes true, // inherrit handle creationflags, // creation flags intptr.zero, // environment processstartinfo.workingdirectory+"\\", // current directory ref startupinfo, out processinfo ); if (!processstarted) { throw new win32exception(marshal.getlastwin32error()); return 0; } int processid = (int)processinfo.dwprocessid; native.closehandle(processinfo.hprocess); native.closehandle(processinfo.hthread); processid = processid; return processid; } public int processid; private static void loginotheruser(processstartinfo processstartinfo) { if (usertoken == intptr.zero) { var tempusertoken = intptr.zero; var password = securestringtostring(processstartinfo.password); bool loginresult = native.logonuser(processstartinfo.username, processstartinfo.domain, password, native.logon32_logon_batch, native.logon32_provider_default, ref tempusertoken); if (loginresult) { usertoken = tempusertoken; } else { native.closehandle(tempusertoken); throw new win32exception(marshal.getlastwin32error()); } } } private static string securestringtostring(securestring value) { var stringpointer = marshal.securestringtobstr(value); try { return marshal.ptrtostringbstr(stringpointer); } { marshal.freebstr(stringpointer); } } public static void releaseusertoken() { native.closehandle(usertoken); } } internal class native { internal const int logon32_logon_interactive = 2; internal const int logon32_logon_batch = 4; internal const int logon32_provider_default = 0; public const int duplicate_same_access = 2; public const int create_no_window = 0x08000000; public const int create_unicode_environment = 0x00000400; [structlayout(layoutkind.sequential)] internal class process_information { public intptr hprocess = intptr.zero; public intptr hthread = intptr.zero; public uint dwprocessid = 0; public uint dwthreadid = 0; } [structlayout(layoutkind.sequential)] internal class startupinfo { public int cb; public string lpreserved; public string lpdesktop = string.empty; public string lptitle; public int dwx = 0; public int dwy = 0; public int dwxsize = 0; public int dwysize = 0; public int dwxcountchars = 0; public int dwycountchars = 0; public int dwfillattribute = 0; public int dwflags = 0; public short wshowwindow = 0; public short cbreserved2 = 0; public intptr lpreserved2 = intptr.zero; public intptr hstdinput = intptr.zero; public intptr hstdoutput = intptr.zero; public intptr hstderror = intptr.zero; public startupinfo() { cb = marshal.sizeof(this); } } [structlayout(layoutkind.sequential)] internal class security_attributes { public security_attributes() { nlength = marshal.sizeof(this); } public int nlength; public intptr lpsecuritydescriptor = intptr.zero; public bool binherithandle; } [dllimport("advapi32.dll", entrypoint = "logonuserw", setlasterror = true, charset = charset.unicode, callingconvention = callingconvention.stdcall)] internal static extern bool logonuser(string lpszusername, string lpszdomain, string lpszpassword, int dwlogontype, int dwlogonprovider, ref intptr phtoken); [dllimport("advapi32.dll", entrypoint = "createprocessasusera", setlasterror = true, charset = charset.ansi)] internal static extern bool createprocessasuser( intptr htoken, [marshalas(unmanagedtype.lpstr)] string lpapplicationname, [marshalas(unmanagedtype.lpstr)] string lpcommandline, intptr lpprocessattributes, intptr lpthreadattributes, bool binherithandle, uint dwcreationflags, intptr lpenvironment, [marshalas(unmanagedtype.lpstr)] string lpcurrentdirectory, ref startupinfo lpstartupinfo, out process_information lpprocessinformation); [dllimport("kernel32.dll", entrypoint = "closehandle", setlasterror = true, charset = charset.auto, callingconvention = callingconvention.stdcall)] internal static extern bool closehandle(intptr handle); }
Comments
Post a Comment