Exemple #1
0
        private static Process CreateProcessAsUser(IntPtr token, string appName, string appArgs)
        {
            Win32.SecurityAttributes sa = new Win32.SecurityAttributes();
            sa.nLength = (UInt32)Marshal.SizeOf(sa);

            Win32.Startupinfo si = new Win32.Startupinfo();
            si.cb = (UInt32)Marshal.SizeOf(si);

            return(CreateProcessAsUser(token, appName, appArgs, sa, sa, false, 0, IntPtr.Zero, null, si));
        }
Exemple #2
0
        public bool Initialise()
        {
            IntPtr userToken = GetUserToken(UserName, Environment.MachineName, Password);

            if (userToken == IntPtr.Zero)
            {
                return(false);
            }

            IntPtr userRegToken = LoadUserProfile(userToken, UserName);

            if (userRegToken == IntPtr.Zero)
            {
                return(false);
            }

            if (Win32.CreateEnvironmentBlock(out IntPtr envBlock, userToken, 0) == 0)
            {
                return(false);
            }

            Win32.SecurityAttributes sa = new Win32.SecurityAttributes();
            sa.nLength = (UInt32)Marshal.SizeOf(sa);

            Win32.Startupinfo si = new Win32.Startupinfo();
            si.cb = (UInt32)Marshal.SizeOf(si);

            Process process = CreateProcessAsUser(userToken, null, ConfigHelper.CommandName, sa, sa, false, 0, IntPtr.Zero, null, si);

            if (process == null)
            {
                return(false);
            }

            if (Win32.UnloadUserProfile(userToken, userRegToken) == 0)
            {
                return(false);
            }
            if (Win32.CloseHandle(envBlock) == 0)
            {
                return(false);
            }
            if (!ReleaseToken(userToken))
            {
                return(false);
            }

            process.WaitForExit();

            return(true);
        }
Exemple #3
0
        private static Process CreateProcessAsUser(IntPtr token, string appName, string appArgs,
                                                   Win32.SecurityAttributes processAttributes, Win32.SecurityAttributes threadAttributes,
                                                   bool inheritHandles, UInt32 creationFlags, IntPtr environment, string currentDir,
                                                   Win32.Startupinfo startupInfo)
        {
            Win32.ProcessInformation pi = new Win32.ProcessInformation();

            if (Win32.CreateProcessAsUser(token, appName, appArgs, ref processAttributes, ref threadAttributes,
                                          inheritHandles ? 1 : 0, creationFlags, environment, currentDir, ref startupInfo, out pi) == 0)
            {
                Program.Log.Error($"CreateProcessAsUser failed with error code: {Marshal.GetLastWin32Error()}");
                return(null);
            }

            try
            {
                return(Process.GetProcessById((Int32)pi.dwProcessId));
            }
            catch (ArgumentException ex)
            {
                Program.Log.Error($"Process with id {pi.dwProcessId} does not exist. {ex.Message}");
                return(null);
            }
        }