Reputation: 41347
I am planning to use various objects that are exposed as COM objects. To make them easier to use, I'd like to wrap them as C# objects. What is the best approach for this?
Upvotes: 41
Views: 65952
Reputation: 15968
If the library is already registered, you can perform the following steps to have Visual Studio generate an interop assembly for you:
This will be a class or set of C# classes that wrap all of the COM interface stuff with a normal C# class. Then you just use it like any other C# library. If the import of the reference worked well, you can explore it like any other reference and the methods/structs/classes/constants should show up in that namespace and intellisense.
This will get you started, at least. If this is deployed in a corporate environment or one you can control, this may be all you need.
Upvotes: 44
Reputation: 12004
You can (initially) just import the reference. If you need more control (or get errors from VS's import) you can use tlbimp in the windows sdk. This will create the interop assemblies. You can get class definitions from metadata.
EDIT: This is actually a lot more complicated if you want to work with 64 bit
Upvotes: 5