从 PInvoked OpenFileDialog (GetOpenFileName) 编组非托管字符串数组
OpenFileDialog 返回一个指向内存的指针,其中包含一系列以 null 结尾的字符串,后跟最终的 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;
}
OpenFileDialog returns a pointer to memory containing a sequence of null-terminated strings, followed by final null to indicate the end of the array.
This is how I'm getting C# strings back from the unmanaged pointer, but I'm sure there must be a safer, more elegant way.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你正在做的事情看起来相当不错 - 我要做的唯一改变是使用
Encoding.Unicode.GetByteCount(name)
而不是name.Length * 2
(它是只是更明显发生了什么)。另外,如果您确信非托管数据是 Unicode,则可能需要使用
Marshal.PtrToStringUni(ptr)
,因为它消除了有关字符串编码的任何歧义。What you're doing looks pretty good - the only change I would make would be to use
Encoding.Unicode.GetByteCount(name)
instead ofname.Length * 2
(it's just more obvious what's going on).Also, you might want to use
Marshal.PtrToStringUni(ptr)
if you are positive that your unmanaged data is Unicode, as it removes any ambiguity about your string encoding.