public static unsafe string IntPtrToString(Encoding encoding, IntPtr ptr) { if (ptr == IntPtr.Zero) return null; // Find the length of the null terminated bytes int length = 0; while (*((byte*)ptr + length) != 0) length++; return IntPtrToString(encoding, ptr, length); }