Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Marshal.PtrToStringAnsi中的垃圾字符_C#_.net_Marshalling_Dllimport - Fatal编程技术网

C# Marshal.PtrToStringAnsi中的垃圾字符

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_

在下面的代码中,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_Out是Unicode字符串,则尝试使用:
CharSet=CharSet.Unicode

谢谢drew,pcp_Out是ANSI格式。我编辑了我的问题以反映这一点。如果你给我这个库,我可以尝试正确调用该函数。调用AllocHGlobal并传递0永远都不正确。如果pinvoke声明正确,那么你就不能使用该函数,这会导致内存泄漏。很好如果不正确,请从声明中删除out,并使用Marshal.AllocHGlobal(666)创建足够大的缓冲区。
IntPtr OutPtr = new IntPtr();
IntPtr ErrorPtr = new IntPtr();