Suspend Windows 10 Store App on launch

2019-03-05 10:09发布

问题:

I am writing a tool in C++ to help debug and test deployed Windows 10 Store Apps. One of the problems I've run into is that I need a way to launch a Store App in a suspended state so I can attach a debugger before the app initializes. The usual way I know of to do something like this would be to set a hook on process creation in the parent process and forcibly flag the created process to start in a suspended state; however, for Store Apps, the parent process that creates the actual app process is svchost.exe which runs at a system level and can't be hooked without a kernel-mode driver of some sort.

I am using the IApplicationActivationManager interface to launch applications, and am using the IPackageDebugSettings interface to control/debug them after launch. Is there a way I can suspend a Windows 10 Store App when it launches so that it isn't allowed to first initialize?

回答1:

Looking back, I nearly answered my own question within the question itself. To launch a Windows 10 Store app in a suspended state, use the IPackageDebugSettings::EnableDebugging method to set an executable as a debugger. The app being debugged will launch suspended automatically; it's then the debugger's responsibility to resume the app.

What I ended up doing in my project is something akin to this:

// Gets the current application's UserModelId and PackageId from the registry
std::wstring appName = AppUtils::GetApplicationUserModelId();
std::wstring appFullName = AppUtils::GetApplicationPackageId();

HRESULT hResult = S_OK;

// Create a new instance of IPackageDebugSettings
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(), pathToDebugger.c_str(), NULL);
if(hResult != S_OK) return hResult;

// Launch the application
DWORD dwProcessId = 0;
hResult = AppUtils::LaunchApplication(appName, &dwProcessId);
if(hResult != S_OK) return hResult;

/* Do more stuff after the app has been resumed by the debugger */

// Stop debugging the application so it can run as normal
hResult = debugSettings->DisableDebugging(appFullName.c_str());
if(hResult != S_OK) return hResult;

I wrote a basic wrapper for IPackageDebugSettings as well as a few utility functions for dealing with Windows 10 Store apps to make the overall process easier.



回答2:

In Visual Studio go to the properties of your project. Then go into the Debug tab and select 'Do not launch, but debug my code when it starts'.

Is this what you're looking for?