I am importing some C ++ dll into a C # project, I am using Visual Studio 2010. I have successfully imported a function that uses the built-in type, however I get an error when I try to figure out the structure. This is a simple example:
C ++ code
typedef long int TDate; typedef struct _TMDYDate { long month; long day; long year; } TMonthDayYear; int JpmcdsDateToMDY (TDate date, TMonthDayYear *mdyDate);
and I translated into 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 a function in my test program, I get this error:
Unhandled exception: System.AccessViolationException: Attempted to read or write protected memory. This often indicates that another memory is corrupted. at CsharpWrapper.NativeMethods.JpmcdsDateToMDY (date Int32, date TMonthDayYear & mdy)
The structure is declared on the stack, and I thought (maybe) about the problem, but I still get the same error, although I have a TMonthDayYear change for the class.
What am I doing wrong?
Thanks for the help.
source share