Udostępnij za pośrednictwem


Avoid Registration Free COM manifest problems with Activation Context API

Having difficulties while loading the Side-by-Side assembly manifests for Registration-Free COM?

Perhaps Activation context API could come to your rescue (Ref: https://msdn2.microsoft.com/en-us/library/aa374166.aspx).

The activation context structure lets you explicitly specify the path of the manifest to be loaded, among other things, using the CreateActCtx and ActivateActCtx functions.

In the sample code below, when we try to create an instance of a COM component (that is not registered using RegSvr32), we encounter a failure. However, after creating an activation context that refers to the path of the Side-by-Side manifest and activating it, the CoCreateInstance succeeds.

 IMyComponent* pObj = NULL;

 // CoCreateInstance Fails without the component being registered

 HRESULT hr = CoCreateInstance(CLSID_MyComponent, NULL, CLSCTX_INPROC_SERVER, IID_IMyComponent, (void**)&pObj);

 ACTCTX actctx;

 ZeroMemory(&actctx, sizeof(actctx));

 actctx.cbSize = sizeof(actctx);

 actctx.lpSource = szManifestFilePath; // Path to the Side-by-Side Manifest File

 HANDLE pActCtx = CreateActCtx(&actctx);

 if(pActCtx == INVALID_HANDLE_VALUE) { return E_FAIL; }

 ULONG_PTR lpCookie;

 if(ActivateActCtx(pActCtx, &lpCookie))
 {

   // CoCreateInstance should succeed now. 

   hr = CoCreateInstance(CLSID_MyComponent, NULL, CLSCTX_INPROC_SERVER, IID_IMyComponent, (void**)&pObj);
   DeactivateActCtx(0, lpCookie);
 }

 ReleaseActCtx(pActCtx);

 return hr;

The advantage here is that we can at run-time decide which manifest file to load and can have control over when to load (instead of letting the loader do it at the application loading time). The manifest file path specified in the activation context could be any valid file system path. However, the COM DLL being referred by the manifest should reside within the same sub-directory structure as that of the manifest.

For a description on the format of the manifest files that should be used for Side-by-Side assemblies, refer to https://msdn2.microsoft.com/en-us/library/aa375632.aspx.