从C#.NET调用非托管DLL所需的教程 [英] Tutorial needed on invoking unmanaged DLL from C#.NET

查看:424
本文介绍了从C#.NET调用非托管DLL所需的教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个供应商提供的DLL,需要从C#调用.我知道C#数据类与C ++数据类型不直接兼容.

I have a DLL from a vendor that I Need to invoke from C#. I know that C# data classes are not directly compatible with C++ data types.

因此,假设我有一个接收数据并返回字符串"的函数.

So, given that I have a function that receives data and returns a "string".

(像这样)

string answer = CreateCode2(int, string1, uint32, string2, uint16);

我该怎么做才能使输入参数兼容,然后使结果字符串兼容?

What must I do to make the input parameters compatible, and then make the result string compatible?

请-我从未做过:不要给出诸如使用P/调用"或使用元帅"之类的答案,我需要一个带有示例的教程.

Please - I have never done this: Don't give answers like "Use P/Invoke" or "Use Marshal" I need a tutorial with examples.

我看到的所有P/Invoke示例都来自.NET Framework 1.1,而Marshall(没有教程)使我完全困惑.

All the P/Invoke examples I have seen are from .NET Framework 1.1, and Marshall (without a tutorial) is totally confusing me.

此外,我还看到了一些示例,这些示例告诉我创建extern函数以将所有数据类型替换为void *时的情况.这是我的IDE要求我使用不安全的".

Also, I have seen some examples that tell me when I create my extern function to replace all the datatypes with void*. This is makes my IDE demand that I use "unsafe".

推荐答案

这不是一个教程,但是它提供了很多有关使用P/Invoke的很好的信息. 在其中调用Win32 DLL具有P/调用功能的C#
它会带给您有关术语,基本概念,如何使用DllImport的概念,并且应该足以使您前进.

This isn't quite a tutorial but it's got a lot of good information on using P/Invoke Calling Win32 DLLs in C# with P/Invoke
It'll give you an idea of the terminology, the basic concepts, how to use DllImport and should be enough to get you going.

在MSDN上有一个教程:

There's a tutorial on MSDN: Platform Invoke Tutorial.
But it's pretty short and to be honest the one I've mentioned above is a much better source of information, but there's a lot of it on there.

PInvoke签名工具包也很有用,如此处下载.
它使您可以粘贴非托管方法签名或结构定义,并为您提供.NET P/Invoke等效项.它不是100%完美的,但是它比尝试自己解决所有问题要快得多.

Also useful is the PInvoke Signature Toolkit, described here .
And downloadable here.
It lets you paste in an unmanaged method signature, or struct definition and it'll give you the .NET P/Invoke equivalent. It's not 100% perfect but it gets you going much quicker than trying to figure everything out yourself.

关于编组,我要说的很简单. 如果您拥有某种指针,而不是尝试使用Marshal在方法签名中将其直接转换为某种.NET类型,则有时将其视为IntPtr然后使用Marshal.Copy,会更容易. PtrToString,.PtrToStructure和其他类似方法可将数据转换为.NET类型.
然后,当您掌握了所有内容后,就可以使用Marshal属性直接进行转化了.

With regards to Marshalling specifically, I would say start simple. If you've got something that's some sort of pointer, rather than trying to convert it directly to some .NET type in the method signature using Marshal it can sometimes be easier to just treat it as an IntPtr and then use Marshal.Copy, .PtrToString, .PtrToStructure and the other similar methods to get the data into a .NET type.
Then when you've gotten to grips with the whole thing you can move on to direct conversions using the Marshal attribute.

关于编组的文章分为三部分,

There's a good 3 part set of articles on marshalling here, here and here.

这篇关于从C#.NET调用非托管DLL所需的教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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