By Ron Klein


2010-06-04 08:13:49 8 Comments

How do I pass a function pointer from managed C++ (C++/CLI) to an unmanaged method? I read a few articles, like this one from MSDN, but it describes two different assemblies, while I want only one.

Here is my code:

1) Header (MyInterop.ManagedCppLib.h):

#pragma once

using namespace System;

namespace MyInterop { namespace ManagedCppLib {

    public ref class MyManagedClass
    {
    public:
        void DoSomething();
    };
}}

2) CPP Code (MyInterop.ManagedCppLib.cpp)

#include "stdafx.h"
#include "MyInterop.ManagedCppLib.h"

#pragma unmanaged
void UnmanagedMethod(int a, int b, void (*sum)(const int))
{
    int result = a + b;
    sum(result);
}

#pragma managed
void MyInterop::ManagedCppLib::MyManagedClass::DoSomething()
{
    System::Console::WriteLine("hello from managed C++");
    UnmanagedMethod(3, 7, /* ANY IDEA??? */);
}

I tried creating my managed delegate and then I tried to use Marshal::GetFunctionPointerForDelegate method, but I couldn't compile.

2 comments

@Graham Asher 2019-09-27 12:48:12

Here's another way to do it based on my experiences implementing a .NET wrapper in C++/CLI around the CartoType C++ map rendering library. This is tested and working code.

The C++ API has an asynchronous Find function which takes a callback:

TResult CartoType::CFramework::FindAsync(FindAsyncCallBack aCallBack,const TFindParam& aFindParam,bool aOverride = false);

The callback is a function of this type:

using FindAsyncCallBack = std::function<void(std::unique_ptr<CMapObjectArray> aMapObjectArray)>;

The task is to provide a .NET wrapper for this function by adding C++/CLI code to the existing wrapper system. First I define a suitable delegate type for my .NET function (an equivalent to FindAsyncCallback in the C++ API):

public delegate void FindAsyncDelegate(MapObjectList^ aMapObjectList);

The signature of the .NET function is thus:

Result FindAsync(FindAsyncDelegate^ aDelegate,FindParam^ aFindParam,bool aOverride);

The main implementation problem to be solved is how to call the native C++ function and provide a native callback function which can then call the delegate passed in by the caller of the .NET function. An associated task is to keep the delegate and the native callback function object alive until the asynchronous function's thread has done its job. Here's how it's done.

I define a C++/CLI delegate type that's the same as the C++ callback function type, and a class to hold the delegate passed in by the caller to the .NET function (of type FindAsyncDelegate), and a delegate of the type to be passed to C++ (of type NativeAsyncHandler):

delegate void NativeAsyncHandler(std::unique_ptr<CMapObjectArray> aMapObjectArray);

ref class FindAsyncHelper
    {
    public:
    FindAsyncHelper(Framework^ aFramework,FindAsyncDelegate^ aDelegate):
        m_framework(aFramework),
        m_delegate(aDelegate)
        {
        }

    void Handler(std::unique_ptr<CMapObjectArray> aMapObjectArray)
        {
        MapObjectList^ o = gcnew MapObjectList;
        SetMapObjectList(m_framework,o,*aMapObjectArray);
        m_delegate(o);

        // Remove this object from the list held by the framework so that it can be deleted.
        m_framework->m_find_async_helper_list->Remove(this);
        }

    Framework^ m_framework;
    FindAsyncDelegate^ m_delegate;
    NativeAsyncHandler^ m_native_handler;
    };

The idea is that we create a FindAsyncHelper object with the two delegates in it, then call the native FindAsync function with the native delegate, arranged to call Handler(), which then calls the original caller's delegate.

And here is how it's implemented:

typedef void(*FIND_ASYNC_CALLBACK)(std::unique_ptr<CMapObjectArray> aMapObjectArray);

Result Framework::FindAsync(FindAsyncDelegate^ aDelegate,FindParam^ aFindParam,bool aOverride)
    {
    if (aDelegate == nullptr || aFindParam == nullptr)
        return Result::ErrorInvalidArgument;
    TFindParam param;
    SetFindParam(param,aFindParam);

    FindAsyncHelper^ h = gcnew FindAsyncHelper(this,aDelegate);
    h->m_native_handler = gcnew NativeAsyncHandler(h,&FindAsyncHelper::Handler);

    IntPtr p = Marshal::GetFunctionPointerForDelegate(h->m_native_handler);
    FIND_ASYNC_CALLBACK f = static_cast<FIND_ASYNC_CALLBACK>(p.ToPointer());
    TResult error = m_framework->FindAsync(f,param,aOverride);

    // Keep h alive by adding it to a list.
    m_find_async_helper_list->Add(h);

    return (Result)(int)error;
    }

