从C DLL到.NET的接口 [英] Interface from a C DLL to .NET

查看:112
本文介绍了从C DLL到.NET的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C编写的遗留DLL,我想从C#.NET应用程序调用。问题是C DLL的DLL接口相当复杂。这是这样的:

  __ 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包含各种事物...缓冲区数组指针,枚举参数,整数参数等等。总之,它们很复杂。



第一个问题:有一个简单的方法来获取DLL头文件中包含的所有结构信息进入C#类,还是需要将所有内容复制并粘贴到C#并重新定义?这似乎是多余的。



相关的是,从C#将结构传递到非托管DLL的正确方法是什么?



最后,是否有一个例子说明如何将缓冲区数组从C#正确传递到非托管DLL?或者,如何将二维数组传递到DLL?



感谢
-Greg

解决方案

它很直接地用C#中的P / Invoke来做这个软件。



我相信你会去必须手动定义C#中的数据结构。



我会推荐你​​看看这个 MSDN文章上的主题


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
);

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.

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.

Related to that, what is the correct way to pass structs into the unmanaged DLL from C#?

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?

Thanks, -Greg

解决方案

Its quite straight forward to do this soft of thing in C# using P/Invoke.

I belive you are going to have to define the data structures in C# manually.

I would reccomend you take a look at this MSDN article on the subject

这篇关于从C DLL到.NET的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