c-如何使用WinAPI以另一用户身份启动通用应用程序



如何使用CreateProcessWithLogonW()以另一用户身份启动通用应用程序?即使start skype:确实在终端中启动,我也无法使用C:\Windows\System32\cmd.exe /c start skype:启动它。

#include <stdio.h>
#include <windows.h>
#include <lmcons.h>
int main(void)
{
PROCESS_INFORMATION pi = { 0 };
STARTUPINFOW        si = { 0 };
si.cb = sizeof(STARTUPINFOW);
/*
not working:
L"C:\Windows\System32\cmd.exe /c start skype"        error: "The filename, directory name, or volume label syntax is incorrect."
L"C:\Windows\System32\cmd.exe /c start skype:"       no error but a pop-up with text: "You'll need a new app to open this"
*/
wchar_t lpCommandLine[] = L"C:\Windows\System32\cmd.exe /c start skype:"; // lpCommandLine must be writable memory
if (!CreateProcessWithLogonW(L"username", L".", L"password", LOGON_WITH_PROFILE, NULL, lpCommandLine, 0, NULL, NULL, &si, &pi))
{
printf("GetLastError(): %in", GetLastError());
char buf[UNLEN + 1] = { 0 };
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, sizeof(buf), NULL);
puts(buf);
return 1;
}
else
{
// do stuff only while skype is running
puts("skype is running.");
if (WaitForSingleObject(pi.hProcess, INFINITE) == WAIT_FAILED)
puts("WaitForSingleObject() failed");
// do stuff only after skype exits
puts("skype is NOT running.");
}
return 0;
}

这是不可能的。无论UWP应用程序在哪个用户下运行,它都将始终在每个用户会话都不同的沙盒AppContainer用户下运行。您不能以使用Win32 api的其他用户身份运行UWP应用程序。

这可能取决于不同用户的意思。例如,在我们的产品中,我们有一个在"本地系统"帐户下运行的windows服务,通过该服务,我们可以在当前登录的用户帐户中启动UWP应用程序。为此,我们使用CreateProcessAsUser在登录的用户帐户中启动一个进程,命令打开UWP应用程序支持的协议。样本代码:

string uwpAppLaunchCmdLine = string.Format("/c start {0}", PROTOCOL_SUPPORTED_BY_UWP_APP);
int processId;
IntPtr hErrorReadOut = IntPtr.Zero;
ProcessMetadata processMetadata;
if (!StartAsCurrentUser("cmd.exe", false, out processId,out processMetadata, coreAppLaunchCmdLine))
{
//Failed to launch
}
else
{
//success!
}   

最新更新