Monday 19 December 2011

COM to managed

So, you need to work with COM.
You have :
    - idl interface(a .idl file that contains the interface of the COM dll)
You need to make:
    - Com dll that implements the interface

 1. The hard way, i do not want to go into details of how atl and com code will try to sabotage you on every step.

2. The way:
         - use Visual Studio tools. To be more exact , open Visual Studio Command prompt
         - execute msil myIdl.idl , more info at: Msil . This will create a tbl file that contains the binary form of the idl interface
         - tbl file will be created by default only if  the idl file contains the library tag. If your idl contains only interface tags , you need to create another idl file that contains library and references the idl with interfaces. Also if no library tags are present the output when executing msil will not be a tbl file, just c and h files that you can reference from your code.

Example of a file that contains library and references the interfaces(most important line is import "MyIdl.idl";) :

- ok, now you have a tlb file, next step is: tlbimp MyIdl.tlb , documentation at Tlb import
- this will create a managed dll that you can just reference in your application, use it and implement the interfaces
- optional step : in the dll you will find already made classes that use the interfaces and have the methods marked as virtual. You can make your class on top of this class or you can just use the interfaces in a direct way (your class on top of the interfaces) (oh yeah , on top means derived from).
In fact, .NET framework 4 has a problem if you choose to use the classes.


FINAL STEP : COM registration
- so you made this nice dll that implements the interfaces, last thing to do is register this dll
- command to execute for registration is regasm /codebase MyDll.dll 
- however, you might find out that this thing fails horribly, so :
- in AssemblyInfo set ComVisible to true
- what you want to do is add some lines of code to the classes in the dll that you want to register for COM in order to create registry entries :



Hope it helped .





No comments:

Post a Comment