Question about C# DllImports for K8055 & K8061

Hello,

A while ago I bought the pre assembled K8055 board and wrote a C# application in MS Visual studio 2008. Since I never had to import a DLL before it was quite a challenge to get it working. (the examples don’t include C#) But thanks to some great forums I found a full set of import instructions for the C# implementation of the ‘K8055D.dll’ which work perfectly.

Now I have switched over to the extended version K8061 and am faced with the same problem of creating import instructions for my C# project to use the ‘K8061_C.dll’. After some searching again I found a full set.

But they are very dissimilar from the ones for the ‘K8055D.dll’ even though in the manual of both boards the API discriptions are similar.

These are some for the ‘K8055D.dll’ that work fine.

[DllImport("k8055d.dll")] public static extern int OpenDevice(int CardAddress); [DllImport("k8055d.dll")] public static extern void CloseDevice(); [DllImport("k8055d.dll")] public static extern int ReadAnalogChannel(int Channel); [DllImport("k8055d.dll")] public static extern void ReadAllAnalog(ref int Data1, ref int Data2); [DllImport("k8055d.dll")] public static extern void OutputAnalogChannel(int Channel, int Data);

And these are some of the ‘K8061_C.dll’ I found but won’t work unless i rewrite alot of my code.

[DllImport("K8061_c.dll")] private static extern unsafe UInt32 OpenDevice(); [DllImport("K8061_c.dll")] private static extern unsafe void CloseDevices(); [DllImport("K8061_c.dll")] private static extern unsafe byte PowerGood(byte CardAddress); [DllImport("K8061_c.dll")] private static extern unsafe byte Connected(byte CardAddress); [DllImport("K8061_c.dll")] private static extern unsafe void ReadVersion(byte CardAddress, ref UInt32 Buffer); [DllImport("K8061_c.dll")] private static extern unsafe void ReadAllAnalog(byte CardAddress, ref UInt32 Buffer);

I figured out that I can change the data-types and remove the unsafe keyword. I tested a few and seemed to work.

But now I’m confused as to which ones are best practice.

I’d be glad to get some more advice on this matter. (if this is the right place to ask)

Bender

[DllImport("K8061_c.dll")]
public static extern int OpenDevice();
[DllImport("K8061_c.dll")]
public static extern void CloseDevices();
[DllImport("K8061_c.dll")]
public static extern byte PowerGood(int addr);
[DllImport("K8061_c.dll")]
public static extern byte Connected(int addr);
[DllImport("K8061_c.dll")]
public static extern unsafe void ReadVersion(int addr, ref int buffer);
[DllImport("K8061_c.dll")]
public static extern unsafe void ReadAllAnalog(byte addr, ref int buffer);

The card address is of type int (32-bit signed integer), passing only a byte will corrupt the stack. Some functions return a boolean (1 byte) which is represented here by the byte type.

http://msdn.microsoft.com/en-us/library/chfa2zb8(VS.71).aspx “unsafe keyword (C#)”
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute(VS.71).aspx “DllImportAttribute Class”
http://msdn.microsoft.com/en-us/library/14akc2c7.aspx “ref (C# Reference)”
http://msdn.microsoft.com/en-us/library/e59b22c5(VS.71).aspx “extern keyword (C#)”