Some notes:

The statements

FindAsyncHelper^ h = gcnew FindAsyncHelper(this,aDelegate);
h->m_native_handler = gcnew NativeAsyncHandler(h,&FindAsyncHelper::Handler);

create a FindAsyncHandler object and store a native handler object in it; keeping it here means we only have one object to keep alive, the FindAsyncHandler. The next statements:

IntPtr p = Marshal::GetFunctionPointerForDelegate(h->m_native_handler);
FIND_ASYNC_CALLBACK f = static_cast<FIND_ASYNC_CALLBACK>(p.ToPointer());

get a function pointer that can be passed to native code, and cast it to the right function pointer type. We can't cast it directly to the std::function type used in FindAsyncCallback, so the cumbersome extra typedef is necessary.

At last the native FindAsync function can be called:

TResult error = m_framework->FindAsync(f,param,aOverride);

And then, to make sure the various callback functions stay alive, the FindAsyncHandler is added to a list owned by the main framework object:

m_find_async_helper_list->Add(h);

It is taken off the list when the task is completed and FindAsyncHelper::Handler is called.

@Hans Passant 2010-06-04 10:29:26

Yes, you want Marshal::GetFunctionPointerForDelegate(). Your code snippet is missing the managed function you'd want to call, I just made one up. You will also have to declare the managed delegate type and create an instance of it before you can get a function pointer. This worked well:

#include "stdafx.h"

using namespace System;
using namespace System::Runtime::InteropServices;

#pragma managed(push, off)
typedef void (* UnmanagedSummer)(int arg);

void UnmanagedMethod(int a, int b, UnmanagedSummer sum)
{
    int result = a + b;
    sum(result);
}
#pragma managed(pop)

ref class Test {
    delegate void ManagedSummer(int arg);
public:
    static void Run() {
        Test^ t = gcnew Test();
        ManagedSummer^ managed = gcnew ManagedSummer(t, &Sum);
        IntPtr stubPointer = Marshal::GetFunctionPointerForDelegate(managed);
        UnmanagedSummer functionPointer = static_cast<UnmanagedSummer>(stubPointer.ToPointer());
        UnmanagedMethod(1, 2, functionPointer);
        GC::KeepAlive(managed);    // Important: ensure stub can't be collected while native code is running
        System::Diagnostics::Debug::Assert(t->summed == 3);
    }
    void Sum(int arg) {
        summed += arg;
    }
    int summed;
};

int main(array<System::String ^> ^args)
{
    Test::Run();
    return 0;
}

@Ron Klein 2010-06-06 15:39:33

This extra casting was the "missing link" :-) Thanks!

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Wrapping managed code for unmanaged use

2 Answered Questions

[SOLVED] Is #pragma managed(push,off) in C++/CLI affects performance?

  • 2013-03-20 08:42:14
  • maverix3
  • 1363 View
  • 3 Score
  • 2 Answer
  • Tags:   interop c++-cli

0 Answered Questions

How to return a managed Boolean from a C++/CLI assembly

  • 2016-01-07 10:44:39
  • Graham Bull
  • 579 View
  • 0 Score
  • 0 Answer
  • Tags:   c++-cli

1 Answered Questions

1 Answered Questions

[SOLVED] C++/CLI wrapper memleak when calling managed lib from unmanaged

  • 2014-07-11 18:07:21
  • lsalamon
  • 467 View
  • -1 Score
  • 1 Answer
  • Tags:   c# c++-cli interop

0 Answered Questions

managed code calling unmanaged function

  • 2013-03-13 03:38:28
  • JoJo AndChickie
  • 172 View
  • 0 Score
  • 0 Answer
  • Tags:   c++-cli

2 Answered Questions

[SOLVED] Garbage collected delegate in C++/CLI

5 Answered Questions

[SOLVED] Copy unmanaged data into managed array

  • 2011-06-19 14:34:19
  • JeffRSon
  • 30909 View
  • 18 Score
  • 5 Answer
  • Tags:   c++-cli

2 Answered Questions

1 Answered Questions

[SOLVED] Cannot use managed event/objects in unmanaged code error c3265, c2811

  • 2010-10-26 16:55:35
  • anivas
  • 6400 View
  • 4 Score
  • 1 Answer
  • Tags:   c++-cli

Sponsored Content