I've got a native C++ DLL that I would like to have a C++/CLI wrapper layer for. From what I understood, if you simple added a C++/CLI class to the project, VS would compile as mixed mode, but I was apparently wrong as VS doesn't seem to be even touching the managed code.
So, given a pre-existing native code-base what exactly, step-by-step, do you need to do to create a mixed mode DLL, so that I can can link into that code from any .NET language?
*I need to do this because my native code uses C++ classes that I cannot P/Invoke into.
Well, no, it doesn't get to be mix-mode until you tell the C++/CLI compiler that your legacy DLL was written in unmanaged code. Which should have been noticeable, you should have gotten linker errors from the unmanaged DLL exports. You need to use #pragma managed:
#pragma managed(push, off)
#include "oldskool.h"
#pragma comment(lib, "oldskool.lib")
#pragma managed(pop)
using namespace System;
public ref class Wrapper {
private:
COldSkool* pUnmanaged;
public:
Wrapper() { pUnmanaged = new COldSkool; }
~Wrapper() { delete pUnmanaged; pUnmanaged = 0; }
!Wrapper() { delete pUnmanaged; }
void sampleMethod() {
if (!pUnmanaged) throw gcnew ObjectDisposedException("Wrapper");
pUnmanaged->sampleMethod();
}
};