What's the best (simple without adding a lot of overhead) share unmanaged scructures between managed libraries?
Lets say I have a managed class library as follows:
// MyClassLib.h
#pragma once
#include "MyLegacyStruct.h"
using namespace System;
namespace MyClassLib {
public ref class Class1
{
protected:
MyLegacyStruct* m_internalBuffer;
public:
Class1() { }
~Class1() { }
MyLegacyStruct* GetBuffer()
{
return m_internalBuffer;
}
};
}
...the struct definition (in the library):
// MyLegacyStruct.h
#pragma once
namespace MyClassLib {
typedef struct MyLegacyStruct
{
unsigned int m_someVar;
} MyLegacyStruct;
}
...and a simple console app which wants to use that library:
// ConsoleApp.cpp : main project file.
#include "stdafx.h"
#include "MyLegacyStruct.h"
using namespace System;
using namespace MyClassLib;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Class1^ c1 = gcnew Class1();
MyLegacyStruct* s1 = c1->GetBuffer(); // <-- This is a problem
return 0;
}
...which makes the compiler whine:
2>.\ConsoleApp.cpp(14) : error C3767: 'MyClassLib::Class1::GetBuffer': candidate function(s) not accessible
The internal buffer is used in some fairly heavy duty processing algorithms which are then glued together with .net code to make it all nice and modular and to hook into a C# gui as well as some command line re-processing tools.
What's the right way to do this? Return a void *
? Make a .net ref struct, copy all the data between modules using that and then convert back?