Skip to content

Instantly share code, notes, and snippets.

@MattHarrington
Created January 11, 2013 07:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattHarrington/4508572 to your computer and use it in GitHub Desktop.
Save MattHarrington/4508572 to your computer and use it in GitHub Desktop.
This is a Windows 8 console application which will launch a Windows Store app by its AppUserModelId.
#include "stdafx.h"
#include <shlobj.h>
#include <stdio.h>
#include <shobjidl.h>
#include <objbase.h>
#include <atlbase.h>
#include <string>
/*++
Routine Description:
This routine launches your app using IApplicationActivationManager.
Arguments:
strAppUserModelID - AppUserModelID of the app to launch.
pdwProcessId - Output argument that receives the process id of the launched app.
Return value:
HRESULT indicating success/failure
--*/
HRESULT LaunchApp(const std::wstring& strAppUserModelId, PDWORD pdwProcessId)
{
CComPtr<IApplicationActivationManager> spAppActivationManager;
HRESULT hrResult = E_INVALIDARG;
if (!strAppUserModelId.empty())
{
// Instantiate IApplicationActivationManager
hrResult = CoCreateInstance(CLSID_ApplicationActivationManager,
NULL,
CLSCTX_LOCAL_SERVER,
IID_IApplicationActivationManager,
(LPVOID*)&spAppActivationManager);
if (SUCCEEDED(hrResult))
{
// This call ensures that the app is launched as the foreground window
hrResult = CoAllowSetForegroundWindow(spAppActivationManager, NULL);
// Launch the app
if (SUCCEEDED(hrResult))
{
hrResult = spAppActivationManager->ActivateApplication(strAppUserModelId.c_str(),
NULL,
AO_NONE,
pdwProcessId);
}
}
}
return hrResult;
}
int _tmain() // Removed command line arguments
{
std::wstring myApp (L"Microsoft.BingNews_8wekyb3d8bbwe!AppexNews"); // Hard-code your AppUserModelId here.
int argc = 2; // Lazy way of faking a command line argument so I don't have to change the rest of the code.
HRESULT hrResult = S_OK;
if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
{
if (argc == 2)
{
DWORD dwProcessId = 0;
//++argv; // Comment this out
hrResult = LaunchApp(myApp, &dwProcessId); // changed argument to myApp from *argv
}
else
{
hrResult = E_INVALIDARG;
}
CoUninitialize();
}
return hrResult;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment