0

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?

Jon Cage
  • 36,366
  • 38
  • 137
  • 215

1 Answers1

2

This has already been answered on stack overflow previously on C++ CLI error C3767: candidate function(s) not accessible by Ben Schwehn.

In summary:

Native types are private by default outside the assembly Native types now will not be visible outside the assembly by default. For more information on type visibility outside the assembly, see Type Visibility. This change was primarily driven by the needs of developers using other, case-insensitive languages, when referencing metadata authored in Visual C++. Source

If you use #pragma make_public(MyClassLib::MyLegacyStruct), to export the MyLegacyStruct native type in the assembly, then your project will compile.

Community
  • 1
  • 1
Alex
  • 669
  • 15
  • 37
  • You have to give proper attribution when you copy an answer like this. In addition to the link to the answer, you have to post a link to the profile of the guy that gave the answer. – Hans Passant Sep 12 '12 at 10:21
  • Hi Hans, I've added a link to the guys profile and added a reference to msdn for the quote...does that look about right? – Alex Sep 12 '12 at 11:20