-1

I am trying to call some functions from a compiled C/C++ dll in a C# program. I have successfully called several of the functions but am having trouble with a couple.

Compliled c/c++ function call:

DECLDIR int ADS1292R_Get_Version(unsigned char ADS129x_Version[])
{
unsigned char Wbuf[10];
unsigned char Rbuf[10], RetVal =1;
Wbuf[0] = START_DATA_HEADER;                // PACKET Start header
Wbuf[1] = FIRMWARE_VERSION_REQ;             // Get firmware version info command
Wbuf[2] = 0x00;                             // Not used
Wbuf[3] = 0x00;                             // Not used
Wbuf[4] = 0x00;                             // Not used
Wbuf[5] = END_DATA_HEADER;                  // Packet End header
Wbuf[6] = '\n';


pComPort->Write(Wbuf, 7);                   // Send command to firmware
Sleep(5);
memset(Rbuf,0,7);
pComPort->Read(Rbuf, 7);                    // Receive response from Frimwate

if ((Rbuf[0] == START_DATA_HEADER) 
    && (Rbuf[1] == FIRMWARE_VERSION_PACKET) 
    && (Rbuf[5] == END_DATA_HEADER))
{
    ADS129x_Version[0]= Rbuf[2];            // Get Major Number
    ADS129x_Version[1]= Rbuf[3];            // Get Minor number
    RetVal = 0;                             // Set return val as su
}

return RetVal;

}

C# implementation (along with a wrapper function for the class):

  [DllImport("ADS1292R_USB_lib.dll", EntryPoint = "ADS1292R_Get_Version")]
    public static extern int ADS1292R_Get_Version(byte[] x);

    public int getVersion()
    {
        byte[] dataTemp = new byte[3];
        int mydata = ADS1292R_Get_Version(dataTemp);
        if (mydata == 0)
        {
            MessageBox.Show("1:" + dataTemp[0].ToString() 
                            + " 2:" + dataTemp[1].ToString() 
                            + " 3:" + dataTemp[2].ToString());
        }
        return 0;
    }

At run-time, the below error is being raised. Is there something that I am missing?

Updated with error text:

"PInvokeStackImbalance was detected" "A call to PInvoke function 'DLLTalk!DLLTalk.DLLClass::ADS1292R_Get_Version' 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."

Greg H
  • 1
  • 2
  • 2
    Are you getting an error message? What is it? – Robert Harvey May 14 '13 at 19:43
  • 1
    Also note, that you don't need C++ to communicate through [serial port](http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx) – Lol4t0 May 14 '13 at 19:48
  • You asked a question......... but in reference to what? You don't mention any issues/errors you're having. – tnw May 14 '13 at 20:04
  • Agreed that C# can handle serial port. The DLL has already been created to interact with the firmware. Was trying to "cheat" as use it without having to recreate the entire dll. – Greg H May 14 '13 at 20:14

1 Answers1

1

Don't forget the CallingConvention attribute setting as well. Also, it is good to denote if your native C++ library you are trying to invoke is 64 or 32bit, as you can't load 32bit assemblies in a 64bit process.

Using a 32bit or 64bit dll in C# DllImport

Community
  • 1
  • 1
CaptDialup
  • 46
  • 4