This is my first C++ in many years. It's based on some stuff I found on the internet, including a Microsoft article on a similar topic. But I'm sure there are C++ idioms that might make this better or leaner (my main goal).
#include <ShObjIdl.h>
#include <atlbase.h>
#include <wchar.h>
int wmain(int argc, wchar_t* argv[])
{
HRESULT hr = S_OK;
if (argc < 2)
{
hr = E_INVALIDARG;
wprintf(L"Supply an app ID (AppUserModelId) for the application to launch.");
return hr;
}
const wchar_t* appId = argv[1];
hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
wprintf(L"Error initializing COM");
return hr;
}
CComPtr<IApplicationActivationManager> aam = nullptr;
hr = CoCreateInstance(CLSID_ApplicationActivationManager, nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&aam));
if (FAILED(hr))
{
wprintf(L"Error creating ApplicationActivationManager");
CoUninitialize();
return hr;
}
hr = CoAllowSetForegroundWindow(aam, nullptr);
if (FAILED(hr))
{
wprintf(L"Error calling CoAllowSetForegroundWindow");
CoUninitialize();
return hr;
}
unsigned long pid = 0;
hr = aam->ActivateApplication(appId, nullptr, AO_NONE, &pid);
if (FAILED(hr))
{
wprintf(L"Error calling ActivateApplication");
CoUninitialize();
return hr;
}
CoUninitialize();
return 0;
}
Pretty small and simple, but I want to open-source this. And I definitely know the feeling of looking at JavaScript newbies' code and going "ugh! why didn't they just do <x>!" So I want to come across as a suave sophisticated C++ programmer instead ;)