You need to write a DllGetClassObject function and export it. This function is responsible for highlighting the "class factory", which you also need to write, and which, in turn, is able to distribute instances of your COM object. It should implement IClassFactory .
This is not too difficult to do. An alternative is to use ATL (see xhantt's answer), which theoretically does this for you, but in practice it is a real mess. Somehow, he manages to encapsulate the complexity of COM inside an abstraction layer, which is even more complicated. Good luck, for example, trying to move an object between DLLs.
But you can run the ATL wizard to see an example of a DllGetClassObject declaration. Implementing IClassFactory very simple - just one method that creates a news item.
Then you need to register your DLL - that is, put the keys in the registry. The regsvr32 tool cannot do this without additional help from you. You have to write and export another function called DllRegisterServer that does all the hard work. All that regsvr32 does is load the DLL, look at the DllRegisterServer and call it.
Again, ATL has a way to implement this for you, but it does this by reading a kind of script with complete instructions for modifying the registry stored in the .rgs file that is embedded in your DLL resources. If you accidentally put any syntax error in this file, the registration fails.
So, you can find an easier way to write a few lines of code to set up the registry yourself. Here are the details .
If you used C # instead, you would not have one of these problems. Everything is encapsulated very cleanly. It really works much better than C ++ as a tool for developing COM objects.
Daniel Earwicker
source share