从C DLL到.NET接口 [英] Interface from a C DLL to .NET
问题描述
我有一个旧的DLL用C写的,我想从C#.NET应用程序来调用。问题是,该DLL接口对于C DLL是相当复杂的。这件事情是这样的:
I have a legacy DLL written in C that I'd like to call from a C# .NET application. The problem is that the DLL interface for the C DLL is fairly complicated. It's something like this:
__declspec(dllexport) void __stdcall ProcessChunk(
void *p_prochdl,
const BIG_INPUT_STRC *p_inparams,
BIG_OUTPUT_STRC *p_outparams
);
在BIG_INPUT_STRC / BIG_OUTPUT_STRC包含各种东西......指针缓存阵列,列举的参数,整型参数,等等,总之,他们是复杂的。
The BIG_INPUT_STRC/BIG_OUTPUT_STRC contain all kinds of things... pointers to buffer arrays, enumerated parameters, integer parameters, etc. In short, they're complicated.
第一个问题:有没有一种简单的方法来获得所有这一切都包含在DLL头文件到C#类的结构信息,或者你需要逐字复制和粘贴一切,C#和重新定义它?这似乎是多余的。
First question: Is there an easy way to get all of the structure information that is contained in the DLL header file into the C# class, or do you need to literally copy and paste everything to C# and re-define it? This seems redundant.
与此相关的,什么是从C#传递结构进入非托管的DLL的正确方法?
Related to that, what is the correct way to pass structs into the unmanaged DLL from C#?
最后,有一个如何正确地传递从C#缓冲区数组到非托管的DLL的例子吗?另外,我怎么能传递一个二维数组到DLL?
Finally, is there an example of how to correctly pass buffer arrays from C# into the unmanaged DLL? Alternatively, how can I pass a two-dimensional array into the DLL?
谢谢, -Greg
推荐答案
它非常直截了当地,使用P做到这一点柔软的东西在C#/调用。
Its quite straight forward to do this soft of thing in C# using P/Invoke.
我相信,你将不得不手动定义数据结构在C#。
I belive you are going to have to define the data structures in C# manually.
我会建议你看看这 MSDN文章上主题
I would reccomend you take a look at this MSDN article on the subject
这篇关于从C DLL到.NET接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!