예제 #1
0
        /// <summary>
        ///     以当前登录系统的用户角色权限启动指定的进程
        /// </summary>
        /// <param name="processPath">指定的进程(全路径)</param>
        public static void CreateProcess(string processPath)
        {
            var ppSessionInfo = IntPtr.Zero;
            var sessionCount  = 0;
            var hasSession    = WinApi.WTSEnumerateSessions(IntPtr.Zero, 0, 1, ref ppSessionInfo, ref sessionCount) != 0;

            try
            {
                if (!hasSession)
                {
                    throw new Exception("WTSEnumerateSessions==0");
                }
                for (var count = 0; count < sessionCount; count++)
                {
                    var si = (WinApi.WTS_SESSION_INFO)Marshal.PtrToStructure(
                        ppSessionInfo + count * Marshal.SizeOf(typeof(WinApi.WTS_SESSION_INFO)), typeof(WinApi.WTS_SESSION_INFO));

                    if (si.State != WinApi.WTS_CONNECTSTATE_CLASS.WTSActive)
                    {
                        continue;
                    }

                    IntPtr hToken = IntPtr.Zero;

                    if (!WinApi.WTSQueryUserToken(si.SessionID, out hToken))
                    {
                        continue;
                    }

                    var tStartUpInfo = new WinApi.STARTUPINFO
                    {
                        cb = Marshal.SizeOf(typeof(WinApi.STARTUPINFO))
                    };
                    var childProcStarted = WinApi.CreateProcessAsUser(
                        hToken,
                        processPath,
                        null,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        false,
                        0,
                        null,
                        null,
                        ref tStartUpInfo,
                        out var tProcessInfo
                        );
                    if (!childProcStarted)
                    {
                        throw new Exception($"CreateProcessAsUser({processPath})");
                    }
                    WinApi.CloseHandle(tProcessInfo.hThread);
                    WinApi.CloseHandle(tProcessInfo.hProcess);

                    WinApi.CloseHandle(hToken);
                    break;
                }
            }
            finally
            {
                if (ppSessionInfo != IntPtr.Zero)
                {
                    WinApi.WTSFreeMemory(ppSessionInfo);
                }
            }
        }
예제 #2
0
 internal static extern bool CreateProcessAsUser(IntPtr hToken, string lpApplicationName, string lpCommandLine,
                                                 IntPtr lpProcessAttributes, IntPtr lpThreadAttributes,
                                                 bool bInheritHandles, uint dwCreationFlags, string lpEnvironment, string lpCurrentDirectory,
                                                 ref WinApi.STARTUPINFO lpStartupInfo, out WinApi.PROCESS_INFORMATION lpProcessInformation);