How to transfer a custom struct pointer to DLL's function?
-
I has a C++ DLL, it has follow code: typedef struct _SC_CONTEXT{ long hPort; long dwCardType; // BYTE params[24]; } SC_CONTEXT; extern int __stdcall TestFunc(SC_CONTEXT* p) { char buf[256]; sprintf(buf, "hPort is : %d, dwCardType is : %d.", p.hPort, p.dwCardType); // sprintf(buf, "hPort is : %d, dwCardType is : %d, params[0] is : %d.", p->hPort, p->dwCardType, p->params[0]); MessageBoxA(NULL, buf, "caption1", MB_OK); return 0; } I wrote C# code like : [StructLayout(LayoutKind.Sequential)] public struct _SC_CONTEXT { public long hPort; public long dwCardType; } namespace MyName { class MyClass { [DllImport("testDll.dll")] public static extern int TestFunc(ref _SC_CONTEXT11 a); } static void Main(string[] args) { unsafe { _SC_CONTEXT a;// = new _SC_CONTEXT(); a.hPort = 67; a.dwCardType = 66; // a.paras[0] = 255; TestFunc(ref a); } ...... } } the question is, function TestFunc can not get _SC_CONTEXT::dwCardType's value. In the messagebox, content always is "hPort is : 67, dwCardType is : 0.". I want to know why, and how to correct this bug. As you see, I comment an array member of the struct, if not comment it, how to use struct with array member.:confused:
-
I has a C++ DLL, it has follow code: typedef struct _SC_CONTEXT{ long hPort; long dwCardType; // BYTE params[24]; } SC_CONTEXT; extern int __stdcall TestFunc(SC_CONTEXT* p) { char buf[256]; sprintf(buf, "hPort is : %d, dwCardType is : %d.", p.hPort, p.dwCardType); // sprintf(buf, "hPort is : %d, dwCardType is : %d, params[0] is : %d.", p->hPort, p->dwCardType, p->params[0]); MessageBoxA(NULL, buf, "caption1", MB_OK); return 0; } I wrote C# code like : [StructLayout(LayoutKind.Sequential)] public struct _SC_CONTEXT { public long hPort; public long dwCardType; } namespace MyName { class MyClass { [DllImport("testDll.dll")] public static extern int TestFunc(ref _SC_CONTEXT11 a); } static void Main(string[] args) { unsafe { _SC_CONTEXT a;// = new _SC_CONTEXT(); a.hPort = 67; a.dwCardType = 66; // a.paras[0] = 255; TestFunc(ref a); } ...... } } the question is, function TestFunc can not get _SC_CONTEXT::dwCardType's value. In the messagebox, content always is "hPort is : 67, dwCardType is : 0.". I want to know why, and how to correct this bug. As you see, I comment an array member of the struct, if not comment it, how to use struct with array member.:confused:
I've spotted at least one bug immediately: .NET
long
is 64bit, but in C++ it's 32bit! So you should declare your struct withint
instead:samfromcn wrote:
public struct _SC_CONTEXT { public long hPort; public long dwCardType; }
public struct _SC_CONTEXT
{
public int hPort;
public int dwCardType;
}Regards, mav -- Black holes are the places where God divided by 0...
-
I've spotted at least one bug immediately: .NET
long
is 64bit, but in C++ it's 32bit! So you should declare your struct withint
instead:samfromcn wrote:
public struct _SC_CONTEXT { public long hPort; public long dwCardType; }
public struct _SC_CONTEXT
{
public int hPort;
public int dwCardType;
}Regards, mav -- Black holes are the places where God divided by 0...