Reputation: 523
I'm writing a program for my college's chemistry department, incorporating three separate devices - a force sensor, a Z-axis nanopositioner stage, and a XYZ axis picomotor stage. I have to incorporate these three devices into a single program and have them communicate with one another. I've already successfully incorporated the force sensor and Z-axis stage. They both came with DLLs ready-made for C#. Since I'm writing a GUI program and I'm already fairly familiar with C#, this was the natural choice.
However, the XYZ stage's DLL was written for C++. I'm not really sure how to get this DLL to work in my program. I can't add a reference to it in Visual Studio because it's not compatible, and the header files that come with it that expose the functions in the DLL were written for C++.
I have a basic understanding of the process. In the ldcncom.h header, there'es a function written as so:
DLLENTRY(int) LdcnInit(char *portname, unsigned int baudrate);
I rewrote it as this in my C# program:
[DllImport("Ldcnlib.dll", EntryPoint = "LdcnInit")]
public static extern int LdcnInit(char[] portname, uint baudrate);
I'm not sure if this is correct, but it "worked" insofar that it was able to read the function from the DLL and perform it's duty... just not completely right because it's still missing all of the other functions.
But I'm not sure how to do the rest. For instance, in sio_util.h, there's a function with a return type of HANDLE, another with a return type of DWORD, and I'm not very familiar with these and don't know how to make it work in C#.
I've heard of stuff like interop, wrappers, C++/CLI, but that's a little beyond me at the moment (still somewhat new to all of this), so any advice on how to do this process would be great.
Thank you.
Upvotes: 2
Views: 557
Reputation: 2681
If you need to convert more C++ data types into C# and find it time consuming, there is great tool "P/Invoke Signature Generator" which makes this job easy. Here is good article about how to use it.
You have to copy the native method signature and the tool generates the C# signature.
Upvotes: 1
Reputation: 564811
But I'm not sure how to do the rest. For instance, in sio_util.h, there's a function with a return type of HANDLE, another with a return type of DWORD, and I'm not very familiar with these and don't know how to make it work in C#.
DWORD
is a 32 bit unsigned integer, so you'd use UInt32
(uint
) to represent that.
A HANDLE
is a void*
, or any pointer, so you'd typically use IntPtr
to represent a HANDLE
in C#.
The Windows Data Types page lists most of the Windows standard types, which should provide a good starting point on figuring out the P/Invoke types you'd need.
Upvotes: 4