Exemple #1
0
 /// <summary>
 /// Get the handle to an existing loading library by name.
 /// </summary>
 /// <param name="name">The name of the module.</param>
 /// <returns>The handle to the loaded library. Returns Null if not found.</returns>
 /// <remarks>This will take a reference on the library, you should dispose the handle after use.</remarks>
 public static SafeLoadLibraryHandle GetModuleHandleNoThrow(string name)
 {
     if (Win32NativeMethods.GetModuleHandleEx(0, name, out SafeLoadLibraryHandle ret))
     {
         return(ret);
     }
     return(Null);
 }
Exemple #2
0
 /// <summary>
 /// Get the handle to an existing loading library by name.
 /// </summary>
 /// <param name="name">The name of the module.</param>
 /// <returns>The handle to the loaded library.</returns>
 /// <exception cref="SafeWin32Exception">Thrown if the module can't be found.</exception>
 /// <remarks>This will take a reference on the library, you should dispose the handle after use.</remarks>
 public static SafeLoadLibraryHandle GetModuleHandle(string name)
 {
     if (Win32NativeMethods.GetModuleHandleEx(0, name, out SafeLoadLibraryHandle ret))
     {
         return(ret);
     }
     throw new SafeWin32Exception();
 }
Exemple #3
0
 /// <summary>
 /// Pin the library into memory. This prevents FreeLibrary unloading the library until
 /// the process exits.
 /// </summary>
 /// <param name="name">The name of the module to pin.</param>
 public static void PinModule(string name)
 {
     if (!Win32NativeMethods.GetModuleHandleEx(
             Win32NativeMethods.GET_MODULE_HANDLE_EX_FLAG_PIN,
             name, out _))
     {
         throw new SafeWin32Exception();
     }
 }
Exemple #4
0
 /// <summary>
 /// Get the handle to an existing loading library by an address in the module.
 /// </summary>
 /// <param name="address">An address inside the module.</param>
 /// <returns>The handle to the loaded library, null if the address isn't inside a valid module.</returns>
 /// <remarks>This will take a reference on the library, you should dispose the handle after use.</remarks>
 public static SafeLoadLibraryHandle GetModuleHandle(IntPtr address)
 {
     if (Win32NativeMethods.GetModuleHandleEx(Win32NativeMethods.GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
                                              address, out SafeLoadLibraryHandle ret))
     {
         return(ret);
     }
     return(null);
 }
Exemple #5
0
 /// <summary>
 /// Pin the library into memory. This prevents FreeLibrary unloading the library until
 /// the process exits.
 /// </summary>
 /// <param name="address">The address of the module to pin.</param>
 public static void PinModule(IntPtr address)
 {
     if (!Win32NativeMethods.GetModuleHandleEx(
             Win32NativeMethods.GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
             | Win32NativeMethods.GET_MODULE_HANDLE_EX_FLAG_PIN,
             address, out _))
     {
         throw new SafeWin32Exception();
     }
 }