问题
With dlopen
you can provide NULL
as the library name and get a handle that allows you to find a symbol in any of the loaded libraries:
If filename is a NULL pointer, then the returned handle is for the main program. When given to dlsym(), this handle causes a search for a symbol in the main program, followed by all shared libraries loaded at program startup, and then all shared libraries loaded by dlopen() with the flag RTLD_GLOBAL.
Can you do the same with GetProcAddress
? I want to search for the presence of a Windows API but different libraries are loaded in Windows 8.
I know what libraries are loaded by looking in the COFF headers, I guess I could loop through the handles there...
This is the code I'm currently using:
.hpp
#include <string>
#include <stdexcept>
/**
* @~english
* Looks up a Windows API function. Make sure you set @c _WIN32_WINNT so that the definition is available at compile
* time.
* @par Example
* @code
* # undef _WIN32_WINNT
* # define _WIN32_WINNT 0x600
* # include <system/inc/nt/windows.h>
* static const auto initialize_srw_lock_ptr = FunctionPtrLookup(InitializeSRWLock, "kernel32");
* @endcode
* @param function the function definition to lookup
* @retval nullptr the function did not exist on this version of Windows
* @returns a function pointer to invoke
*/
#define FunctionPtrLookup(function, library) \
FunctionLookup<decltype(function)>(#function, library)
/**
* @~english
* The return type of FunctionLookup
*/
typedef void(*FunctionLookupPtr)();
/**
* @~english
* Looks up a Windows API function.
* @param name the name of the function to find in the library
* @retval nullptr the function did not exist on this version of Windows
* @returns a function pointer to invoke
* @see FunctionPtrLookup
*/
FunctionLookupPtr FunctionLookup(const std::string& name, const std::string& library);
/// @copydoc FunctionLookup
template<typename Signature>
const Signature * FunctionLookup(const std::string& name, const std::string& library) {
return reinterpret_cast<const Signature*>(FunctionLookup(name, library));
}
.cpp
FunctionLookupPtr FunctionLookup(const std::string& name, const std::string& library) {
const auto wide_library = Utf8ToWide(library);
const auto lib = LoadLibraryW(wide_library.c_str());
if (!lib) {
return nullptr;
}
return reinterpret_cast<FunctionLookupPtr>(GetProcAddress(lib, name.c_str()));
}
Ideally, I'd want to remove the library
variable.
回答1:
You can use EnumProcessModules to enumerate all loaded modules for current process, use example from here: http://msdn.microsoft.com/en-us/library/ms682621%28v=vs.85%29.aspx, if you call PrintModules
with GetCurrentProcessId()
, it will enumerate all HMODULE handles (value is in hMods[i]
) for current process. You can use them with GetProcAddress to find your function.
You must be aware that its possible to find the same named functions in different dll-s, mostly you know dll name for WinAPI function.
来源:https://stackoverflow.com/questions/23437007/getprocaddress-with-all-loaded-libraries