The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[Desktop Converter] How can I launch a converted desktop app as administrator via code? RRS feed

  • Question

  • Hi,

    I use Desktop Converter convert a desktop exe to a .appx and Install it.

    I can run my converted desktop app as administrator via:

    start menu right-click my app -> More -> Rus as administrator

    Is there a way to do the same thing (launch my app or launch other app) from code (launch from a non-converted desktop app or a converted desktop app)?

    I try to launch by IApplicationActivationManager but not work (test on Windows 1803/1809).


    reference to:

    https://docs.microsoft.com/en-us/windows/uwp/xbox-apps/automate-launching-uwp-apps

    https://docs.microsoft.com/en-us/windows/desktop/com/the-com-elevation-moniker

    here is main code:

    #include "pch.h"
    
    #include <ShObjIdl.h>
    #include <atlbase.h>
    #include <strsafe.h>
    #include <sstream>
    
    using namespace winrt;
    using namespace Windows::Foundation;
    
    HRESULT LaunchApp(LPCWSTR AUMID, bool admin);
    
    int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
    {
        init_apartment(apartment_type::multi_threaded);
    
        LPWSTR *szArgList;
        int argCount;
        szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
    
        bool admin = false;
        std::wstring aumid;
    
        for (int i = 1; i < argCount; i++)
        {
            if (lstrcmpW(szArgList[i], L"/runas") == 0)
            {
                admin = true;
            }
            else if (lstrcmpW(szArgList[i], L"/aumid") == 0 && i + 1 < argCount)
            {
                aumid = szArgList[i + 1];
                ++i;
            }
        }
    
        HRESULT hr = LaunchApp(aumid.c_str(), admin);
    
        if (FAILED(hr))
        {
            std::wstringstream stream;
            stream << std::hex << hr;
            std::wstring msg = L"LaunchApp fail hr = 0x" + stream.str();
            ::MessageBoxW(::GetDesktopWindow(), msg.c_str(), L"Launch desktop app", MB_OK);
        }
        else
        {
            ::MessageBoxW(::GetDesktopWindow(), L"LaunchApp Success", L"Launch desktop app", MB_OK);
        }
    }
    
    
    // https://docs.microsoft.com/en-us/windows/desktop/com/the-com-elevation-moniker
    HRESULT CoCreateInstanceAsAdmin(HWND hwnd, REFCLSID rclsid, REFIID riid, __out void ** ppv)
    {
        BIND_OPTS3 bo;
        WCHAR  wszCLSID[50];
        WCHAR  wszMonikerName[300];
    
        StringFromGUID2(rclsid, wszCLSID, sizeof(wszCLSID) / sizeof(wszCLSID[0]));
        HRESULT hr = StringCchPrintf(wszMonikerName, sizeof(wszMonikerName) / sizeof(wszMonikerName[0]), L"Elevation:Administrator!new:%s", wszCLSID);
        if (FAILED(hr))
            return hr;
        memset(&bo, 0, sizeof(bo));
        bo.cbStruct = sizeof(bo);
        bo.hwnd = hwnd;
        bo.dwClassContext = CLSCTX_LOCAL_SERVER;
        return CoGetObject(wszMonikerName, &bo, riid, ppv);
    }
    
    // https://docs.microsoft.com/en-us/windows/uwp/xbox-apps/automate-launching-uwp-apps
    HRESULT LaunchApp(LPCWSTR AUMID, bool admin)
    {
        HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
        if (FAILED(hr))
        {
            wprintf(L"LaunchApp %s: Failed to init COM. hr = 0x%08lx \n", AUMID, hr);
        }
        {
            CComPtr<IApplicationActivationManager> AppActivationMgr = nullptr;
            if (SUCCEEDED(hr))
            {
                if (admin)
                {
                    hr = CoCreateInstanceAsAdmin(
                        NULL,
                        CLSID_ApplicationActivationManager,
                        IID_PPV_ARGS(&AppActivationMgr));
                }
                else
                {
                    hr = CoCreateInstance(CLSID_ApplicationActivationManager, nullptr,
                        CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&AppActivationMgr));
                }
                if (FAILED(hr))
                {
                    wprintf(L"LaunchApp %s: Failed to create Application Activation Manager.hr = 0x%08lx \n", AUMID, hr);
                }
            }
            if (SUCCEEDED(hr))
            {
                DWORD pid = 0;
                hr = AppActivationMgr->ActivateApplication(AUMID, nullptr, AO_NONE,
                    &pid);
                if (FAILED(hr))
                {
                    wprintf(L"LaunchApp %s: Failed to Activate App. hr = 0x%08lx \n", AUMID, hr);
                }
            }
        }
        CoUninitialize();
        return hr;
    }

    the complete code LaunchDesktopApp.zip:

    https://drive.google.com/open?id=1jOrzSdxg1H30sBtRPaJ-sy3DLKIB7QXa

    build the code and run in cmd

    1. launch without request admin is success

    LaunchDesktopApp.exe /aumid "Microsoft.DesktopAppConverter_8wekyb3d8bbwe!DesktopAppConverter"

    2. launch with request admin will got error: 0x80080017

    LaunchDesktopApp.exe /runas /aumid "Microsoft.DesktopAppConverter_8wekyb3d8bbwe!DesktopAppConverter"

    Is there something wrong in my code? Or I should use another API? Or it is a limitation?

    Thanks.


    • Edited by Emmett Tsai Monday, June 24, 2019 10:43 AM add launch with request admin cmd line
    Monday, June 24, 2019 9:42 AM

Answers

  • Hi,

    Well, it is possible to request elevation for your desktop bridge app. But you need to add a restricted capability to your converted app. I would suggest you to use Windows Application Packaging Project to convert you desktop app. Before the conversion, add an Application Manifest File to your desktop app and change the requested execution level from “asInvoker” to “highestAvailable”. Then add allowElevation capability to the manifest file of the Windows Application Packaging Project. Then the converted app could run as elevated process. There is a tutorial about this on the Internet. Please open your search engine and search for "App Elevation Samples – Part 1 stefanwick". That is a more detailed blog about this.

    But please note, the allowElevation capability is a restricted capability. After you declared this capability in applications and you want to submit your app to the Microsoft Store. In most cases, the use of this capability won't be approved. It will only be approved for line-of-business apps deployed by enterprises to their private store via the Microsoft Store for Business.

    For more information, please refer this link:Restricted capabilities

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Emmett Tsai Tuesday, June 25, 2019 6:10 PM
    Tuesday, June 25, 2019 2:38 AM
    Moderator

All replies

  • Hi,

    Well, it is possible to request elevation for your desktop bridge app. But you need to add a restricted capability to your converted app. I would suggest you to use Windows Application Packaging Project to convert you desktop app. Before the conversion, add an Application Manifest File to your desktop app and change the requested execution level from “asInvoker” to “highestAvailable”. Then add allowElevation capability to the manifest file of the Windows Application Packaging Project. Then the converted app could run as elevated process. There is a tutorial about this on the Internet. Please open your search engine and search for "App Elevation Samples – Part 1 stefanwick". That is a more detailed blog about this.

    But please note, the allowElevation capability is a restricted capability. After you declared this capability in applications and you want to submit your app to the Microsoft Store. In most cases, the use of this capability won't be approved. It will only be approved for line-of-business apps deployed by enterprises to their private store via the Microsoft Store for Business.

    For more information, please refer this link:Restricted capabilities

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Emmett Tsai Tuesday, June 25, 2019 6:10 PM
    Tuesday, June 25, 2019 2:38 AM
    Moderator
  • Hi Roy,

    Thank you. The Tutorial Part 2 is what I want, it work very well!


          <Extensions>
            <uap3:Extension Category="windows.appExecutionAlias"
                            Executable="MyProject\MyProject.exe"
                            EntryPoint="Windows.FullTrustApplication">
              <uap3:AppExecutionAlias>
                <desktop:ExecutionAlias Alias="MyProjectSelfElevation.exe" />
              </uap3:AppExecutionAlias>
            </uap3:Extension>
          </Extensions>

    I don't known why if I set Executable="MyProject.exe" will launch app fail. But adding one more directory will succeed.

    Well, in short I can run the converted desktop app as admin on windows 1809.

    ShellExecute(NULL, L"runas", L"%userprofile%\AppData\Local\microsoft\windowsapps\MyProjectSelfElevation.exe", L"", NULL, SW_SHOWDEFAULT);


    Thanks again.

    Tuesday, June 25, 2019 6:10 PM