3

How can I launch an Universal App as another user using CreateProcessWithLogonW()? I cannot get it to launch using C:\\Windows\\System32\\cmd.exe /c start skype: even though start skype: does launch in the terminal.

#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;
}

2 Answers2

0

This is impossible. No matter what user the UWP app is run under, it will always run under a sandboxed AppContainer user that is different for every user session. You can't run a UWP app as another user using the Win32 api.

Arush Agarampur
  • 1,340
  • 7
  • 20
0

This is possible depending on what you mean by different user. For example, in our product we have a windows service which runs under 'Local System' account and from this we are able to launch a UWP app in the currently logged on user account. For this, we have used CreateProcessAsUser to launch a process in the logged on user account with command to open the protocol supported by the UWP app. Sample code :

        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!
        }