How to create a COM library in Visual Studio 2008?

It has been a long time since I wrote COM-dll. Now I have made a couple of classes that inherit from some COM interfaces, but I want to test them. I know that I have to put the GUID somewhere and then register it with regsvr32, but what steps have been taken?

Edit: Sorry, forgot to mention that I am using C ++.

+7
c ++ guid dll visual-studio-2008 com
source share
3 answers

To create a new ATL COM project, you can proceed as follows:

  • File / New Project
  • Visual C ++ / ATL / ATL Project
  • Configure its settings and click Finish when done.

You created a new dll, but it is empty to add a COM object that you can do:

  • Project / Add Class
  • Simple Visual C ++ / ATL / ATL object, click
  • Specify the name you want (for example, MyObject), and click Finish to add it

If you want the object to implement an interface

  • In the class view, select the object class (CMyObject)
  • Right click / Add / Run Interface ...
  • You can choose which interface will be implemented
    • From an .idl file already in your project files
    • From the .tlb / .dll / .exe file into which the type library is built
    • From registered object
  • When finished, click Finish.

PS: It’s much easier to create a new ATL project with the same name in a different folder and add the files that you configured. The wizard performs several tasks and creates several custom files.

For large projects that are difficult to add a file by file, I do the same, but instead of adding my files to a new project, I start copying settings from new projects to old ones and adding an additional file that the wizard created and corrected the headers. such as stdafx.h to combine new settings.

PPS: If you want your dll to support MFC, instead of selecting ATL Project you should select DLC MFC / MFC. When you add a simple ATL object, the wizard will ask you to add ATL support to the project.

+10
source share

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.

+3
source share

When you create a solution, it automatically registers the dll. He also creates two _i.c and .h files.

To test the dll, create an example application:

  • Create a sample Win32 application. Include _i.c and .h in the cpp file of the win32 application, which has the main function

  • Call CoInitialize ();

  • Declare an interface pointer CComPtr pMyInterface = NULL; // where IMyInterface is declared in _i.c

  • Create an instance of pMyInterface.CoCreateInstance (CLSID_MyClass); // CLSID_MyClass is a GUID representing CoClass

  • Call the APIs present in the interface

  • Call CoUnInitialize ();

+1
source share

All Articles