typedef struct struct_DATA_OPEN_SETTINGS { char szUserID[DEFAULT_SIZE]; //用户名 char szPassword[DEFAULT_SIZE]; //密码 char** pp; //数组 int nCount; //个数 pfnOnMsg pfnMsg; pfnOnData pfnData; }DATA_OPEN_SETTINGS;
上面结构体中的char** pp怎么在C#里面用?
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct DATA_OPEN_SETTINGS { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 25)] public string pszUserID; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 25)] public string pszPassword; //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] public string[] pp; public int nCount; public CallbackDelegateMsg callbackMsg; public CallbackDelegateData callbackData; }
这样写不行。
讨教高手,应该怎么写?
解决方案
100
不可以。
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct DATA_OPEN_SETTINGS { //... public IntPtr pp; // 用IntPtr来传入char** public int nCount; //... public void 准备数组pp(int 个数, int 最大缓冲长度) { nCount = 个数; pp = Marshal.AllocHGlobal(IntPtr.Size * nCount); for(int i = 0; i<nCount; i++) { IntPtr 缓冲区指针 = Marshal.AllocHGlobal(最大缓冲长度); Marshal.WriteIntPtr(pp + (IntPtr.Size), 缓冲区指针); } } public void 销毁数组pp() { // 要记得销毁... } }