}
public static IntPtr /* IDispatch */ GetIDispatchForObject(object o) => throw new PlatformNotSupportedException();
+
+ public static void ZeroFreeBSTR(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)SysStringByteLen(s));
+ FreeBSTR(s);
+ }
+
+ public unsafe static void ZeroFreeCoTaskMemAnsi(IntPtr s)
+ {
+ ZeroFreeCoTaskMemUTF8(s);
+ }
+
+ public static unsafe void ZeroFreeCoTaskMemUnicode(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)(string.wcslen((char*)s) * 2));
+ FreeCoTaskMem(s);
+ }
+
+ public static unsafe void ZeroFreeCoTaskMemUTF8(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
+ FreeCoTaskMem(s);
+ }
+
+ public unsafe static void ZeroFreeGlobalAllocAnsi(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
+ FreeHGlobal(s);
+ }
+
+ public static unsafe void ZeroFreeGlobalAllocUnicode(IntPtr s)
+ {
+ if (s == IntPtr.Zero)
+ {
+ return;
+ }
+ RuntimeImports.RhZeroMemory(s, (UIntPtr)(string.wcslen((char*)s) * 2));
+ FreeHGlobal(s);
+ }
+
+ internal static unsafe uint SysStringByteLen(IntPtr s)
+ {
+ return *(((uint*)s) - 1);
+ }
}
}
override protected bool ReleaseHandle()
{
- RuntimeImports.RhZeroMemory(handle, (UIntPtr)(Interop.OleAut32.SysStringLen(handle) * sizeof(char)));
+ RuntimeImports.RhZeroMemory(handle, (UIntPtr)Marshal.SysStringByteLen(handle));
Interop.OleAut32.SysFreeString(handle);
return true;
}
try
{
AcquirePointer(ref bufferPtr);
- RuntimeImports.RhZeroMemory((IntPtr)bufferPtr, (UIntPtr)(Interop.OleAut32.SysStringLen((IntPtr)bufferPtr) * sizeof(char)));
+ RuntimeImports.RhZeroMemory((IntPtr)bufferPtr, (UIntPtr)Marshal.SysStringByteLen((IntPtr)bufferPtr));
}
finally
{
[DllImport(Interop.Libraries.OleAut32)]
internal static extern IntPtr SysAllocStringByteLen(byte[] str, uint len); // BSTR
- [DllImport(Interop.Libraries.OleAut32)]
- internal static extern uint SysStringByteLen(IntPtr bstr); // BSTR
-
[DllImport(Interop.Libraries.Kernel32, SetLastError = true)]
internal static extern unsafe int WriteFile(SafeFileHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern IntPtr GetFunctionPointerForDelegateInternal(Delegate d);
-
- public static void ZeroFreeBSTR(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)(Win32Native.SysStringLen(s) * 2));
- FreeBSTR(s);
- }
-
- public unsafe static void ZeroFreeCoTaskMemAnsi(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
- FreeCoTaskMem(s);
- }
-
- public static unsafe void ZeroFreeCoTaskMemUnicode(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)(string.wcslen((char*)s) * 2));
- FreeCoTaskMem(s);
- }
-
- public static unsafe void ZeroFreeCoTaskMemUTF8(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
- FreeCoTaskMem(s);
- }
-
- public unsafe static void ZeroFreeGlobalAllocAnsi(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)string.strlen((byte*)s));
- FreeHGlobal(s);
- }
-
- public static unsafe void ZeroFreeGlobalAllocUnicode(IntPtr s)
- {
- if (s == IntPtr.Zero)
- {
- return;
- }
- RuntimeImports.RhZeroMemory(s, (UIntPtr)(string.wcslen((char*)s) * 2));
- FreeHGlobal(s);
- }
}
}
}
else
{
- uint length = Win32Native.SysStringByteLen(bstr);
+ uint length = Marshal.SysStringByteLen(bstr);
// Intentionally checking the number of bytes not characters to match the behavior
// of ML marshalers. This prevents roundtripping of very large strings as the check