Execute an exe under particular user account?
-
Hi all, I want to execute an executable file under particular user account in my C# application. can any body tell me how to do the same? Thanking You, Sunil G.
-
Hi all, I want to execute an executable file under particular user account in my C# application. can any body tell me how to do the same? Thanking You, Sunil G.
using
Process.Start
[^], it supports launching a process under a particular user;MSDN states:
Use this overload to create a new process and its primary thread by specifying its file name, command-line arguments, user name, password, and domain. The new process then runs the specified executable file in the security context of the specified credentials (user, domain, and password).
I are Troll :suss:
-
using
Process.Start
[^], it supports launching a process under a particular user;MSDN states:
Use this overload to create a new process and its primary thread by specifying its file name, command-line arguments, user name, password, and domain. The new process then runs the specified executable file in the security context of the specified credentials (user, domain, and password).
I are Troll :suss:
-
Hi all, I want to execute an executable file under particular user account in my C# application. can any body tell me how to do the same? Thanking You, Sunil G.
-
questions should be answered within the thread, and not by e-mail. That way anyone can benefit. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
questions should be answered within the thread, and not by e-mail. That way anyone can benefit. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Yes there you are right but I send him more than 300 lines of code. I think this is tooo long for this forum. But here a short version (with out DLLImports, structs, ...):
public static void StartProcessOnActiveConsole(string szApplication, string szArguments, string szDirectory) { IntPtr hUserToken = IntPtr.Zero; IntPtr hDuplicatedUserToken = IntPtr.Zero; IntPtr lpEnvironment = IntPtr.Zero; IntPtr hProcess = IntPtr.Zero; IntPtr hThread = IntPtr.Zero; try { uint dwSessionId = 0xFFFFFFFF; dwSessionId = WTSGetActiveConsoleSessionId(); if (dwSessionId == 0xFFFFFFFF) // no active session return; if(!WTSQueryUserToken(dwSessionId, ref hUserToken)) throw new Exception("WTSQueryUserToken failed. GetLastError() = " + Marshal.GetLastWin32Error()); if(!DuplicateTokenEx(hUserToken, 0x02000000 /\*MAXIMUM\_ALLOWED\*/, IntPtr.Zero, SECURITY\_IMPERSONATION\_LEVEL.SecurityImpersonation, TOKEN\_TYPE.TokenPrimary, ref hDuplicatedUserToken)) throw new Exception("DuplicateTokenEx failed. GetLastError() = " + Marshal.GetLastWin32Error()); if(!CreateEnvironmentBlock(ref lpEnvironment, hDuplicatedUserToken, false)) throw new Exception("CreateEnvironmentBlock failed. GetLastError() = " + Marshal.GetLastWin32Error()); STARTUPINFO startupInfo = new STARTUPINFO(); startupInfo.dwSize = (uint)Marshal.SizeOf(startupInfo); startupInfo.pReserved = IntPtr.Zero; startupInfo.szDesktop = "winsta0\\\\default"; startupInfo.pTitle = IntPtr.Zero; startupInfo.dwX = 0; startupInfo.dwY = 0; startupInfo.dwXSize = 0; startupInfo.dwYSize = 0; startupInfo.dwXCountChars = 0; startupInfo.dwYCountChars = 0; startupInfo.dwFillAttribute = 0; startupInfo.dwFlags = 128 /\*STARTF\_FORCEOFFFEEDBACK\*/; startupInfo.wShowWindow = 0; startupInfo.wReserved2 = 0; startupInfo.lpReserved2 = IntPtr.Zero; startupInfo.hStdInput = IntPtr.Zero; startupInfo.hStdOutput = IntPtr.Zero; startupInfo.hStdError = IntPtr.Zero; PROCESS\_INFORMATION processIn