看看能用不 Function StringToHeap(ByVal s As String, ByVal encoding As System.Text.Encoding) As IntPtr If encoding Is Nothing Then Return "" End If Dim min_byte_count As Integer = encoding.GetMaxByteCount(1) Dim copy() As Char = s.ToCharArray() Dim marshal(encoding.GetByteCount(copy) + min_byte_count) As Byte Dim bytes_copied As Integer = encoding.GetBytes(copy, 0, copy.Length, marshal, 0) If bytes_copied <> marshal.Length - min_byte_count Then Throw New NotSupportedException("encoding.GetBytes() doesn't equal encoding.GetByteCount()!") End If Dim mem As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(marshal.Length) If mem = IntPtr.Zero Then Throw New OutOfMemoryException() End If Dim copied As Boolean = False Try System.Runtime.InteropServices.Marshal.Copy(marshal, 0, mem, marshal.Length) copied = True Finally If copied = False Then System.Runtime.InteropServices.Marshal.FreeCoTaskMem(mem) End If End Try Return mem End Function
不好意思,上面贴的代码错了,正确的代码是:00253 public static unsafe string HeapToString (IntPtr p, Encoding encoding) 00254 { 00255 if (encoding == null) 00256 return Marshal.PtrToStringAnsi (p); 00257 00258 if (p == IntPtr.Zero) 00259 return null; 00260 00261 // This assumes a single byte terminates the string. 00262 00263 int len = 0; 00264 while (Marshal.ReadByte (p, len) != 0) 00265 checked {++len;} 00266 00267 string s = new string ((sbyte*) p, 0, len, encoding); 00268 len = s.Length; 00269 while (len > 0 && s [len-1] == 0) 00270 --len; 00271 if (len == s.Length) 00272 return s; 00273 return s.Substring (0, len); 00274 }
00223 {
00224 if (encoding == null)
00225 return Marshal.StringToCoTaskMemAnsi (s);
00226
00227 int min_byte_count = encoding.GetMaxByteCount(1);
00228 char[] copy = s.ToCharArray ();
00229 byte[] marshal = new byte [encoding.GetByteCount (copy) + min_byte_count];
00230
00231 int bytes_copied = encoding.GetBytes (copy, 0, copy.Length, marshal, 0);
00232
00233 if (bytes_copied != (marshal.Length-min_byte_count))
00234 throw new NotSupportedException ("encoding.GetBytes() doesn't equal encoding.GetByteCount()!");
00235
00236 IntPtr mem = Marshal.AllocCoTaskMem (marshal.Length);
00237 if (mem == IntPtr.Zero)
00238 throw new OutOfMemoryException ();
00239
00240 bool copied = false;
00241 try {
00242 Marshal.Copy (marshal, 0, mem, marshal.Length);
00243 copied = true;
00244 }
00245 finally {
00246 if (!copied)
00247 Marshal.FreeCoTaskMem (mem);
00248 }
00249
00250 return mem;
00251 }
If encoding Is Nothing Then
Return ""
End If
Dim min_byte_count As Integer = encoding.GetMaxByteCount(1)
Dim copy() As Char = s.ToCharArray()
Dim marshal(encoding.GetByteCount(copy) + min_byte_count) As Byte
Dim bytes_copied As Integer = encoding.GetBytes(copy, 0, copy.Length, marshal, 0)
If bytes_copied <> marshal.Length - min_byte_count Then
Throw New NotSupportedException("encoding.GetBytes() doesn't equal encoding.GetByteCount()!")
End If
Dim mem As IntPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(marshal.Length)
If mem = IntPtr.Zero Then
Throw New OutOfMemoryException()
End If
Dim copied As Boolean = False
Try
System.Runtime.InteropServices.Marshal.Copy(marshal, 0, mem, marshal.Length)
copied = True
Finally
If copied = False Then
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(mem)
End If
End Try
Return mem
End Function
00254 {
00255 if (encoding == null)
00256 return Marshal.PtrToStringAnsi (p);
00257
00258 if (p == IntPtr.Zero)
00259 return null;
00260
00261 // This assumes a single byte terminates the string.
00262
00263 int len = 0;
00264 while (Marshal.ReadByte (p, len) != 0)
00265 checked {++len;}
00266
00267 string s = new string ((sbyte*) p, 0, len, encoding);
00268 len = s.Length;
00269 while (len > 0 && s [len-1] == 0)
00270 --len;
00271 if (len == s.Length)
00272 return s;
00273 return s.Substring (0, len);
00274 }
我看了一下msdn,上面是这样滴Visual Basic(声明)
Visual Basic 不支持会使用或返回不安全类型的 API。
Visual Basic(用法)
Visual Basic 不支持会使用或返回不安全类型的 API。不过我很纳闷,既然就是net本身的string,又不啥第三方的东西,为啥一定你要用这个构造,直接默认构造,然后赋值就是了。
如果非要用这个构造,只能用Marshal类封送到托管对象上玩,有多此一举的嫌疑说