Tutorial :Registering implementation of a COM interface

Question:

I'm new to COM programming. I've got a COM object (and associated IClassFactory) all ready to go, but I can't quite figure out how to go about registering the resulting DLL for use by other programs. The number of GUIDs I need to sling around is also unclear to me.

I have come across the DllRegisterServer and DllUnregisterServer functions, but I haven't found any clear demonstrations of their usage. What keys do they deal with, how are they invoked, by what and when, etc.?

Thanks, -Kevin Montrose

Solution:1

I'm not sure from this post whether you are implementing or consuming the DLL that supports IAudioSessionEvents. If you're consuming this DLL, then you can register the component using the comment line utility regsvr32. To register use:

regsvr32

To unregister:

regsvr32 /u

regsvr32 should be on your path, so this command will work from any directory.

If you are implementing the DLL in question, then you must provide an implementaion of the DllRegisterServer and DllUnRegisterServer functions. These functions must set up and clean up registry entries for your component. The purpose of the registry entries is to provide a ProgID, map it to a CLSID, and provid interface ID for the interfaces that the component supports. For example, the interface ID for IAudioSessionEvent. If you're implementing the DLL, you'll have to provide code to perform all of these tasks.

Note: these functions are called by regsvr32 in order to register the component.

If very unusual to actually write this code, generally you'll want to use a framework like ATL, which takes care of the busywork for you. It is a good exercise to write this code at least once if you really want to know COM from the ground up.

Solution:2

You need one GUID for every class you expose to COM and one GUID for every new interface you introduce and want to make available through COM.

DllRegisterServer/DllUnregister server are called when you use regsvr32 utility (ships with Windows) to register your COM-exposed classes. It adds/removes keys to HKCR/CLSID branch for every class you expose to COM. These keys are used by CoCreateInstance() to find out which DLL to load for creating an instance of a class with a given GUID.

If you use ATL or something similar usually don't need to completely implement DllRegisterServer/DllUnRegisterServer but use the implementation provided with the library.

Solution:3

Quite often the easiest way to implement self registration is to use the ATL server classes, have a global variable that derives from CComModule (or some other similar class) and define a COM_MAP in your module. You then ask the com module to handle the registration based on the .rgs files that you have added to your project.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com