create a wrapper for libraries c#
Sometimes it will be great if I could use some other programs dll and customize them. For example there is a device called usb uirt where I can send ir signals from a computer. The software that is used to send the signals sucks and the drivers from the device come with a lot of dll's. As a result, I tried importing them to visual stud开发者_如何学运维io but I could not create a reference. 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.
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? 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. What phrase should I Google to learn about this?
What you are looking for is P/Invoke.
What do the [] mean?
This is the way you add attributes to function calls. This is the DllImportAttribute
.
Alternatively, if you meant the set of brackets in UInt32[]
, then this signifies that this is an array of UInt32's.
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. Marshalling allows types to be converted between your managed C# code and the unmanaged code in the 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.
How can I tell when can I use those dll's? 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.
The [DLLImport]
is a class attribute. This is a standard part of C#. You can read more about attributes here - Attributes (C# and Visual Basic)
This []
in UInt32[] data
signifies that it is an array of UInt32 objects. This is a standard part of C# as well. You can read more about arrays in C# here - Arrays (C# Programming Guide)
精彩评论