1
我已经获得了需要在C#应用程序中使用的第三方C/C++库(.dll,.lib,.exp和.h)。C++/CLI:将C++类ptr传递给非托管方法
ThirdPartyLibrary.h包含...
class AClass {
public:
typedef enum {
green = 7,
blue = 16
} Color;
virtual int GetData()=0;
virtual int DoWork(Color, char *)=0;
};
void * Func1(int, AClass **aClass);
在我的C++/CLI的代码,我已经这样做了......
#include "ThirdPartyLibrary.h"
using namespace System;
using namespace System::Runtime::InteropServices;
namespace Wrapper {
public ref class MyBridgeClass
{
private:
AClass* pAClass;
public:
// C# code will call this method
void AMethod (int x)
{
int y = x+10;
Func1 (y, &(this->pAClass)); // <-- error!
}
}
}
我得到读取生成错误...
cannot convert parameter 2 from 'cli::interior_ptr<Type>' to 'AClass **'
with
[
Type=AClass *
]
Cannot convert a managed type to an unmanaged type
任何想法?也许我需要在我的C++/CLI中使用#pragma manage/unmanged标签?
我知道固定,但我没有意识到非托管字段仍然分配在托管堆上。我添加了一个'this-> unmanaged = new UnmanagedHolder()'给桥接类'ctor,'if(this-> unmanged)'删除this-> unmanged'给桥接类'终结器。 – Tony 2013-05-01 17:03:24