C++/C# COM Wrappers

Posted on 2006-04-12
Last Modified: 2008-02-01
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.
Question by:CoreSupport
    LVL 6

    Accepted Solution

    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:

    Author Comment

    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!!!

    LVL 6

    Expert Comment

    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:

    Author Comment

    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.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now