如何将托管C++(C++/CLI)的函数指针传递给非托管方法?我读了几篇文章,如this one from MSDN,但它描述了两个不同的程序集,而我只需要一个。C++/cli将托管代码传递给非托管代码
这是我的代码:
1)报头(MyInterop.ManagedCppLib.h):
#pragma once
using namespace System;
namespace MyInterop { namespace ManagedCppLib {
public ref class MyManagedClass
{
public:
void DoSomething();
};
}}
2)CPP代码(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??? */);
}
我尝试创建我的托管代理,然后尝试使用Marshal::GetFunctionPointerForDelegate
方法,但我无法编译。
请问您可以发布代码,其中您使用GetFunctionPointerForDelegate? – Simon 2010-06-04 08:21:40