I also run my application through the Windows service. See if my code can help you.
public class WindowsApi { [DllImport("Wtsapi32.dll", EntryPoint = "WTSQueryUserToken", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool WTSQueryUserToken(uint SessionId, ref IntPtr phToken); [DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUserW", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool CreateProcessAsUser([InAttribute()]IntPtr hToken, InAttribute(), MarshalAs(UnmanagedType.LPWStr)]string lpApplicationName, [InAttribute(), MarshalAs(UnmanagedType.LPWStr)] string lpCommandLine, [InAttribute()] IntPtr pProcessAttributes, [InAttribute()] IntPtr lpThreadAttributes, MarshalAs(UnmanagedType.Bool)] bool bInheritHandles, uint dwCreationFlags, [InAttribute()] IntPtr lpEnvironment, [InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)] string pCurrentDirectory, ref STARTUPINFOW lpStartupInfo, ref PROCESS_INFORMATION lpProcessInformation); [StructLayout(LayoutKind.Sequential)] public struct SECURITY_ATTRIBUTES { public uint nLength; public IntPtr lpSecurityDescriptor; [MarshalAs(UnmanagedType.Bool)] public bool bInheritHandle; } [StructLayout(LayoutKind.Sequential)] public struct STARTUPINFOW { public uint cb; [MarshalAs(UnmanagedType.LPWStr)] public string lpReserved; [MarshalAs(UnmanagedType.LPWStr)] public string lpDesktop; [MarshalAs(UnmanagedType.LPWStr)] 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 IntPtr lpReserved2; public IntPtr hStdInput; public IntPtr hStdOutput; public IntPtr hStdError; } [StructLayout(LayoutKind.Sequential)] public struct PROCESS_INFORMATION { public IntPtr hProcess; public IntPtr hThread; public uint dwProcessId; public uint dwThreadId; } }
Put the following code in your method
try { IntPtr UserTokenHandle = IntPtr.Zero; WindowsApi.WTSQueryUserToken(WindowsApi.WTSGetActiveConsoleSessionId(), ref UserTokenHandle); WindowsApi.PROCESS_INFORMATION ProcInfo = new WindowsApi.PROCESS_INFORMATION(); WindowsApi.STARTUPINFOW StartInfo = new WindowsApi.STARTUPINFOW(); StartInfo.cb = Convert.ToUInt32(System.Runtime.InteropServices.Marshal.SizeOf(StartInfo)); string arguments = " nonGUI"; WindowsApi.CreateProcessAsUser(UserTokenHandle, pathToExe + "\\YourAppName.exe", arguments, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref StartInfo, ref ProcInfo); catch (Exception ex) {
This will create the process in the current user account. This code works and works.
I want this to help! greetings!
Parth jani
source share