Click here to Skip to main content
11,641,392 members (57,983 online)
Click here to Skip to main content

Calling Managed .NET C# COM Objects from Unmanaged C++ Code

, 11 Jan 2006 330.2K 4.6K 128
Rate this:
Please Sign up or sign in to vote.
An article on calling managed .NET C# COM objects from unmanaged C++ code.

Preface

COM Interoperability is the feature of Microsoft .NET that allows managed .NET code to interact with unmanaged code using Microsoft's Component Object Model semantics.

This article is geared towards C# programmers who are familiar with developing COM components and familiar with the concept of an interface. I'll review some background on COM, explain how C# interacts with COM, and then show how to design .NET components to smoothly interact with COM.

For those die-hard COM experts, there will be some things in this article that are oversimplified, but the concepts, as presented, are the important points to know for those developers supplementing their COM code with .NET components.

Introduction

.NET Interfaces and Classes

The basis for accessing .NET objects either from other .NET code or from unmanaged code is the Class. A .NET class represents the encapsulation of the functionality (methods and properties) that the programmer wants to expose to other code. A .NET interface is the abstract declaration of the methods and properties that classes which implement the interface are expected to provide in their implementations. Declaring a .NET interface doesn't generate any code, and a .NET interface is not callable directly. But any class which implements ("inherits") the interface must provide the code that implements each of the methods and properties declared in the interface definition.

Microsoft realized that the very first version of .NET needed a way to work with the existing Windows technology used to develop applications over the past 8+ years: COM. With that in mind, Microsoft added support in the .NET runtime for interoperating with COM - simply called "COM Interop". The support goes both ways: .NET code can call COM components, and COM code can call .NET components.

Using the code

Steps to create a Managed .NET C# COM Object:

  1. Open VS.NET2003->New Project->Visual C# Projects->Class Library.
  2. Project name: MyInterop.
  3. Create MyDoNetClass.cs file, and add the following lines of code:
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
  4. Create an Interface IMyDotNetInterface.
  5. Create a class MyDoNetClass.
  6. Add the following line for MyDotNetClass:
    [ClassInterface(ClassInterfaceType.None)]

Although a .NET class is not directly invokable from unmanaged code, Microsoft has provided the capability of wrapping a .NET interface in an unmanaged layer of code that exposes the methods and properties of the .NET class as if the class were a COM object. There are two requirements for making a .NET class visible to unmanaged code as a COM object:

Requirement 1:

