Search code examples
c++visual-c++static-linkingdynamic-linkingidl

IDL file - static linking or dynamic linking


I want to use the function interface "IApplicationActivationManager" & it's member functions "IApplicationActivationManager::ActivateApplication" MSDN link

It's present in Shobjidl.h & Shobjidl.idl. I've seen *.DLL or *.lib as the dependency where the Microsoft functions are defined, so we know wheather it's static linking or dynamci linking.

But I'm trying to understand what does IDL do - dunamic or static lining ? DO I need to provide any DLL/lib ? How does the lining happen ?


Solution

  • IApplicationActivationManager is a COM interface type. COM objects are always created dynamically at runtime, they don't have a linking requirement beyond ole32.lib, possibly a .lib that defines the GUID for the object. You obtain the interface pointer with the universal COM object factory, CoCreateInstance().

    A sample probably helps, I'll post the code for a Win32 console mode app that activates the weather app on Windows 8. Create the project from the provided VS project template. No changes required, the template already links everything that's needed (kernel32.lib, ole32.lib and uuid.lib).

    #include "stdafx.h"
    #include <Windows.h>
    #include <ShlObj.h>
    #include <assert.h>
    
    int main()
    {
        auto hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
        assert(SUCCEEDED(hr));
        IApplicationActivationManager* itf;
        hr = CoCreateInstance(CLSID_ApplicationActivationManager, NULL, 
                              CLSCTX_LOCAL_SERVER,
                              __uuidof(IApplicationActivationManager),
                              (void**)&itf);
        if (SUCCEEDED(hr)) {
            auto weatherApp = L"Microsoft.BingWeather_8wekyb3d8bbwe!App";
            DWORD dontuse;
            hr = itf->ActivateApplication(weatherApp,
                                          L"", AO_NONE, &dontuse);
            assert(SUCCEEDED(hr));
            itf->Release();
        }
        CoUninitialize();
        return 0;
    }
    

    You need the application user model ID for the app that you want to activate, this MSDN page describes how to discover them.