Click here to Skip to main content
12,290,988 members (59,404 online)
Click here to Skip to main content
Add your own
alternative version


74 bookmarked

Unmanaged to Managed calls (C++ to C#) without Regasm

, 10 Jan 2006
Rate this:
Please Sign up or sign in to vote.
A simple way to call a managed method from the unmanaged world.


As .NET is penetrating the development environment day by day, we are having unmanaged and managed code running parallel. These days, managed to unmanaged calls have become very popular. But unmanaged to managed calls are still tedious. So my aim was to make such calls as simple as possible.


Unmanaged to managed calls using Regasm are very common. I have tried a straightforward call from unmanaged C++ code to a managed C# code. The main funda which I worked on are:

  • Each type of .NET Framework application requires a piece of code called a Runtime host to start it. The runtime host loads the runtime into a process, creates the application domains within the process, and loads and executes user code within those application domains. This section explains how to write a runtime host that performs several fundamental tasks.
  • Operating systems and runtime environments typically provide some form of isolation between applications. This isolation is necessary to ensure that code running in one application cannot adversely affect other, unrelated applications.

    Application domains provide a more secure and versatile unit of processing that the common language runtime can use to provide isolation between applications. Application domains are typically created by runtime hosts, which are responsible for bootstrapping the common language runtime before an application is run.

  • Unmanaged hosting code is used to configure the common language runtime, load it in the process, and transition the program into managed code. The managed portion of the hosting code creates the application domains in which the user code will run and dispatches user requests to the created application domains.

    In my project, I have made an attempt to create a CLRHost to call methods of a managed DLL or EXE. I have done the steps described in the following section.

Using the code

Suppose we have a .NET DLL with a method declaration like this:

using System;

namespace ManagedDll
    public class ManagedClass
        public ManagedClass()

        public int Add(int i, int j)

Now, the C++ code which directly calls this DLL without using Regasm would be:

// ExposeManageCode.cpp : Defines the entry point
//                        for the console application.
// REF : MSDN : Accessing Members Through IDispatch

#include "stdafx.h"
#include <atlbase.h>
#include <mscoree.h>
#include <comutil.h>

// Need to be modified as your directory settings.
#import "C:\\WINNT\\Microsoft.NET\\Framework\\" 
        "v1.1.4322\\Mscorlib.tlb" raw_interfaces_only    

using namespace mscorlib;

int CallManagedFunction(char*, char*, BSTR, int, 
                          VARIANT *, VARIANT *);

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

    VARIANT varArgs[2] ;

    varArgs[0].vt = VT_INT;
    varArgs[0].intVal = 1;

    varArgs[1].vt = VT_INT;
    varArgs[1].intVal = 2;

    VARIANT varRet;
    varRet.vt = VT_INT;
    //Calling manageddll.dll Add() method.
    int iRet = CallManagedFunction("ManagedDll", 
        printf("\nSum = %d\n",varRet.intVal);

    return 0;

int CallManagedFunction(char* szAsseblyName, 
    char* szClassNameWithNamespace,BSTR szMethodName, 
    int iNoOfParams, VARIANT * pvArgs, VARIANT * pvRet)
    CComPtr<ICorRuntimeHost>    pRuntimeHost;
    CComPtr<_AppDomain>            pDefAppDomain;

        //Retrieve a pointer to the ICorRuntimeHost interface
        HRESULT hr = CorBindToRuntimeEx(
            NULL,    //Specify the version 
                     //of the runtime that will be loaded. 
            L"wks",  //Indicate whether the server
                     // or workstation build should be loaded.
            //Control whether concurrent
            //or non-concurrent garbage collection
            //Control whether assemblies are loaded as domain-neutral. 
            //Obtain an interface pointer to ICorRuntimeHost 
        if (FAILED(hr)) return hr;
        //Start the CLR
        hr = pRuntimeHost->Start();
        CComPtr<IUnknown> pUnknown;
        //Retrieve the IUnknown default AppDomain
        hr = pRuntimeHost->GetDefaultDomain(&pUnknown);
        if (FAILED(hr)) return hr;
        hr = pUnknown->QueryInterface(&pDefAppDomain.p);
        if (FAILED(hr)) return hr;
        CComPtr<_ObjectHandle> pObjectHandle;
        _bstr_t _bstrAssemblyName(szAsseblyName);
        _bstr_t _bstrszClassNameWithNamespace(szClassNameWithNamespace);
        //Creates an instance of the Assembly
        hr = pDefAppDomain->CreateInstance( 
        if (FAILED(hr)) return hr;
        CComVariant VntUnwrapped;
        hr = pObjectHandle->Unwrap(&VntUnwrapped);
        if (FAILED(hr)) return hr;
        if (VntUnwrapped.vt != VT_DISPATCH)    
            return E_FAIL;
        CComPtr<IDispatch> pDisp;
        pDisp = VntUnwrapped.pdispVal;
        DISPID dispid;
        DISPPARAMS dispparamsArgs = {NULL, NULL, 0, 0};
        dispparamsArgs.cArgs = iNoOfParams;
        dispparamsArgs.rgvarg = pvArgs;
        hr = pDisp->GetIDsOfNames (
        if (FAILED(hr)) return hr;
        //Invoke the method on the Dispatch Interface
        hr = pDisp->Invoke (
        if (FAILED(hr)) return hr;

        return ERROR_SUCCESS;
    catch(_com_error e)
        //Exception handling.

We need to be careful about some settings like:

  • The managed DLL should be in the right path, i.e., any mapped path, or inside the debug/release folder of the calling C++ project.

MSCOREE.H contains the definition of the interfaces used in this program.

Points of Interest

So it is done. Unmanaged to managed call becomes very simple, we just need to pass the namespace, class name and the method name with the arguments to be passed. If you want more fundas, click here.


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


About the Author

Chakrabarty Rajib
Web Developer
United States United States
Rajib is one of the many altruist guy working with Cognizant Technology Solution ... Smile | :)

You may also be interested in...

Comments and Discussions

Questionhow to pass a string to from c# to c++ where the length is not known? Pin
Member 826153824-Sep-14 4:17
memberMember 826153824-Sep-14 4:17 
QuestionThank you Pin
Member 81680188-Aug-12 15:51
memberMember 81680188-Aug-12 15:51 
QuestionHi, Can you help me with my issue? Pin
Steve Ween20-Jul-12 3:38
memberSteve Ween20-Jul-12 3:38 
Questionthis is stupid way to call the c# dll just add the c# dll as a reference to the win32 project with /clr option Pin
a ahole26-Jun-11 16:47
membera ahole26-Jun-11 16:47 
Questionhow to pass a string to c# from vc++ using the above function Pin
kindi19-Jul-10 0:44
memberkindi19-Jul-10 0:44 
AnswerRe: how to pass a string to c# from vc++ using the above function Pin
baek hum kyung17-Aug-12 19:43
memberbaek hum kyung17-Aug-12 19:43 
Questionhow would the code change if the constructor for the C# app took arguments? Pin
ronn kling21-Apr-10 10:34
memberronn kling21-Apr-10 10:34 
QuestionHow to call C# DLL functions from Delphi Pin
vijay.victory14-Apr-10 3:01
membervijay.victory14-Apr-10 3:01 
GeneralWhy it doesn't work if the C# method is static Pin
marvinmenaleal11-Jun-09 4:25
membermarvinmenaleal11-Jun-09 4:25 
GeneralInteresting information on arguments. Pin
Doug071620-Mar-09 9:51
memberDoug071620-Mar-09 9:51 
GeneralRe: Interesting information on arguments. Pin
Habib Ahmed2-Aug-12 4:57
memberHabib Ahmed2-Aug-12 4:57 
Generalgetting errors - please help urgent Pin
sanjeevkum5-Mar-09 4:44
membersanjeevkum5-Mar-09 4:44 
GeneralRe: getting errors - please help urgent Pin
drQyH16j876K23-Jun-09 15:01
memberdrQyH16j876K23-Jun-09 15:01 
GeneralQuestion of calling managed dll from unmanaged c++ [modified] - Please help- Pin
sanjeevkum4-Mar-09 17:38
membersanjeevkum4-Mar-09 17:38 
GeneralUnmanaged to Managed, how to pass data by reference Pin
Mustafa Alahwel19-Feb-09 4:03
memberMustafa Alahwel19-Feb-09 4:03 
GeneralRe: Unmanaged to Managed, how to pass data by reference Pin
Doug071620-Mar-09 9:30
memberDoug071620-Mar-09 9:30 
QuestionHow about performance? Pin
aiflame330-Jan-09 5:28
memberaiflame330-Jan-09 5:28 
QuestionHow to do the same thing with BOOL without parameter? Pin
Mohd Audy12-Jan-09 15:50
memberMohd Audy12-Jan-09 15:50 
GeneralCool! [modified] Pin
Rabbit6331-Oct-08 9:41
memberRabbit6331-Oct-08 9:41 
Generalcompilation error with this code Pin
Member 448196331-Oct-08 6:58
memberMember 448196331-Oct-08 6:58 
GeneralRe: compilation error with this code Pin
menakerman1-Dec-08 21:33
membermenakerman1-Dec-08 21:33 
QuestionHow to do the same thing but with byte[]? Pin
mringholm22-Sep-08 5:18
membermringholm22-Sep-08 5:18 
QuestionExample works but... [modified] Pin
apalomba15-Sep-08 10:43
memberapalomba15-Sep-08 10:43 
AnswerRe: Example works but... Pin
Chakrabarty Rajib5-Oct-08 4:04
memberChakrabarty Rajib5-Oct-08 4:04 
QuestionThanks and a remark - release pointers? Pin
tb200011-Mar-08 9:16
membertb200011-Mar-08 9:16 
AnswerRe: Thanks and a remark - release pointers? Pin
Chakrabarty Rajib5-Oct-08 4:08
memberChakrabarty Rajib5-Oct-08 4:08 
QuestionDifferent Directory Pin
CSpicer21-Feb-08 10:17
memberCSpicer21-Feb-08 10:17 
GeneralGood Work! Pin
admcse8-Jan-08 5:01
memberadmcse8-Jan-08 5:01 
QuestionWhy it doesn't work? [modified] Pin
caimodorro12-Feb-07 0:15
membercaimodorro12-Feb-07 0:15 
AnswerRe: Why it doesn't work? Pin
Hristo Konstantinov2-Mar-07 9:22
memberHristo Konstantinov2-Mar-07 9:22 
GeneralRe: Why it doesn't work? Pin
caimodorro4-Mar-07 19:49
membercaimodorro4-Mar-07 19:49 
GeneralUse unmenage C lib on C# Pin
Bandoleiro11-Dec-06 0:24
memberBandoleiro11-Dec-06 0:24 
GeneralDebuggin... Pin
theDarkBrianer30-Jul-06 5:58
membertheDarkBrianer30-Jul-06 5:58 
GeneralSay thanks Pin
AlexEvans22-Jan-06 18:17
memberAlexEvans22-Jan-06 18:17 
GeneralUnmanged to managed calls Pin
Severian@Severian.org19-Jan-06 20:38
memberSeverian@Severian.org19-Jan-06 20:38 
GeneralRe: Unmanged to managed calls Pin
Claus Brod22-Jan-06 6:03
memberClaus Brod22-Jan-06 6:03 
GeneralRe: Unmanged to managed calls Pin
Antony M Kancidrowski13-Jun-06 6:38
memberAntony M Kancidrowski13-Jun-06 6:38 
Generalperformance? and what about managed C++ Pin
nuno8918-Jan-06 16:55
membernuno8918-Jan-06 16:55 
GeneralRe: performance? and what about managed C++ Pin
ischen_s118-Jan-06 20:53
memberischen_s118-Jan-06 20:53 
GeneralRe: performance? and what about managed C++ Pin
Andromeda Shun18-Jan-06 20:53
memberAndromeda Shun18-Jan-06 20:53 
GeneralExtending this to Visual Basic 6 Pin
goldsun18-Jan-06 13:48
membergoldsun18-Jan-06 13:48 
GeneralThis is nuts Pin
Andrei Dumitrache17-Jan-06 22:19
memberAndrei Dumitrache17-Jan-06 22:19 
GeneralRe: This is nuts Pin
Dasbose19-Jan-06 4:17
memberDasbose19-Jan-06 4:17 
GeneralRe: This is nuts Pin
Nagy Pityu19-Jan-06 6:26
memberNagy Pityu19-Jan-06 6:26 
GeneralRe: This is nuts Pin
Andrei Dumitrache19-Jan-06 22:34
memberAndrei Dumitrache19-Jan-06 22:34 
QuestionMind boggling! More on pointers? Pin
Koushik Biswas11-Jan-06 15:52
memberKoushik Biswas11-Jan-06 15:52 
AnswerRe: Mind boggling! More on pointers? Pin
Chakrabarty Rajib13-Jan-06 2:47
memberChakrabarty Rajib13-Jan-06 2:47 
GeneralInteresting Pin
Stephen Hewitt10-Jan-06 15:56
memberStephen Hewitt10-Jan-06 15:56 
GeneralRe: Interesting Pin
Chakrabarty Rajib10-Jan-06 19:17
memberChakrabarty Rajib10-Jan-06 19:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web02 | 2.8.160518.1 | Last Updated 10 Jan 2006
Article Copyright 2006 by Chakrabarty Rajib
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid