简体   繁体   English

如何从C ++ dll导入结构?

[英]How do I Import a struct from a C++ dll?

How can I create a struct in C++ put it in a dll and use from C# code? 如何在C++创建struct并将其放入dll并从C#代码中使用? I'm developing an application using C# and C++ where each process talk to each one other using named-pipes and I'd like to share data between via structs (pass raw bytes to process and then cast it to struct type) but rather than define two structs one in C++ and another in C# with same memory alignment, members names and such (which is very error prone, if I update the C++ one and forget the C#'s one) I'd like to creare only one so that there's only one place to change. 我正在使用C#和C ++开发一个应用程序,其中每个进程都使用命名管道相互通信,我想在通过结构之间共享数据(将原始字节传递给进程,然后将其转换为结构类型),而不是定义两个结构,一个在C ++中,另一个在C#中,具有相同的内存对齐方式,成员名称等(这很容易出错,如果我更新C ++却忘记了C#的那个)我只想创建一个,所以只有一个一个改变的地方。 My idea is (if even possible, I think it isn't not possible due P/invoke limitations) define this struct in a dll written in C++ and just use it from my C# application. 我的想法是(即使可能,由于P / invoke的限制我也不可能)在用C ++编写的dll中定义此结构,并从我的C#应用​​程序中使用它。 This is just my idea; 这只是我的主意; any different approach to solve this is very welcome too. 任何其他解决此问题的方法也非常受欢迎。

You can't. 你不能 Structs and classes are not part of the exposed ABI, the DLL doesn't define them in any way. 结构和类不是公开的ABI的一部分,DLL没有以任何方式定义它们。 That's part of the reason for DLL hell in the C/C++ world, your function definitions state the name of the struct they take, but the details of the implementation of that struct aren't actually exposed or defined anywhere in the DLL itself. 这是C / C ++世界中DLL陷入地狱的部分原因,您的函数定义说明了它们采用的结构的名称,但是该结构的实现细节实际上并未在DLL本身的任何地方公开或定义。

C and C++ rely on the use of header files to inform dependent projects of the layout of structs and classes, as that information isn't exposed via the DLL or lib file. C和C ++依靠头文件的使用将结构和类的布局通知相关项目,因为该信息不会通过DLL或lib文件公开。 Since it's not possible in native code, it's going to be doubly not possible (or, pedantically, just as impossible) in managed code. 由于在本机代码中是不可能的,因此在托管代码中将是不可能的(或者,就学上来说,也是不可能的)。

Some alternatives I'd recommend looking into if über performance is not a constraint would be some sort of serialization library. 我建议研究über性能是否受到限制的一些替代方法是某种序列化库。 You can get some crazy things done with something like Google's protobuf, eliminating p/invoke and compatibility concerns, etc. if you want to focus on rapid development and consistency. 如果您想专注于快速开发和一致性,则可以使用Google的protobuf来完成一些疯狂的工作,消除p / invoke和兼容性问题,等等。 There are also ways to generate C# and C/C++ source code with the relevant structures via a script ranging from a hack-it-yourself C/Python/Perl script to generate a struct for C and C# to complete projects that focus on creating source code from language-agnostic struct definitions, but that's probably outside the scope of this answer. 还有一些方法可以通过脚本生成具有相关结构的C#和C / C ++源代码,包括自行破解C / Python / Perl脚本以生成C和C#结构以完成专注于创建源代码的项目。来自与语言无关的结构定义的代码,但这可能超出了此答案的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM