I've been fighting this problem for some time now and am hoping someone can help. I'm converting a VB6 application to C#. Everything I'm about to show works perfect in VB6, but fails in C#.
I'm making an API call into a C dll, passing a structure. The structure gets values modified in the C side and I should see the modified values in my return structure.
Here are the C structures:
Expand|Select|Wrap|Line Numbers
- typedef struct
- {
- short vers;
- short flags;
- short cmd;
- short objtype;
- DWORD id;
- DWORD client;
- char buf[MAX_TOOLBUF];
- DWORD toolID;
- NMSG msg;
- DWORD caller;
- CLIENTID clientID;
- DWORD ticket;
- long spare[4];
- } Request;
- typedef struct
- {
- DWORD hwnd;
- DWORD message;
- DWORD wParam;
- DWORD lParam;
- } NMSG;
Expand|Select|Wrap|Line Numbers
- [StructLayout(LayoutKind.Sequential)]
- public struct NMSG
- {
- [MarshalAs(UnmanagedType.U4)]
- public int hWnd;
- [MarshalAs(UnmanagedType.U4)]
- public int msg;
- [MarshalAs(UnmanagedType.U4)]
- public int wParam;
- [MarshalAs(UnmanagedType.U4)]
- public int lParam;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct Request
- {
- [MarshalAs(UnmanagedType.U4)]
- public Int32 vers;
- [MarshalAs(UnmanagedType.U2)]
- public Int16 flags;
- [MarshalAs(UnmanagedType.U2)]
- public Int16 cmd;
- [MarshalAs(UnmanagedType.U4)]
- public int objType;
- [MarshalAs(UnmanagedType.U4)]
- public int id;
- [MarshalAs(UnmanagedType.U4)]
- public int Client;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
- public string buf;
- [MarshalAs(UnmanagedType.U4)]
- public int toolID;
- [MarshalAs(UnmanagedType.Struct)]
- public NMSG msg;
- [MarshalAs(UnmanagedType.U4)]
- public int caller;
- [MarshalAs(UnmanagedType.U4)]
- public int clientID;
- [MarshalAs(UnmanagedType.U4)]
- public int ticket;
- [MarshalAs(UnmanagedType.U4)]
- public int result;
- [MarshalAs(UnmanagedType.Struct)]
- public MVToolResult res;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst= 4) ]
- public int[] spare;
- }
Expand|Select|Wrap|Line Numbers
- SendRequest(Request PTR req)
- {
- ....
- }
Expand|Select|Wrap|Line Numbers
- [DllImport("TheCDLL.dll", EntryPoint = "_Request@4", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
- public static extern IntPtr Request(ref Request req);
Expand|Select|Wrap|Line Numbers
- Request req = new Request();
- req.vers = Marshal.SizeOf(req);
- req.vers = 1;
- req.toolID = 4000;
- req.Client = 0;
- req.cmd = 11;
- req.objType = 1;;
- req.id = 1;
- req.msg.hWnd = Hwnd;
- req.msg.msg = msg;
- req.msg.wParam = wParam;
- req.msg.lParam = lParam;
- req.spare = new int[4];
- req.buf = new string(' ', 260);
- req.flags = 11;
- IntPtr retptr = Request(ref req);