Created
November 27, 2012 20:16
-
-
Save domenic/4156732 to your computer and use it in GitHub Desktop.
COM error handling critique me
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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