<p>I need to <code>ShellExecute</code> something as another user, currently I start a helper process with <code>CreateProcessAsUser</code> that calls <code>ShellExecute</code>, but that seems like too much of a hack (Wrong parent process etc.) Is there a better way to do this?</p>

<p>@PabloG: ImpersonateLoggedOnUser does not work:</p>

<pre>
HANDLE hTok;
VERIFY(LogonUser("otheruser",0,"password",LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,&hTok));
VERIFY(ImpersonateLoggedOnUser(hTok));
ShellExecute(0,0,"calc.exe",0,0,SW_SHOW);
RevertToSelf();
CloseHandle(hTok);
</pre>

<p>will just start calc as the logged in user, not "otheruser"</p>

<p>@1800 INFORMATION: <code>CreateProcess</code>/<code>CreateProcessAsUser</code> is not the same as <code>ShellExecute</code>, with UAC on Vista, <code>CreateProcess</code> is useless when you don't have control over what program the user is executing (<code>CreateProcess</code> will return with a error if you give it a exe file with a manifest marked as requireAdmin)</p>

<p>@Brian R. Bondy: I already know this info (And don't get me wrong, its good stuff), but it is off topic (IMHO) I am asking for a <code>ShellExecuteAsUser</code>, not about starting processes as another user, I already know how to do that.</p>

[{'answer_id': 33661, 'author': 'PabloG', 'author_id': 394, 'author_profile': 'https://Stackoverflow.com/users/394', 'pm_score': 0, 'selected': False, 'text': '<p>You can wrap the ShellExecute between ImpersonateLoggedOnUser / RevertToSelf</p>\n\n<p>links: \nImpersonateLoggedOnUser: <a href="http://msdn.microsoft.com/en-us/library/aa378612(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa378612(VS.85).aspx</a>\nRevertToSelf: <a href="http://msdn.microsoft.com/en-us/library/aa379317.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa379317.aspx</a></p>\n\n<p>sorry, cannot hyperlink URLs with "()"</p>\n'}, {'answer_id': 33824, 'author': '1800 INFORMATION', 'author_id': 3146, 'author_profile': 'https://Stackoverflow.com/users/3146', 'pm_score': 0, 'selected': False, 'text': "<p>Why don't you just do CreateProcessAsUser specifying the process you want to run?</p>\n\n<p>You may also be able to use SHCreateProcessAsUserW.</p>\n"}, {'answer_id': 35688, 'author': 'Brian R. Bondy', 'author_id': 3153, 'author_profile': 'https://Stackoverflow.com/users/3153', 'pm_score': 3, 'selected': False, 'text': '<p>The solution really depends on what your needs are, and can be pretty complex (Thanks fully to Windows Vista).  This is probably going to be beyond your need, but this will help others that find this page via search.</p>\n\n<ol>\n<li>If you do not need the process to run with a GUI and you do not require elevation</li>\n<li>If the user you want to run as is already logged into a session</li>\n<li>If you need to run the process with a GUI, and the user may, or may not be logged in</li>\n<li>If you need to run the process with elevation</li>\n</ol>\n\n<p><strong>Regarding 1:</strong>\nIn windows Vista there exists something called session 0 isolation.  All services run as session 0 and you are not supposed to have a GUI in session 0.  The first logged on user is logged into session 1.  In previous versions of windows (pre Vista), the first logged on user was also ran fully in session 0.  </p>\n\n<p>You can run several different processes with different usernames in the same session.  You can find a good document about session 0 isolation <a href="http://www.microsoft.com/whdc/system/vista/services.mspx" rel="noreferrer">here</a>.</p>\n\n<p>Since we\'re dealing with option 1), you don\'t need a GUI.  Therefore you can start your process in session 0. </p>\n\n<p>You\'ll want a call sequence something like this:\nLogonUser, ExpandEnvironmentStringsForUser, GetLogonSID, LoadUserProfile, CreateEnvironmentBlock, CreateProcessAsUser. </p>\n\n<p>Example code for this can be found via any search engine, or via <a href="http://www.google.com/codesearch" rel="noreferrer">Google code search</a></p>\n\n<p><strong>Regarding 2:</strong>  If the user you\'d like to run the process as is already logged in, you can simply use: WTSEnumerateSessions, and WTSQuerySessionInformation to get the session ID, and then WTSQueryUserToken to get the user token.  From there you can just use the user token in the CreateProcessAsUser Win32 API.</p>\n\n<p>This is a great method because you don\'t even need to login as the user nor know the user\'s username/password.  I believe this is only possible via a service though running as local system account. </p>\n\n<p>You can get the current session via WTSGetActiveConsoleSessionId.</p>\n\n<p><strong>Regarding 3:</strong>\nYou would follow the same steps as #1, but in addition you would use the STARTUPINFO\'s lpDesktop field.  Set this to winsta0\\Default.  You will also need to try to use the OpenDesktop Win32 API and if this fails you can CreateDesktop.  Before using the station and desktop handles you should use SetSecurityInfo on each of them with SE_WINDOW_OBJECT, and GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION.</p>\n\n<p>If the user in question later tries to login, he will actually see the running process. </p>\n\n<p><strong>Regarding 4:</strong>\nThis can be done as well, but it requires you to already be running an elevated process.  A service running as local system account does run as elevated.  I could also only get it to work by having an authenticode signed process that I wanted to start.   The process you want to start also must have a manifest file associated with it with the requestedExecutionLevel level="requireAdministrator"</p>\n\n<p>Other notes:</p>\n\n<ul>\n<li>You can set a token\'s session via SetTokenInformation and TokenSessionId</li>\n<li>You cannot change the session ID of an already running process. </li>\n<li>This whole process would be drastically more simple if Vista was not in the equation. </li>\n</ul>\n'}, {'answer_id': 1012800, 'author': 'devdimi', 'author_id': 54983, 'author_profile': 'https://Stackoverflow.com/users/54983', 'pm_score': 2, 'selected': False, 'text': '<p>If you need ShellExecute semantics you can feed following: </p>\n\n<p><code>C:\\windwos\\system32\\cmd.exe /k" start &lt;your_target_to_be_ShellExecuted&gt;"</code>\n to <code>CreateProcessAsUser</code> and you are done.</p>\n'}]
['https://Stackoverflow.com/questions/33594', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3501/']