and thanks for the consultation for any help.
i has this trivial function in C:
__declspec(dllexport) Point* createPoint (int x, int y) {
Point *p;
p = (Point*) malloc(sizeof(Point));
p->x = x;
p->y=y;
return p;
}
A dot is a very simple structure with two int fields, x and y.
I would like to call this function from C #.
I am using this code:
[DllImport("simpleC.dll", EntryPoint = "createPoint", CallingConvention = CallingConvention.Cdecl, SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.LPStruct)]
public static extern Point createPoint(int x, int y);
Point p = Wrapper.createPoint(1, 2);
But at runtime I have AccessViolationException. Watching the exception in detail, I found that the exception was thrown from the method Marshal.CoTaskMemFree(IntPtr).
It seems that this method cannot free the memory allocated by C malloc.
What am I doing wrong?
Thanks a lot.
source
share