You have to add GUIDs - Globally Unique Identifiers - into your code for the interface and the class separately, through a GUID tool.

  1. Now, create a GUID for the Interface, and add the following line for the interface:
    [Guid("03AD5D2D-2AFD-439f-8713-A4EC0705B4D9")]
  2. Now, create a GUID for the class, and add the following line for the class:
    [Guid("0490E147-F2D2-4909-A4B8-3533D2F264D0")]
  3. Your code will look like:
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace MyInterop
    {
        [Guid("03AD5D2D-2AFD-439f-8713-A4EC0705B4D9")]
        interface IMyDotNetInterface
        {
            void ShowCOMDialog();
        }
         
        [ClassInterface(ClassInterfaceType.None)]
        [Guid("0490E147-F2D2-4909-A4B8-3533D2F264D0")]
        class MyDotNetClass : IMyDotNetInterface
        {
            // Need a public default constructor for COM Interop.
            public MyDotNetClass()
            {}
            public void ShowCOMDialog()
            {
                System.Windows.Forms.MessageBox.Show(“I am a" + 
                      "  Managed DotNET C# COM Object Dialog”);
            }
        }
    }
  4. Compile the solution.
  5. You will see inside the project directory->obj->debug directory, the file “MyInterop.dll” generated after compilation.

Requirement 2:

Registration of the COM Class and Interfaces

For a COM class to be accessible by the client at runtime, the COM infrastructure must know how to locate the code that implements the COM class. COM doesn't know about .NET classes, but .NET provides a general "surrogate" DLL - mscoree.dll -- which acts as the wrapper and intermediary between the COM client and the .NET class.

  1. Hard-code a specific version number in your AssemblyVersion attribute in the AssemblyInfo.cs file which is in your project.

    Example:

    [assembly: AssemblyVersion("1.0.0.0")]
  2. Create a strong-name key pair for your assembly and point to it via the AssemblyKeyFile attribute in the AssemblyInfo.cs file which is in your project. Example:
    sn -k TestKeyPair.snk
    [assembly: AssemblyKeyFile("TestKeyPair.snk")]
  3. Add your assembly to the GAC using the following command:
    gacutil /i MyInterop.dll
  4. Register your assembly for COM by using the REGASM command along with the "/tlb" option to generate a COM type library.
    REGASM MyInterop.dll /tlb:com.MyInterop.tlb
  5. Close the C# project.

Steps to create an Unmanaged C++ application to call a .NET Managed C# COM

  1. Open VS.NET2003->New Project->Visual C++ Projects->Win32->Win32 Console Project.
  2. Name: DotNet_COM_Call.
  3. Include the following line in your DoNet_COM_Call.cpp file:
    #import “<Full Path>\com.MyInterop.tlb" named_guids raw_interfaces_only
  4. Compile the solution.
  5. It will generate a “com.myinterop.tlh” file into your project->debug directory.
  6. You can open this file and see the contents. This is basically the proxy code of the C# COM code.
  7. Now, you can write the code to call the .NET Managed COM.
  8. Please add the following lines of code before calling the COM exported functions:
    CoInitialize(NULL);   //Initialize all COM Components
        
    // <namespace>::<InterfaceName>
    MyInterop::IMyDotNetInterfacePtr pDotNetCOMPtr;
    
    // CreateInstance parameters
    // e.g. CreateInstance (<namespace::CLSID_<ClassName>)
    HRESULT hRes = 
      pDotNetCOMPtr.CreateInstance(MyInterop::CLSID_MyDotNetClass);
    if (hRes == S_OK)
    {
        BSTR str;
        pDotNetCOMPtr->ShowCOMDialog ();
        //call .NET COM exported function ShowDialog ()
    }
    
    CoUninitialize ();   //DeInitialize all COM Components
  9. Run this console application.
  10. Expected result: a managed code (C# ) dialog should appear with the string “I am a Managed DotNET C# COM Object Dialog”.

Points of Interest

While creating an Interface for COM exported functions, creating GUIDs for the Interface and the class and registering the class are required steps, and doing all this is always interesting and fun. Calling parameterized exported functions also is very interesting.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Atul Mani
Web Developer
United States United States
Atul Mani Tripathi has been a professional developer since 1997 and working as a senior consultant with Cognizant Technology Solutions (CTS) .

Most of the time, his focus is on creating a clean and simple solution to development problems. He is a C++, Visual C++ and C# (Dot NET) guy who loves Visual Studio, Google, Code Project and developing personal projects.


You may also be interested in...

Comments and Discussions

 
Questionmanaged C# method returns a class object Pin
asagi16-Dec-14 23:10
memberasagi16-Dec-14 23:10 
QuestionKK: error CS0101: The namespace 'MyInterop' already contains a definition for 'IMyDotNetInterface' Pin
Member 1114939216-Oct-14 1:52
memberMember 1114939216-Oct-14 1:52 
Questionmoving COM dll to another system Pin
Member 1104201415-Oct-14 22:39
memberMember 1104201415-Oct-14 22:39 
QuestionRegasm error Pin
Member 1096403510-Oct-14 9:11
memberMember 1096403510-Oct-14 9:11 
QuestionMultiple ShowCOMDialog() calls Pin
Jake Driscoll4-Dec-13 10:32
memberJake Driscoll4-Dec-13 10:32 
BugCorrection In Code(Interface should be Public) Pin
DavidSam524-Aug-13 7:45
memberDavidSam524-Aug-13 7:45 
QuestionSource code not available Pin
Pratik Pattanayak23-Jun-13 20:06
memberPratik Pattanayak23-Jun-13 20:06 
QuestionHandle C# COM Events in VC++ Pin
Member 737491217-Mar-13 21:42
memberMember 737491217-Mar-13 21:42 
QuestionRe: Multiple constructors callable? Pin
mla1543-Jul-12 10:12
membermla1543-Jul-12 10:12 
QuestionHow do I release the managed .net object. Pin
sandeep_sinha21-Jun-12 22:52
membersandeep_sinha21-Jun-12 22:52 
GeneralMy vote of 5 Pin
manoj kumar choubey28-Feb-12 18:13
membermanoj kumar choubey28-Feb-12 18:13 
QuestionUseful article, GACUtil and SNK not necessarily required, update to VS2010? Pin
DavidCarr17-Feb-12 10:41
memberDavidCarr17-Feb-12 10:41 
QuestionGAC registration is not required Pin
jive_b25-Jan-12 6:02
memberjive_b25-Jan-12 6:02 
GeneralMy vote of 5 Pin
Menon Santosh15-Dec-11 0:43
memberMenon Santosh15-Dec-11 0:43 
Questionhow to release onto production PCs Pin
Member 315687714-Nov-11 8:58
memberMember 315687714-Nov-11 8:58 
QuestionFile '...\ManagedCOM\Call_CSharp_COM\Call_CSharp_COM.vcproj' was not found. Pin
on2Gaute13-Jul-11 2:18
memberon2Gaute13-Jul-11 2:18 
QuestionI am not able to access intefaces from C# DLL Pin
Member 79736701-Jun-11 19:36
memberMember 79736701-Jun-11 19:36 
QuestionRe: I am not able to access intefaces from C# DLL Pin
Member 823172414-Sep-11 8:52
memberMember 823172414-Sep-11 8:52 
AnswerRe: I am not able to access intefaces from C# DLL Pin
Member 797367020-Sep-11 0:39
memberMember 797367020-Sep-11 0:39 
AnswerRe: I am not able to access intefaces from C# DLL Pin
Member 1144322710-Feb-15 17:34
memberMember 1144322710-Feb-15 17:34 
GeneralMy vote of 5 Pin
Anoop Chandran M27-Oct-10 3:58
memberAnoop Chandran M27-Oct-10 3:58 
GeneralRegarding Memory leak Pin
mbcvamsidhar4-Oct-10 11:38
membermbcvamsidhar4-Oct-10 11:38 
GeneralRe: Regarding Memory leak Pin
Member 771320628-Dec-11 9:49
memberMember 771320628-Dec-11 9:49 
GeneralCompile using DevC++ Pin
yusof_hardy19-Mar-10 14:58
memberyusof_hardy19-Mar-10 14:58 
I can compile the C++ code in Visual C++ 2008.
BUt I can't compile it using Dev-C++.

How do I make it compatible with Dev-C++?
GeneralYou forgot to put "public" before the interface and call definition in the c# project. Pin
zengamer211-Dec-09 7:37
memberzengamer211-Dec-09 7:37 
GeneralRe: You forgot to put "public" before the interface and call definition in the c# project. Pin
zengamer211-Dec-09 7:38
memberzengamer211-Dec-09 7:38 
GeneralRe: You forgot to put "public" before the interface and call definition in the c# project. Pin
segber21-Jan-10 7:39
membersegber21-Jan-10 7:39 
QuestionWhat if path to tlb is different Pin
cos081531-Jul-09 4:54
membercos081531-Jul-09 4:54 
GeneralHRESULT:0x80040154 Pin
erdiay25-May-09 3:11
membererdiay25-May-09 3:11 
GeneralRe: HRESULT:0x80040154 Pin
M i s t e r L i s t e r8-Oct-13 4:36
memberM i s t e r L i s t e r8-Oct-13 4:36 
Questionwhat about properties ? Pin
roboticEDAR14-May-09 14:43
memberroboticEDAR14-May-09 14:43 
QuestionHow to handle Events. Pin
BicycleTheif16-Apr-09 1:44
memberBicycleTheif16-Apr-09 1:44 
GeneralAssembly used as COM Pin
MarkusSchreiber24-Feb-09 20:28
memberMarkusSchreiber24-Feb-09 20:28 
General0x80040154 Class not registered Pin
xamlavoie13-Nov-08 3:18
memberxamlavoie13-Nov-08 3:18 
GeneralGetting Unhandled exception [modified] Pin
plenitude21-Sep-08 21:24
memberplenitude21-Sep-08 21:24 
GeneralDebugging Pin
Sitaram Sukumar29-Jul-08 23:13
memberSitaram Sukumar29-Jul-08 23:13 
GeneralRe: Debugging Pin
Pratik Pattanayak23-Jun-13 20:14
memberPratik Pattanayak23-Jun-13 20:14 
GeneralCannot instantiate abstract class. [modified] Pin
theCPkid4-Jul-08 4:24
membertheCPkid4-Jul-08 4:24 
GeneralRe: Cannot instantiate abstract class. Pin
Foxhunter4214-Jun-10 4:17
memberFoxhunter4214-Jun-10 4:17 
GeneralRe: Cannot instantiate abstract class. Pin
theCPkid14-Jun-10 17:58
membertheCPkid14-Jun-10 17:58 
GeneralProblem with CLSID_MyDotNetClass Pin
Charllemagne4-Jun-08 0:18
memberCharllemagne4-Jun-08 0:18 
GeneralRe: Problem with CLSID_MyDotNetClass Pin
Jeffgosurfing12-Jun-08 8:47
memberJeffgosurfing12-Jun-08 8:47 
GeneralRe: Problem with CLSID_MyDotNetClass Pin
on2Gaute13-Jul-11 3:25
memberon2Gaute13-Jul-11 3:25 
Generalcom.MyInterop.tlb file is not getting created Pin
Member 28779846-May-08 0:03
memberMember 28779846-May-08 0:03 
GeneralCould'nt ablw to run the demo of MyInterop Pin
Member 28779846-May-08 0:00
memberMember 28779846-May-08 0:00 
GeneralRe: Could'nt ablw to run the demo of MyInterop Pin
roboticEDAR13-May-09 22:16
memberroboticEDAR13-May-09 22:16 
GeneralRe: Could'nt ablw to run the demo of MyInterop Pin
xman_mylife23-Feb-11 1:22
memberxman_mylife23-Feb-11 1:22 
GeneralProblem with CLSID_MyDotNetClass Pin
erick.gaither31-Mar-08 11:27
membererick.gaither31-Mar-08 11:27 
GeneralRe: Problem with CLSID_MyDotNetClass Pin
Charllemagne3-Jun-08 22:37
memberCharllemagne3-Jun-08 22:37 
GeneralRe: Problem with CLSID_MyDotNetClass Pin
Robby_19862-May-10 23:48
memberRobby_19862-May-10 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 11 Jan 2006
Article Copyright 2006 by Atul Mani
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid