How to determine if UWP app is running with windows open?

116 Views Asked by At

I tried using IPackageDebugSettings.GetPackageExecutionState, but it returns PES_RUNNING despite no app windows being open, and in the Task Manager "Status" column the exe showing "Suspended".

My final goal is to launch the app in suspended state, but only if it was not already running (scenario requires me to raise an error in that case). If it was running already in the background, I need to gracefully shut it down. The app in question is a third-party app.

1

There are 1 best solutions below

0
Junjie Zhu - MSFT On

I had the same problem during testing, but when I used GetPackageExecutionState directly I was able to read the correct state of the UWP application.

You can launch the UWP application from Start Menu and do not use EnableDebugging method. You can refer to following code.

#include <windows.h>
#include <shobjidl_core.h>
#include <atlbase.h>
#include <iostream>

int main()
{
    std::wstring appFullName = L"app package full name";
    
    HRESULT hResult = S_OK;

    // Create a new instance of IPackageDebugSettings
    CoInitialize(NULL);
    ATL::CComQIPtr<IPackageDebugSettings> debugSettings;
    hResult = debugSettings.CoCreateInstance(CLSID_PackageDebugSettings, NULL, CLSCTX_ALL);
    if (hResult != S_OK) return hResult;

    // Enable debugging and use a custom debugger
    //hResult = debugSettings->EnableDebugging(appFullName.c_str(), NULL, NULL);
    //hResult = debugSettings->DisableDebugging(appFullName.c_str());
    //if (hResult != S_OK) return hResult;

    PACKAGE_EXECUTION_STATE state;
    hResult = debugSettings->GetPackageExecutionState(appFullName.c_str(), &state);

    std::cout << state;
    
}