murrayju / CreateProcessAsUser

Creates a process in a different Windows session
MIT License
369 stars 114 forks source link

While trying to start an application, getting ex that says: "StartProcessAsCurrentUser: GetSessionUserToken failed.". #17

Closed uguryanik closed 7 years ago

uguryanik commented 7 years ago

if (WTSQueryUserToken(activeSessionId, ref hImpersonationToken) != 0) { // Convert the impersonation token to a primary token bResult = DuplicateTokenEx(hImpersonationToken, 0, IntPtr.Zero, (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, (int)TOKEN_TYPE.TokenPrimary, ref phUserToken);

        CloseHandle(hImpersonationToken);

}

WTSQueryUserToken return 0 and it fails to start process.

AndrewSav commented 7 years ago

@uurynk https://github.com/murrayju/CreateProcessAsUser/issues/16 https://github.com/murrayju/CreateProcessAsUser/issues/4

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

now i'm getting token but CreateProcessAsUser return false with error code -740, in console app it working fine, but in windows service getting error. Whats wrong?

AndrewSav commented 7 years ago

@uurynk Hard to tell without knowing what you are doing. The demo service in the repo certainly works. What are you doing differently? The code you posted above is verbatim from the github project and it is known to work without issues. I also encourage you to re-read the thread I linked above and make sure your process is holding correct privileges - the elevation error you are citing suggesting that it is not.

uguryanik commented 7 years ago

EnableLUA resolved my problem. Thank you for your support.