我是 C++/CLI 的新手,在运行我的应用程序时遇到了一些问题。我有一个场景,非托管代码需要调用托管代码。我为此目的使用 GCHandle。这就是我的 CLI 类的样子
#pragma once
#include <msclr\gcroot.h>
#include "UnmanagedClass1.h"
using namespace System;
namespace Wrapper {
public ref class WrapperClass
{
private:
UnmanagedClass::UnmanagedClass1* UnmanagedClass1obj;
GCHandle delegateHandle_;
public:
WrapperClass(void);
delegate void EventDelegate(char *);
EventDelegate^ nativeCallback_;
void callback(char *msg);
};
}
和cpp文件
using namespace Wrapper;
WrapperClass::WrapperClass(void)
{
UnmanagedClass1obj = new UnmanagedClass::UnmanagedClass1 ();
nativeCallback_ = gcnew EventDelegate(this, &WrapperClass::callback);
// As long as this handle is alive, the GC will not move or collect the delegate
// This is important, because moving or collecting invalidate the pointer
// that is passed to the native function below
delegateHandle_ = GCHandle::Alloc(nativeCallback_);
// This line will actually get the pointer that can be passed to
// native code
IntPtr ptr = Marshal::GetFunctionPointerForDelegate(nativeCallback_);
// Convert the pointer to the type required by the native code
UnmanagedClass1obj ->RegisterCallback( static_cast<EventCallback>(ptr.ToPointer()) );
}
void WrapperClass::callback(char *msg)
{
//TDO
}
我收到以下错误
error C2146: syntax error : missing ';' before identifier 'delegateHandle_'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C4430: missing type specifier - int assumed. Note: C++ does not support
error C2065: 'delegateHandle_' : undeclared identifier
error C2653: 'GCHandle' : is not a class or namespace name
error C3861: 'Alloc': identifier not found
error C2653: 'Marshal' : is not a class or namespace name
error C3861: 'GetFunctionPointerForDelegate': identifier not found
前 3 个错误在 .h 文件中,其余在 cpp 文件中 我错过了一些库吗?
我还有一些关于实施的问题:
项目输出将是一个 dll。然后我如何使用它来实现与 c# 代码的回调。我的意思是我是否需要将 c# 类对象作为参考(如何?)或其他方式传递?
我正在使用 char 指针传回 C#。有更好的数据类型吗?例如 BSTR?谁来释放内存 C#、CLI、C++?