Using dll with c#

Hi, I’m writing my GUI in C# and using okFrontPanel.dll. My wires and triggers are working properly but I’m having trouble with with the pipe. Here’s the relevant pieces of code followed by the error I’m getting.

unsafe private void GetData()
{
byte] ByteArray = new byte[656];
fixed (byte* buff_ptr = ByteArray)
{
long foo = XEM.okUsbFrontPanel_ReadFromPipeOut(handle, 0xA0, 656, buff_ptr);
}
}

unsafe class XEM
{
//…
[DllImport(“okFrontPanel.Dll”)]
public static extern long okUsbFrontPanel_ReadFromPipeOut(int handle, int epAddr, long length, byte *data);
//…
}

this builds fine but when I actually get to this spot in the program it hangs and gives me this error:

A call to PInvoke function ‘FDS!XEM::okUsbFrontPanel_ReadFromPipeOut’ has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Any Ideas?

Thanks,
Ryan

Well I found the problem… I was so sure that it was something to do with the pointer that I didn’t really pay attention to the other parameters. Turns out long in C++ is 32 bits but in C# is 64 bits so I changed the prototype to:

[DllImport(“okFrontPanel.Dll”)]
public unsafe static extern int okUsbFrontPanel_ReadFromPipeOut(int handle, int epAddr, int length, byte *data);

This now seems to be working for me.

Thanks for posting the update (and resolution)!

I am also using c# .NET to interface with okFrontPanel.Dll. I tried to read the GetSerialNumber but got nothing back. This is what I did:

   int XEMHandle = XEM.okUsbFrontPanel_Construct(); 
   MessageBox.Show(GetSerialNumber(XEMHandle));

    unsafe private string GetSerialNumber(int handle)
    {
        char] CharArray = new char[256];
        fixed (char* buff_ptr = CharArray)
        {
            XEM.okUsbFrontPanel_GetSerialNumber(handle, buff_ptr);
        }
        return new string(CharArray );
    }

unsafe class XEM
{
//…
[DllImport(“okFrontPanel.Dll”)]
public static extern int okUsbFrontPanel_Construct();

    [DllImport("okFrontPanel.Dll")]
    public static extern void okUsbFrontPanel_GetSerialNumber(int handle, char* buf);
    //...
 }

I was able to get the device count respond correctly using the above, so I am sure that the dll is loaded and running, I think my problem is on the char pointer… Can someone help me?