Calling C++ struct parameter from C#


Below is the header documentation to call the DLL:

#define CM24_NUMTYPENOTE            24

typedef struct SNOTETYPE
    char     Notetype[6];
    int      NumNote;

typedef struct SDEP
    int      InNumber;
    int      TotalDepBnk;
    int      Refused;
    int      Unrecognized;
    SNOTE    Types[CM24_NUMTYPENOTE*4];


int APIENTRY InterDoCheck                   (int , PADEPOSITCASS);

And here is my code in C# which is calling the C++ DLL

public struct SNOTE
    public String notetype;
    public int numNote;

public struct SDEPOSIT
    public int InNumber;
    public int TotalDepBnk;
    public int Refused;
    public int Unrecognized;
    public SNOTE[] Types;

public static extern int InterDoCheck(int mode, ref SDEPOSIT PADEPOSITCASS);

private void button3_Click(object sender, EventArgs e) 
    SDEPOSIT sdeposit = new SDEPOSIT();
    deposit.Types = new SNOTE[96];
    int r = InterDoCheck(0, ref sdeposit);

So, here when I click the button it will call the DLL and getting an error below:

An unhandled exception of type 'System.ArgumentException' occurred in WindowsFormsApp2.exe The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

I tried to commented out the line SDEPOSIT sdeposit = new SDEPOSIT(); It was calling successfully but it's getting hang when receiving the result from DLL.

I'm not sure if I'm I calling it correctly, or is my struct in C# is correct or not. Basically I want to know how to pass values to this type of struct in C++.

asked on Stack Overflow Jan 13, 2020 by OldCoder • edited Jan 13, 2020 by Bill Tür

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0