C# Marshal.PtrToStringAnsi中的垃圾字符
在下面的代码中,pcp_Out应该以ANSI格式返回系统日期C# Marshal.PtrToStringAnsi中的垃圾字符,c#,.net,marshalling,dllimport,C#,.net,Marshalling,Dllimport,在下面的代码中,pcp_Out应该以ANSI格式返回系统日期 返回系统日期,但它前面有一些垃圾字符 AllocHGlobal初始化IntPtr是否正确 输出 Outstring = "h\0Qr\0\0\0\0Ä<g\a?\004/22/13 10:25" Outstring=“h\0Qr\0\0\0\0Ä我认为,为空指针分配内存的更好方法是: IntPtr OutPtr = new IntPtr(); IntPtr ErrorPtr = new IntPtr(); 如果pcp_
- 返回系统日期,但它前面有一些垃圾字符
- AllocHGlobal初始化IntPtr是否正确
Outstring = "h\0Qr\0\0\0\0Ä<g\a?\004/22/13 10:25"
Outstring=“h\0Qr\0\0\0\0Ä我认为,为空指针分配内存的更好方法是:
IntPtr OutPtr = new IntPtr();
IntPtr ErrorPtr = new IntPtr();
如果pcp_Out是Unicode字符串,则尝试使用:
CharSet=CharSet.Unicode
谢谢drew,pcp_Out是ANSI格式。我编辑了我的问题以反映这一点。如果你给我这个库,我可以尝试正确调用该函数。调用AllocHGlobal并传递0永远都不正确。如果pinvoke声明正确,那么你就不能使用该函数,这会导致内存泄漏。很好如果不正确,请从声明中删除out,并使用Marshal.AllocHGlobal(666)创建足够大的缓冲区。
IntPtr OutPtr = new IntPtr();
IntPtr ErrorPtr = new IntPtr();