I am importing some c++ dll into a c# project, I am using visual studio 2010. I have succeded to import function that are using built-in type, however I am getting error when I have tried to deal with structure. This is a simple example:
c++ code
typedef long int TDate;
typedef struct _TMDYDate
{
long month; /* In range [1,12] */
long day; /* In range [1-31] */
long year; /* In range [1600-] */
} TMonthDayYear;
int JpmcdsDateToMDY
(TDate date, /* (I) TDate format */
TMonthDayYear *mdyDate);
and I have translated to c# as:
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct TMonthDayYear {
public int month;
public int day;
public int year;
}
public partial class NativeMethods {
[System.Runtime.InteropServices.DllImportAttribute("MyDll.dll", EntryPoint="JpmcdsDateToMDY")]
public static extern int JpmcdsDateToMDY(int date, ref TMonthDayYear mdyDate) ;
}
when I try to run the function in my test program I get this error:
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at CsharpWrapper.NativeMethods.JpmcdsDateToMDY(Int32 date, TMonthDayYear& mdy Date)
The struct are declare in the stack and I thought (maybe) was the problem but I am still getting the same error even though I have change TMonthDayYear to class.
What am I doing wrong?
Thanks for you help.
You have to use the CallingConvention property in the [DllImport] attribute, this is Cdecl since you didn’t use __stdcall in the native function declaration. While that’s wrong, it is not a great explanation for the AV. You need to debug the C code, the AV suggests it has a pointer bug. Project + Properties, Debug, tick “Enable unmanaged code debugging” and set a breakpoint on the C function.
Frankly, a date conversion like this should be written in pure C#.