Search code examples
c++-climixed-mode

Create a mixed mode C++/CLI DLL



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?


Solution

  • 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.