Take the 2-minute tour ×

in my next project I want to implement a GUI for already existing code in C++. My plan is to wrap the C++ part in a DLL and to implement the GUI in C#. My problem is that I don't know how to implement a callback from the unmanaged DLL into the manged C# code. I've already done some development in C# but the interfacing between managed and unmanaged code is new to me. Can anybody give me some hints or reading tips or a simple example to start from? Unfortunatly I could not find anything helpful.

share|improve this question

4 Answers 4

up vote 23 down vote accepted

You don't need to use Marshal.GetFunctionPointerForDelegate(), the P/Invoke marshaller does it automatically. You'll need to declare a delegate on the C# side whose signature is compatible with the function pointer declaration on the C++ side. For example:

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

class UnManagedInterop {
  private delegate int Callback(string text);
  private Callback mInstance;   // Ensure it doesn't get garbage collected

  public UnManagedInterop() {
    mInstance = new Callback(Handler);
  public void Test() {

  private int Handler(string text) {
    // Do something...
    return 42;
  private static extern void SetCallback(Callback fn);
  private static extern void TestCallback();

And the corresponding C++ code used to create the unmanaged DLL:

#include "stdafx.h"

typedef int (__stdcall * Callback)(const char* text);

Callback Handler = 0;

extern "C" __declspec(dllexport)
void __stdcall SetCallback(Callback handler) {
  Handler = handler;

extern "C" __declspec(dllexport)
void __stdcall TestCallback() {
  int retval = Handler("hello world");

That's enough to get you started with it. There are a million details that can get you into trouble, you are bound to run into some of them. The much more productive way to get this kind of code going is writing a wrapper in the C++/CLI language. That also lets you wrap a C++ class, something you can't do with P/Invoke. A decent tutorial is available here.

share|improve this answer
Thanks nobugz, I'll try that soon. And I hope i'll don't get into too much trouble ;-) –  chrmue Jan 30 '10 at 18:04
works perfectly for me, thanks again! –  chrmue Feb 15 '10 at 13:39

P/Invoke can handle marshaling a managed delegate to a function pointer. So if you expose API's that register a call back function from your DLL and in C# pass a delegate to that function.

There is an example on MSDN of doing this with the EnumWindows function. In that article be careful to pay attention to the line in point 4 that states:

If, however, the callback function can be invoked after the call returns, the managed caller must take steps to ensure that the delegate remains uncollected until the callback function finishes. For detailed information about preventing garbage collection, see Interop Marshaling with Platform Invoke.

What that is saying is that you need to make sure that your delegate isn't garbage collected until after the managed code is done calling it by either keeping a reference to it in your code, or pinning it.

share|improve this answer
+1 for critical - using P/Invoke alone only guarantees the validity of the pointer during the call - if you're caching it, you need to pin it (can't speak to stashing a reference, am suspicious, but that doesn't mean much :-) –  Mark Mullin Jan 8 '13 at 21:28

See Marshal.GetFunctionPointerForDelegate, which will give you a function pointer for calling managed (i.e. C# code) from unmanaged code.

share|improve this answer

Have a look at this, Marshal.GetDelegateForFunctionPointer?

share|improve this answer

Your Answer


