• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

C++/C# COM Wrappers

Hi Folks,

I am working with an SDK that uses Visual C++ 6.0 and COM objects.
I want to code in C# and was wondering how I go about wrapping COM objects for the SDK?

Previously I have written C++/C# wrappers for DLL's and this has worked a treat using the using System.Runtime.InteropServices along with the [DllImport("*.dll")]

I am unsure how to proceed with C++ COM wrapping, can you use the same DllImport along with InteropServices or do you need to use something else?


Thanks in advance.
Cheers,
Con
0
CoreSupport
Asked:
CoreSupport
  • 2
  • 2
1 Solution
 
enwhyseeCommented:
You can use a COM DLL by exporting its type information/metadata with the tlbimp.exe tool that comes with .NET Framework. Tlbimp will generate a .NET assembly that will serve as a sort of intermediary between regular .NET code and your COM DLL. In other words, you'd be able to use the COM DLL just as if it was any regular .NET DLL, which is quite convenient.

You can find tlbimp.exe somewhere under Program Files (depending on how you installed .NET). For example, I have it under "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin"

Check out the following article for a more detailed example:
http://www.c-sharpcorner.com/Code/2002/April/COMInteropP1AJ.asp

Tlbimp.exe reference from Microsoft:
http://msdn2.microsoft.com/en-us/library/tt0cf3sx.aspx
0
 
CoreSupportAuthor Commented:
Hi enwhysee,

Thanks for the help!!
I had been looking at this method this morning but wasn’t sure if it was the way to go with it.. NOVICE :-)

Have done what you said and looks good.
Just have to find out how you call the CoCreateInstance using the [DllImport("ole32.Dll")] for my object!!!

Cheers,
Con
0
 
enwhyseeCommented:
Glad it helps :) You should be able to instantiate it normally as if it was a .NET assembly, i.e.

MyCOM.COMClass obj = new MyCOM.COMClass();

I found another article that describes it all in more detail:
http://www.dnjonline.com/article.aspx?ID=sep01_COMtodotNET
0
 
CoreSupportAuthor Commented:
Hi enwhysee,

Thanks for the help again and the article looks the part.
You were right about being able to instantiate it normally as if it was a .NET assembly.
The SDK had given me _ClassX in .NET and also just ClassX.
When I tried to create a _ClassX myClass = new _ClassX (); it failed.

But without the underscore it worked a treat...
Much appreciated.
Cheers,
Con
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now