错误讯息 - 系统呼叫的资料区太小
下面的程序代码
在执行到Marshal.StructureToPtr(user, buffer, false)
会出现传递到系统呼叫的资料区太小的错误讯息,
请问是哪里出错了呢?
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct User
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public string name,
[MarshalAs(UnmanagedType.I4)]
public uint age;
}
User user;
user.name = tmpName.PadRight(32, ' ').ToCharArray();
user.age = tmpAge;
Int32 size = Marshal.SizeOf(user);
IntPtr buffer = Marshal.AllocHGlobal(size);
Byte[] bytes = new Byte[size];
Marshal.StructureToPtr(user, buffer, false);
Marshal.Copy(buffer, bytes, 0, size);
Marshal.FreeHGlobal(buffer);