IJW (It Just Works) and COM Interop

As I promised in my last post, today I am going to give an overview about how to call C# code from C++. After googling around a bit you will find there are at least 2 well-known ways to call C# from C++.

It Just Works

The first one I want to mention is “IJW” that stands for “It Just Works”. As the name implies, there is nothing much to discuss about IJW, is pretty easy to use, all one have to do is specify the /clr switch in the Microsoft VS compiler. This switch will cause your C++ code to be compiled to IL (Intermediate Language) and therefore will run in a managed environment. Nice isn’t it? However there is a catch, even though your code will be compiled to IL, the classes are not managed, which means among other things that the CLR will not take care of the memory. This is kind of obvious, since the original code was meant to take care of the memory itself, all the /clr switch does is give the opportunity to that old C++ code to run in managed environment.

Once that we have this C++ code working on a managed environment, there is some new fun stuff we can do with it.

#using <mscorlib.dll>
#using <myassembly.dll>

#include <stdio.h>

using namespace System;
using namespace MyNameSpace;

void callSomethingInMyAssembly()

    Console::WriteLine("In Managed Environment!");

#pragma unmanaged

void oldAndCrappyFunction()

The compiler pragma directive “managed” and “unmanaged” will help you to switch from managed to unmanaged and viceversa. This way you can call managed code from your old unmanaged code. Be aware that this solution will not work when you have an old DLL that other components depend on, since the resulting DLL will be a DLL with IL code and therefore not callable from other unmanaged DLLs.

COM Interop

To use COM Interop, the managed code, in this case, a C# class needs to allocate an UUID to be used when creating a COM instance. The following code shows how to create code with an interface and class with UUIDs.

using System;

using System.Runtime.InteropServices;

namespace ManagedNameSpace 

/* The GUID should be generated using guidgen.exe */
public interface IManagedComponent
        void ManagedMethod();


public class ManagedClass : IManagedComponent
        public void ManagedMethod()

                Console.WriteLine("in managed environment!\n");


You can create a UUID using the tool guidgen.exe. The class should be compiled with

C:\csc /target:library mycomponent.cs

That will generate a mycomponent.dll assembly. In order to other COM components to use our C# class we must register the DLL, we can do so using regasm utility:

C:\regasm mycomponent.dll /tlb:mycomponent.tlb

Now that the DLL is registered you can use it from any language that supports COM, C++ included. Before proceeding to call this C# code from C++, you may be wondering about that mycomponent.tlb file specified as argument to regasm /tlb switch. That file is known as a “type library” and is used by the COM infrastructure to save meta-data about the types involved in the exported interfaces, that way any language that supports COM can interact with any other COM component, in this case, written in C#.

Finally let’s call our C# component from C++ code.

#include <windows.h>
#include <iostream>

using namespace std;

#import <mscorlib.tlb> raw_interfaces_only

#import "dtcom.tlb" no_namespace named_guids

int main(int argc, char *argv[])

        IManagedComponent *component;
        HRESULT hr;

        /* COM require an initialize routine */

        hr = CoCreateInstance(CLSID_ManagedClass, NULL, 
                        CLSCTX_INPROC_SERVER, IID_IManagedComponent, reinterpret_cast<void **>(&component));

        if ( FAILED(hr) ) {
                cerr << "Could not create component instance." << endl;

                return -1;

        cout << "Calling managed methods." << endl;

        /* COM Require to release the instance */

        /* Close COM */

        return 0;

As you can see we use the CoCreateInstance COM function to create an object that references (indirectly via CCW ) to the C# object.

So, that’s it, you can learn more about COM at Wikipedia.

This entry was posted in C/C++, windows. Bookmark the permalink.

2 Responses to IJW (It Just Works) and COM Interop

  1. Joy says:

    “dtcom.tlb” should actually be mycomponent.tlb created before by me, right?

  2. olmuser says:

    hi, I am able to follow the article but have some issues with my implementation, could I bother you with some questions?

Leave a Reply

Your email address will not be published. Required fields are marked *