OpenFileDialog returns a pointer to memory containing a sequence of strings with a null terminating character, and then trailing null to indicate the end of the array.
This is how I return C # strings from an unmanaged pointer, but I'm sure there should 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; }
What you do looks pretty good - the only change I would make is to use Encoding.Unicode.GetByteCount(name)instead name.Length * 2(it's more obvious what is happening).
Encoding.Unicode.GetByteCount(name)
name.Length * 2
, Marshal.PtrToStringUni(ptr), , Unicode, .
Marshal.PtrToStringUni(ptr)
Source: https://habr.com/ru/post/1746493/More articles:Criteria for an intellectual program - artificial-intelligenceAdnimistration wordpress page - htmlHow to live plugin in jQuery? - jqueryXcode Organizer Screen Capture Problem - iphoneRedmon Run As User does not load user environment variables - c #XML and XSLT: only specific child nodes need to be sorted - xmlШаблон Visual Studio 2010 для нового модуля UnitTest - visual-studio-2010Should I commit or roll back a transaction that creates a temporary table, reads, and then deletes it? - commitPython 3.0 IDE - Komodo and Eclipse are both flaky? - pythonHow to finish work in Matlab? - matlabAll Articles