开发者

Export c++ functions inside a C# Application

开发者 https://www.devze.com 2022-12-17 04:50 出处:网络
Greetings, I am sorry for bothering, I\'ll show the question: I am trying to export some functions written in c++ in a DLL in order to import them in a C# Application running on Visual Studio.

Greetings, I am sorry for bothering, I'll show the question:

I am trying to export some functions written in c++ in a DLL in order to import them in a C# Application running on Visual Studio. I make the export as reported in the following code,

tobeexported.h:

namespace SOMENAMESPACE
{
                class __declspec(dllexport) SOMECLASS
                {
                               public: 
                               SOMETYPE func(param A,char b[tot]);

                };
}

tobeexported.cpp:

#include "stdafx.h"
#include "tobeexported.h"
...


using namespace SOMENAMESPACE;

SOMETYPE SOMECLASS:: func(param A,char b[tot])
                {
                               ...some stuff inside...
                }

The dll is righly created and the code is already CLR-managed(looked with a disassembling software(reflector)) and contains the exported functions then I "Add the Reference" 开发者_如何学Cin my c# application and the dll is found, but when I open it with the object browser it is completely empty, neither class, nor object has been exported and ready to be used

can you help me please? thanks best regards


What about using managed C++ to compile your DLL? Then you just have to add a ref to the class like this:

namespace SOMENAMESPACE
{
                public ref class SOMECLASS
                {
                               public: 
                               SOMETYPE func(param A,char b[tot]);

                };
}

After successful compilation and referencing in the other project, the class should be visible. Exporting native C++ is not really portable, each compiler produces different results and is tedious to bind from within C#...

EDIT: added public access modifier to ref class...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号