【发布时间】:2009-03-20 17:00:33
【问题描述】:
OpenFileDialog 返回一个指向内存的指针,其中包含一系列以 null 结尾的字符串,后跟 final null 表示数组的结尾。
这就是我从非托管指针中获取 C# 字符串的方式,但我确信一定有一种更安全、更优雅的方式。
IntPtr unmanagedPtr = // start of the array ...
int offset = 0;
while (true)
{
IntPtr ptr = new IntPtr( unmanagedPtr.ToInt32() + offset );
string name = Marshal.PtrToStringAuto(ptr);
if(string.IsNullOrEmpty(name))
break;
// Hack! (assumes 2 bytes per string character + terminal null)
offset += name.Length * 2 + 2;
}
【问题讨论】:
标签: c# windows pinvoke marshalling openfiledialog