Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _declspec(dllexport) void TestArray(wchar_t** OutBuff,int Count,int MaxLength)
- {
- for(int i=0;i<Count;i++)
- {
- wchar_t buff[25];
- _itow(i,buff,10);
- wcsncpy(OutBuff[i],buff,MaxLength);
- }
- }
- class Program
- {
- [DllImport("Native.dll", EntryPoint = "?TestArray@@YAXPAPA_WHH@Z", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
- private static extern void TestArray([MarshalAs(UnmanagedType.LPArray)]
- IntPtr[] OutBuff, int Count, int MaxLength);
- static void Main(string[] args)
- {
- int count = 10;
- int maxLen = 50;
- IntPtr[] buffer = new IntPtr[maxLen];
- for (int i = 0; i < count; i++)
- buffer[i] = Marshal.AllocHGlobal(maxLen);
- TestArray(buffer, count, maxLen);
- string[] output = new string[count];
- for (int i = 0; i < count; i++)
- {
- output[i] = Marshal.PtrToStringUni(buffer[i]);
- Marshal.FreeHGlobal(buffer[i]); // crash is here, when count is 1
- Console.WriteLine(output[i]);
- }
- Console.ReadKey();
- }
- }
- }
- IntPtr[] buffer = new IntPtr[count]; // NOTE: not maxLen
- for (int i = 0; i < count; i++)
- buffer[i] = Marshal.AllocHGlobal(maxLen * sizeof(Char));
Add Comment
Please, Sign In to add comment