From 12d1fcba0e0cc7d7b1b4b52686c7ef5fe6b2a6c8 Mon Sep 17 00:00:00 2001 From: dahall Date: Thu, 7 Oct 2021 17:35:56 -0600 Subject: [PATCH] Added GetModuleHandleEx overload for an address --- PInvoke/Kernel32/LibLoaderApi.cs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/PInvoke/Kernel32/LibLoaderApi.cs b/PInvoke/Kernel32/LibLoaderApi.cs index 97ce4ac5..19ea0d88 100644 --- a/PInvoke/Kernel32/LibLoaderApi.cs +++ b/PInvoke/Kernel32/LibLoaderApi.cs @@ -1289,7 +1289,7 @@ namespace Vanara.PInvoke // HMODULE WINAPI GetModuleHandle( _In_opt_ LPCTSTR lpModuleName); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683199(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winbase.h", MSDNShortId = "ms683199")] - public static extern HINSTANCE GetModuleHandle([Optional] string lpModuleName); + public static extern SafeHINSTANCE GetModuleHandle([Optional] string lpModuleName); /// /// Retrieves a module handle for the specified module and increments the module's reference count unless @@ -1324,7 +1324,42 @@ namespace Vanara.PInvoke [DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winbase.h", MSDNShortId = "ms683200")] [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG dwFlags, [Optional] string lpModuleName, out HINSTANCE phModule); + public static extern bool GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG dwFlags, [Optional] string lpModuleName, out SafeHINSTANCE phModule); + + /// + /// Retrieves a module handle for the specified module and increments the module's reference count unless + /// GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT is specified. The module must have been loaded by the calling process. + /// + /// + /// This parameter can be zero or one or more of the following values. If the module's reference count is incremented, the caller + /// must use the FreeLibrary function to decrement the reference count when the module handle is no longer needed. + /// + /// + /// The name of the loaded module (either a .dll or .exe file), or an address in the module (if dwFlags is GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS). + /// + /// For a module name, if the file name extension is omitted, the default library extension .dll is appended. The file name string + /// can include a trailing point character (.) to indicate that the module name has no extension. The string does not have to specify + /// a path. When specifying a path, be sure to use backslashes (\), not forward slashes (/). The name is compared (case + /// independently) to the names of modules currently mapped into the address space of the calling process. + /// + /// If this parameter is NULL, the function returns a handle to the file used to create the calling process (.exe file). + /// + /// + /// A handle to the specified module. If the function fails, this parameter is NULL. + /// + /// The GetModuleHandleEx function does not retrieve handles for modules that were loaded using the + /// LOAD_LIBRARY_AS_DATAFILE flag. For more information, see LoadLibraryEx. + /// + /// + /// + /// If the function succeeds, the return value is nonzero. + /// If the function fails, the return value is zero. To get extended error information, see GetLastError. + /// + // BOOL WINAPI GetModuleHandleEx( _In_ DWORD dwFlags, _In_opt_ LPCTSTR lpModuleName, _Out_ HMODULE *phModule); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683200(v=vs.85).aspx + [DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)] + [PInvokeData("Winbase.h", MSDNShortId = "ms683200")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG dwFlags, [In] IntPtr lpModuleName, out SafeHINSTANCE phModule); /// Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL). ///