【问题标题】:How to launch an Universal App as another user with WinAPI?如何使用 WinAPI 作为另一个用户启动通用应用程序?
【发布时间】:2019-04-11 04:42:20
【问题描述】:

如何使用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(): %i\n", 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;
}

【问题讨论】:

  • GetLastError() 返回什么?

标签: c winapi windows-10-desktop


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这取决于不同用户的意思。例如,在我们的产品中,我们有一个在“本地系统”帐户下运行的 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!
            }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      相关资源
      最近更新 更多