Skip to content

Instantly share code, notes, and snippets.

@domenic
Created November 27, 2012 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domenic/4156732 to your computer and use it in GitHub Desktop.
Save domenic/4156732 to your computer and use it in GitHub Desktop.
COM error handling critique me
#include <ShObjIdl.h>
#include <wchar.h>
HRESULT CreateAAM(IApplicationActivationManager*& aam)
{
return CoCreateInstance(CLSID_ApplicationActivationManager, nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&aam));
}
HRESULT ActivateApp(IApplicationActivationManager* aam, const wchar_t* appId)
{
unsigned long unused = 0;
return aam->ActivateApplication(appId, nullptr, AO_NONE, &unused);
}
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.");
}
else
{
hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
wprintf(L"Error initializing COM");
}
else
{
IApplicationActivationManager* aam = nullptr;
hr = CreateAAM(aam);
if (FAILED(hr))
{
wprintf(L"Error creating ApplicationActivationManager");
}
else
{
hr = CoAllowSetForegroundWindow(aam, nullptr);
if (FAILED(hr))
{
wprintf(L"Error calling CoAllowSetForegroundWindow");
}
else
{
hr = ActivateApp(aam, argv[1]);
if (FAILED(hr))
{
wprintf(L"Error calling ActivateApplication");
}
}
aam->Release();
}
CoUninitialize();
}
}
return hr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment