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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:

Tlbimp.exe reference from Microsoft:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!!!

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:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.