Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

How to: Pass a Delegate^ to a Native Function Expecting a Function Pointer

From a managed component you can call a native function with function pointer parameters where the native function then can call the member function of the managed component's delegate.


This sample created the .dll that exports the native function.

// delegate_to_native_function.cpp
// compile with: /LD
#include < windows.h >
extern "C" {
   void nativeFunction(void (CALLBACK *mgdFunc)(const char* str)) {
      mgdFunc("Call to Managed Function");

This sample consumes the .dll and passes a delegate handle to the native function expecting a function pointer.

// delegate_to_native_function_2.cpp
// compile with: /clr
using namespace System;
using namespace System::Runtime::InteropServices;

delegate void Del(String ^s);
public ref class A {
   void delMember(String ^s) {

[DllImportAttribute("delegate_to_native_function", CharSet=CharSet::Ansi)]
extern "C" void nativeFunction(Del ^d);

int main() {
   A ^a = gcnew A;
   Del ^d = gcnew Del(a, &A::delMember);
   nativeFunction(d);   // Call to native function


Call to Managed Function

See Also



Community Additions

© 2015 Microsoft