Click here to Skip to main content
15,906,569 members
Articles / Programming Languages / C#

Calling C# .NET methods from unmanaged C/C++ code

Rate me:
Please Sign up or sign in to vote.
4.83/5 (32 votes)
13 Dec 2013CPOL1 min read 280.5K   12K   46   54
Describes with an example of how you can call C#.NET methods from unmanaged C++ code.


For a number of reasons which I won't get into, I needed to use a C# .NET DLL from unmanaged code. I spent a long time looking for the answer before figuring it out. So I thought I'd post the example here to spread the word.

The way it works is fairly straightforward and requires three components:

  1. C# DLL doing whatever.
  2. Managed C++ DLL with exposed C function. This DLL will call your C# DLL methods. 
  3. Unmanaged C++ application/DLL which will call the exposed C function in the Managed C++ DLL.

Using the code

Create your C# DLL. The below example just shows a simple MessageBox and sets the result of the value based on OK or CANCEL.

// ManagedClass.cs

// Any source code blocks look like this

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ManagedCSharp
    public static class ManagedClass
        public static void ShowValue(ref int value)
            DialogResult result = MessageBox.Show("C# Message Box", 
                    "C# Message Box", MessageBoxButtons.OKCancel);

            if (result == DialogResult.OK)
                value = 1;
                value = 2;

Create a Managed C++ DLL and reference it in your C# project.

This exports your function ShowMessageBox in an unmanaged format.

Inside the exported function, call the Managed C++ method which calls your C# methods.

// ManagedDll.h

#pragma once

using namespace System;
using namespace System::Reflection;

namespace ManagedDll {    

    public ref class DoWork
        public:void ShowCSharpMessageBox(int *value)

__declspec(dllexport) void ShowMessageBox(int *value)
    ManagedDll::DoWork work;    

Create your unmanaged C or C++ DLL or EXE and call the exposed C++ method in your managed code.

In your unmanaged project setting, you will need to reference the ManagedDll.lib file created by the ManagedDll project and build time.

// TestApp.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "conio.h"
#include <iostream>
#include <windows.h>

_declspec(dllexport) void ShowMessageBox(int *value);

int _tmain()
    int *result;


    if(*result == 1)
        printf("Ok Was Pressed \n");
        if(*result == 2)
            printf("Cancel Was Pressed \n");
            printf("Unknown result \n");


    return 0;

Find the attached full project which should build straight away. Built in Visual Studio 2008.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralRe: Can't get this to work, please help Pin
Stanley Wu20-Nov-14 8:14
Stanley Wu20-Nov-14 8:14 
AnswerRe: Can't get this to work, please help Pin
Member 1209768521-Dec-16 4:35
Member 1209768521-Dec-16 4:35 
QuestionGeneric and detailed solution Pin
Pragmateek10-Jan-14 5:06
professionalPragmateek10-Jan-14 5:06 
QuestionMy vote of 5 Pin
mlnlover1113-Dec-13 7:35
mlnlover1113-Dec-13 7:35 

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.