简体   繁体   English

为库 c# 创建一个包装器

[英]create a wrapper for libraries c#

Sometimes it will be great if I could use some other programs dll and customize them.有时,如果我可以使用其他一些程序 dll 并自定义它们,那就太好了。 For example there is a device called usb uirt where I can send ir signals from a computer.例如,有一个名为 usb uirt 的设备,我可以在其中从计算机发送 ir 信号。 The software that is used to send the signals sucks and the drivers from the device come with a lot of dll's.用于发送信号的软件很糟糕,设备的驱动程序带有很多 dll。 As a result, I tried importing them to visual studio but I could not create a reference.结果,我尝试将它们导入 Visual Studio,但无法创建参考。 That was probably because maybe they were written in a different language I think.那可能是因为我认为它们可能是用不同的语言编写的。 But someone managed to get a wrapper for those libraries and when I imported those dll it worked and I was able to control the device from c# which is great.但是有人设法获得了这些库的包装器,当我导入这些 dll 时,它起作用了,我能够从 c# 控制设备,这很棒。

Anyways I was just curious about this and I want to learn more about it... How can I tell when can I use those dll's?无论如何,我只是对此感到好奇,我想了解更多关于它的信息......我怎么知道我什么时候可以使用这些 dll? On what language they where created?他们在哪里创建的语言是什么? I have tried googling that and I find stuff like:我试过用谷歌搜索,我发现了类似的东西:

[DllImport("my.dll")]
static extern void methodA(UInt32[] data);

what do the [] mean? [] 是什么意思? Sometimes they use pointers... From my experience I have never had to use pointers.有时他们使用指针......根据我的经验,我从来没有使用过指针。 I can always pass a variable in c# by reference or by value plus with the help of delegates I don't find the need for pointers but maybe I am wrong... I am interested in learning about all this.我总是可以在 c# 中通过引用或值加上在代表的帮助下传递一个变量我不觉得需要指针,但也许我错了......我有兴趣了解这一切。 What phrase should I Google to learn about this?我应该谷歌什么短语来了解这个?

What you are looking for is P/Invoke .您正在寻找的是P/Invoke

What do the [] mean? [] 是什么意思?

This is the way you add attributes to function calls.这是您向 function 调用添加属性的方式。 This is the DllImportAttribute .这是DllImportAttribute

Alternatively, if you meant the set of brackets in UInt32[] , then this signifies that this is an array of UInt32's.或者,如果您指的是UInt32[]中的括号集,则这表示这是 UInt32 的数组

Sometimes they use pointers...有时他们使用指针...

It is possible to create pointers in C# unsafe code, but for use with P/Invoke there is a better system called marshalling .可以在 C# unsafe代码中创建指针,但是对于 P/Invoke 使用,有一个更好的系统称为marshalling Marshalling allows types to be converted between your managed C# code and the unmanaged code in the DLL.编组允许在您的托管 C# 代码和 DLL 中的非托管代码之间转换类型。 There is for instance a IntPtr class for representing pointers to integers that you can use in the method declaration, but you would normally just pass in a regular integer and the marshalling code will automatically convert it for you.例如,有一个IntPtr class 用于表示您可以在方法声明中使用的整数指针,但您通常只需传入一个常规 integer ,编组代码将自动为您转换它。

How can I tell when can I use those dll's?我怎么知道什么时候可以使用这些 dll? On what language they where created?他们在哪里创建的语言是什么?

I'm unsure how to tell whether a given DLL is managed or not, but in regards to what language a given DLL was written in is irrelevant, and very difficult to determine after it has been compiled.我不确定如何判断给定的 DLL 是否被管理,但是关于给定的 DLL 是用什么语言编写的是无关紧要的,并且在编译后很难确定。

The [DLLImport] is a class attribute. [DLLImport]是一个 class 属性。 This is a standard part of C#.这是 C# 的标准部件。 You can read more about attributes here - Attributes (C# and Visual Basic)您可以在此处阅读有关属性的更多信息 -属性(C# 和 Visual Basic)

This [] in UInt32[] data signifies that it is an array of UInt32 objects. UInt32[] data中的这个[]表示它是 UInt32 对象的数组。 This is a standard part of C# as well.这也是 C# 的标准部件。 You can read more about arrays in C# here - Arrays (C# Programming Guide)您可以在此处阅读 C# 中有关 arrays 的更多信息 - Arrays(C# 编程指南)

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

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