Create a mixed mode C++/CLI DLL

大城市里の小女人 提交于 2019-12-13 06:27:27

问题



I hope not to repeat something already asked..I search around but I haven't found something similar.
I have developed a native sdk which expose some classes and interfaces.
Now, I need to implement a mixed mode DLL which uses this SDK.
But the following code does not compile:

WrapperClass.h

#pragma once

#include <vcclr.h>

#using <mscorlib.dll>

class WrapperClass{
public:
  WrapperClass();

private:
  gcroot<Client^> m_ManagedObj;
};

NativeClass.h

#pragma once

#include "stdafx.h"
#include "NativeSDK.h"

#include "WrapperClass.h"

class Native : public INativeSDK {
public:
  // ... code ...

private:
  WrapperClass ManagedObj;
}

The settings are:

Project Setting   : No Support for CLR
NativeClass.cpp   : No Support for CLR
WrapperClass.cpp  : /clr

The compiler error is:

..\include\vcclr.h(16): fatal error C1190: The managed code require an option '/clr'

Because NativeClass is not compiled with /clr.
I suppose I need to use #pragma mananaged/unmanaged directive but I cannot figure out how.
Could someone give me some suggestions?


回答1:


You many need to add another layer of indirection, so your unmanaged source files don't see the "contents" of the WrapperClass class.

//WrapperClassWrapper.h
class WrapperClass;

class WrapperClassWrapper
{
public:
    WrapperClassWrapper();
    ~WrapperClassWrapper();
    //etc.
private:
    WrapperClass *m_pWrapper;
}

And then you implement it in a WrapperClassWrapper.cpp which you compile with /clr.



来源:https://stackoverflow.com/questions/16816159/create-a-mixed-mode-c-cli-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!