1
votes

I have a native C++ COM Server dll accompanied with type library (.tlb file) and .NET Interop assembly generated by TlbImp.exe. The server is registration free and I have a manifest to activate the context for it. I want to use the server from managed C# application. I activate the context right before creating an object of the server class but I'm not sure when should I deactivate it? I found the article (http://www.mazecomputer.com/sxs/help/sxsapi2.htm) which says

Note that unlike in the C++ example above, we cannot deactivate the activation context yet. COM Interop will need the context to locate the type library necessary to make the COM object call.

Should I keep the context activated for all or just the first COM object call? Is there detailed description on that matter on MSDN site?

1
Check this and this.noseratio

1 Answers

2
votes

and I have a manifest to activate the context for it

Calling Create/ActivateActCtx() explicitly in your code is never the right thing to do for a reg-free COM server. There are a few corner cases where it can be useful, but only for manifest entries that refer to side-by-side installed assemblies. DLLs that are deployed in c:\windows\winsxs. The canonical example is the manifest entry that enables Visual Styles, getting version 6.0 for comctl32.dll. You might not want to do that if you write a DLL that needs to live in a legacy app that doesn't use visual styles.

But never for a COM server since it cannot operate correctly without the manifest entry active. You want it active immediately at the start of the program, the default behavior for manifest entries in the client's manifest.

Do keep in mind that this is all entirely automatic. Simply set the Isolated property of the type library reference to True. That gets the build system to lookup the registration for the server in the registry and auto-generates the manifest. No need for you to do anything at all, you can never get it wrong that way. The only requirement is that the server is actually registered on your machine so the registry keys can be read. Which is fine, you only really care about this on the user's machine.