错误讯息 - 系统呼叫的资料区太小

iorice 2012-04-25 03:26:03

下面的程序代码
在执行到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);
...全文
351 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
蔡袅 2012-04-25
  • 打赏
  • 举报
回复
这是一个无厘头的错误,信息源太少。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