📄 marshalex.cs
字号:
/// <param name="source">The memory pointer to copy from.</param>
/// <param name="destination">The array to copy to.</param>
/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
/// <param name="length">The number of array elements to copy.</param>
public static void Copy(IntPtr source, char[] destination, int startIndex, int length)
{
Marshal.Copy(source, destination, startIndex, length);
}
/// <summary>
/// Copies data from an unmanaged memory pointer to a managed 32-bit signed integer array.
/// </summary>
/// <param name="source">The memory pointer to copy from.</param>
/// <param name="destination">The array to copy to.</param>
/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
/// <param name="length">The number of array elements to copy.</param>
public static void Copy(IntPtr source, int[] destination, int startIndex, int length)
{
Marshal.Copy(source, destination, startIndex, length);
}
/// <summary>
/// Copies data from a one-dimensional, managed 8-bit unsigned integer array to an unmanaged memory pointer.
/// </summary>
/// <param name="source">The one-dimensional array to copy from.</param>
/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
/// <param name="destination">The memory pointer to copy to.</param>
/// <param name="length">The number of array elements to copy.</param>
public static void Copy(byte[] source, int startIndex, IntPtr destination, int length)
{
Marshal.Copy(source, startIndex, destination, length);
}
/// <summary>
/// Copies data from a one-dimensional, managed double-precision floating-point number array to an unmanaged memory pointer.
/// </summary>
/// <param name="source">The one-dimensional array to copy from.</param>
/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
/// <param name="destination">The memory pointer to copy to.</param>
/// <param name="length">The number of array elements to copy.</param>
public static void Copy(double[] source, int startIndex, IntPtr destination, int length)
{
Marshal.Copy(source, startIndex, destination, length);
}
/// <summary>
/// Copies data from a one-dimensional, managed single-precision floating-point number array to an unmanaged memory pointer
/// </summary>
/// <param name="source">The one-dimensional array to copy from.</param>
/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
/// <param name="destination">The memory pointer to copy to.</param>
/// <param name="length">The number of array elements to copy.</param>
public static void Copy(float[] source, int startIndex, IntPtr destination, int length)
{
Marshal.Copy(source, startIndex, destination, length);
}
/// <summary>
/// Copies data from a one-dimensional, managed 64-bit signed integer array to an unmanaged memory pointer.
/// </summary>
/// <param name="source">The one-dimensional array to copy from.</param>
/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
/// <param name="destination">The memory pointer to copy to.</param>
/// <param name="length">The number of array elements to copy.</param>
public static void Copy(long[] source, int startIndex, IntPtr destination, int length)
{
Marshal.Copy(source, startIndex, destination, length);
}
/// <summary>
/// Copies data from a one-dimensional, managed 16-bit signed integer array to an unmanaged memory pointer.
/// </summary>
/// <param name="source">The one-dimensional array to copy from.</param>
/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
/// <param name="destination">The memory pointer to copy to.</param>
/// <param name="length">The number of array elements to copy.</param>
public static void Copy(short[] source, int startIndex, IntPtr destination, int length)
{
Marshal.Copy(source, startIndex, destination, length);
}
/// <summary>
/// Copies data from a one-dimensional, managed character array to an unmanaged memory pointer.
/// </summary>
/// <param name="source">The one-dimensional array to copy from.</param>
/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
/// <param name="destination">The memory pointer to copy to.</param>
/// <param name="length">The number of array elements to copy.</param>
public static void Copy(char[] source, int startIndex, IntPtr destination, int length)
{
Marshal.Copy(source, startIndex, destination, length);
}
/// <summary>
/// Copies data from a one-dimensional, managed 32-bit signed integer array to an unmanaged memory pointer.
/// </summary>
/// <param name="source">The one-dimensional array to copy from.</param>
/// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
/// <param name="destination">The memory pointer to copy to.</param>
/// <param name="length">The number of array elements to copy.</param>
public static void Copy(int[] source, int startIndex, IntPtr destination, int length)
{
Marshal.Copy(source, startIndex, destination, length);
}
#endregion
#region Memory Functions
private static bool IsNotWin32Atom(IntPtr ptr)
{
long b;
b = (long)ptr;
return (((long) 0) != (b & (long)MarshalEx.HIWORDMASK));
}
/// <summary>
/// Allocates unmanaged memory.
/// </summary>
/// <param name="cb">The number of bytes in memory required. </param>
/// <returns>An IntPtr to the newly allocated memory. This memory must be released using the Marshal.FreeHGlobal method.</returns>
public static IntPtr AllocHGlobal(int cb)
{
IntPtr ptr = LocalAlloc(LPTR, (uint)cb);
if (ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return ptr;
}
/// <summary>
/// Allocates unmanaged memory.
/// </summary>
/// <param name="cb">The number of bytes in memory required. </param>
/// <returns>An IntPtr to the newly allocated memory. This memory must be released using the Marshal.FreeHGlobal method.</returns>
public static IntPtr AllocHGlobal(IntPtr cb)
{
return MarshalEx.AllocHGlobal((int)cb);
}
/// <summary>
/// Resizes a block of memory previously allocated with <see cref="AllocHGlobal(System.IntPtr)"/>.
/// <para><b>New in v1.2</b></para>
/// </summary>
/// <param name="pv">A pointer to memory allocated with <see cref="AllocHGlobal(System.IntPtr)"/>.</param>
/// <param name="cb"> The new size of the allocated block.</param>
/// <returns>An <see cref="System.IntPtr"/> to the reallocated memory.
/// This memory must be released using <see cref="FreeHGlobal(System.IntPtr)"/>.</returns>
/// <exception cref="OutOfMemoryException">There is insufficient memory to satisfy the request.</exception>
public static IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb)
{
IntPtr ptr = LocalReAllocCE(pv, (int)cb, LPTR);
if(ptr == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
return ptr;
}
/// <summary>
/// Frees memory previously allocated from unmanaged memory.
/// </summary>
/// <param name="hGlobal">The handle returned by the original matching call to AllocHGlobal.</param>
public static void FreeHGlobal(IntPtr hGlobal)
{
if(MarshalEx.IsNotWin32Atom(hGlobal))
{
LocalFree(hGlobal);
}
else
{
throw new ArgumentException("hGlobal is not an unmanaged atom.");
}
}
/// <summary>
/// Allocates unmanaged memory.
/// </summary>
/// <param name="cb">The number of bytes in memory required. </param>
/// <returns>An IntPtr to the newly allocated memory. This memory must be released using the <see cref="M:MarshalEx.FreeHLocal(System.IntPtr)"/> method.</returns>
[CLSCompliant(false)]
public static IntPtr AllocHLocal(uint cb)
{
return LocalAlloc(LPTR, cb);
}
/// <summary>
/// Allocates unmanaged memory.
/// </summary>
/// <param name="cb">The number of bytes in memory required. </param>
/// <returns>An IntPtr to the newly allocated memory. This memory must be released using the <see cref="M:MarshalEx.FreeHLocal(System.IntPtr)"/> method.</returns>
public static IntPtr AllocHLocal(int cb)
{
return LocalAlloc(LPTR, (uint)cb);
}
/// <summary>
/// Frees memory previously allocated from unmanaged memory.
/// </summary>
/// <param name="hMem">The handle returned by the original matching call to AllocHGlobal.</param>
public static void FreeHLocal(IntPtr hMem)
{
if(hMem != IntPtr.Zero)
LocalFree(hMem);
}
/// <summary>
/// Converts a time_t value to a DateTime value.
/// </summary>
/// <param name="time_t">The time_t value to convert.</param>
/// <returns>A DateTime value equivalent to the time_t suppled.</returns>
[CLSCompliant(false)]
public static DateTime Time_tToDateTime(uint time_t)
{
long win32FileTime = 10000000*(long)time_t + 116444736000000000;
return DateTime.FromFileTimeUtc(win32FileTime);
}
/// <summary>
/// Returns the unmanaged size of an object in bytes.
/// </summary>
/// <param name="structure">The object whose size is to be returned.</param>
/// <returns>The size of the structure parameter in unmanaged code</returns>
public static int SizeOf(object structure)
{
return Marshal.SizeOf(structure);
}
/// <summary>
/// Returns the size of an unmanaged type in bytes.
/// </summary>
/// <param name="t">The System.Type whose size is to be returned.</param>
/// <returns>The size of the structure parameter in unmanaged code</returns>
public static int SizeOf(Type t)
{
return Marshal.SizeOf(t);
}
/// <summary>
/// Marshals data from a managed object to an unmanaged block of memory.
/// </summary>
/// <param name="structure">A managed object holding the data to be marshaled. This object must be an instance of a formatted class.</param>
/// <param name="ptr">A pointer to an unmanaged block of memory, which must be allocated before this method is called.</param>
/// <param name="fDeleteOld">true to have the System.Runtime.InteropServices.Marshal.DestroyStructure(System.IntPtr,System.Type) method called on the ptr parameter before this method executes. Note that passing false can lead to a memory leak.</param>
public static void StructureToPtr(object structure, IntPtr ptr, bool fDeleteOld)
{
Marshal.StructureToPtr(structure, ptr, fDeleteOld);
}
/// <summary>
/// Returns the length of the string at the pointer
/// </summary>
/// <param name="ptr">The pointer to the string to measure.</param>
/// <returns>The length of the string at the pointer.</returns>
private static int lstrlenW(IntPtr ptr)
{
return String_wcslen(ptr);
}
#endregion
#region Miscellaneous Functions
#region GetHINSTANCE
/// <summary>
/// Returns the instance handle (HINSTANCE) for the specified module.
/// <para><b>New in v1.3</b></para>
/// </summary>
/// <param name="m">The <see cref="System.Reflection.Module"/> whose HINSTANCE is desired.</param>
/// <returns>The HINSTANCE for m; -1 if the module does not have an HINSTANCE.</returns>
public static IntPtr GetHINSTANCE(System.Reflection.Module m )
{
IntPtr hinst = IntPtr.Zero;
if(m.Assembly == System.Reflection.Assembly.GetCallingAssembly())
{
hinst = GetModuleHandle(null);
}
else
{
hinst = GetModuleHandle(m.Assembly.GetName().CodeBase);
}
if(hinst == IntPtr.Zero)
{
return new IntPtr(-1);
}
else
{
return hinst;
}
}
[DllImport("coredll.dll", EntryPoint="GetModuleHandleW", SetLastError=true)]
private static extern IntPtr GetModuleHandle(string lpszModule);
#endregion
/// <summary>
/// Returns the error code returned by the last unmanaged function called using platform invoke that has the System.Runtime.InteropServices.DllImportAttribute.SetLastError flag set.
/// </summary>
/// <returns>The last error code set by a call to the Win32 SetLastError API method. </returns>
public static int GetLastWin32Error()
{
return Marshal.GetLastWin32Error();
}
/// <summary>
/// Indicates whether a specified object represents a COM object.
/// </summary>
/// <param name="o">The object to check. </param>
/// <returns> true if the o parameter is a COM type; otherwise, false.</returns>
public static bool IsComObject(object o)
{
return Marshal.IsComObject(o);
}
#endregion
#region API Prototypes
[DllImport("coredll.dll",EntryPoint="LocalAlloc",SetLastError=true)]
static extern IntPtr LocalAlloc(uint uFlags, uint Bytes);
[DllImport("coredll.dll", EntryPoint="LocalReAlloc", SetLastError=true)]
static extern IntPtr LocalReAllocCE(IntPtr hMem, int uBytes, int fuFlags);
[DllImport("coredll.dll",EntryPoint="LocalFree",SetLastError=true)]
static extern IntPtr LocalFree(IntPtr hMem);
[DllImport("mscoree.dll",EntryPoint="#1",SetLastError=true)]
static extern int String_wcslen(IntPtr pws);
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -