Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static bool IsLogin()
        {
            var hUserToken = IntPtr.Zero;
            var startInfo  = new ProcessExtensions.STARTUPINFO();
            var procInfo   = new ProcessExtensions.PROCESS_INFORMATION();
            var pEnv       = IntPtr.Zero;

            startInfo.cb = Marshal.SizeOf(typeof(ProcessExtensions.STARTUPINFO));

            try
            {
                int errorCode = 0;
                return(ProcessExtensions.GetSessionUserToken(ref hUserToken, ref errorCode));
            }
            finally
            {
                ProcessExtensions.CloseHandle(hUserToken);
                if (pEnv != IntPtr.Zero)
                {
                    ProcessExtensions.DestroyEnvironmentBlock(pEnv);
                }
                ProcessExtensions.CloseHandle(procInfo.hThread);
                ProcessExtensions.CloseHandle(procInfo.hProcess);
            }
        }
Пример #2
0
        /// <summary>
        /// 以登入者的權限啟動程式
        /// from https://github.com/murrayju/CreateProcessAsUser
        /// </summary>
        /// <param name="appPath"></param>
        /// <param name="cmdLine"></param>
        /// <param name="workDir"></param>
        /// <param name="visible"></param>
        /// <returns></returns>
        public static bool StartProcessAsCurrentUser(string appPath, string cmdLine = null, string workDir = null, bool visible = true)
        {
            var hUserToken = IntPtr.Zero;
            var startInfo  = new ProcessExtensions.STARTUPINFO();
            var procInfo   = new ProcessExtensions.PROCESS_INFORMATION();
            var pEnv       = IntPtr.Zero;
            int iResultOfCreateProcessAsUser;

            startInfo.cb = Marshal.SizeOf(typeof(ProcessExtensions.STARTUPINFO));

            try
            {
                int errorCode = 0;
                if (!ProcessExtensions.GetSessionUserToken(ref hUserToken, ref errorCode))
                {
                    LOG.Error("StartProcessAsCurrentUser: GetSessionUserToken failed. errorCode:[" + errorCode + "]");
                }

                uint dwCreationFlags = ProcessExtensions.CREATE_UNICODE_ENVIRONMENT | (uint)(visible ? ProcessExtensions.CREATE_NEW_CONSOLE : ProcessExtensions.CREATE_NO_WINDOW);
                startInfo.wShowWindow = (short)(visible ? WinApi.SW.SW_SHOW : WinApi.SW.SW_HIDE);
                startInfo.lpDesktop   = "winsta0\\default";

                if (!ProcessExtensions.CreateEnvironmentBlock(ref pEnv, hUserToken, false))
                {
                    throw new Exception("StartProcessAsCurrentUser: CreateEnvironmentBlock failed.");
                }

                if (!ProcessExtensions.CreateProcessAsUser(hUserToken,
                                                           appPath, // Application Name
                                                           cmdLine, // Command Line
                                                           IntPtr.Zero,
                                                           IntPtr.Zero,
                                                           false,
                                                           dwCreationFlags,
                                                           pEnv,
                                                           workDir, // Working directory
                                                           ref startInfo,
                                                           out procInfo))
                {
                    iResultOfCreateProcessAsUser = Marshal.GetLastWin32Error();
                    throw new Exception("StartProcessAsCurrentUser: CreateProcessAsUser failed.  Error Code -" + iResultOfCreateProcessAsUser);
                }

                iResultOfCreateProcessAsUser = Marshal.GetLastWin32Error();
            }
            finally
            {
                ProcessExtensions.CloseHandle(hUserToken);
                if (pEnv != IntPtr.Zero)
                {
                    ProcessExtensions.DestroyEnvironmentBlock(pEnv);
                }
                ProcessExtensions.CloseHandle(procInfo.hThread);
                ProcessExtensions.CloseHandle(procInfo.hProcess);
            }

            return(true);
        }