带有所有已加载库的GetProcAddress

Mat*_*son 3 c c++ dll winapi

有了dlopen你能提供NULL的库名,并获得一个句柄,可以让你找到一个符号在任何加载的库:

如果filename是NULL指针,则返回的句柄用于主程序.当给予dlsym()时,此句柄会导致在主程序中搜索符号,然后搜索程序启动时加载的所有共享库,然后由dlopen()加载标记为RTLD_GLOBAL的所有共享库.

你可以这样做GetProcAddress吗?我想搜索Windows API的存在,但在Windows 8中加载了不同的库.

我知道通过查看COFF标头加载了哪些库,我想我可以在那里循环处理...

这是我目前正在使用的代码:

.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));
}
Run Code Online (Sandbox Code Playgroud)

.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()));
}
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想删除library变量.

mar*_*inj 7

您可以使用EnumProcessModules枚举当前进程的所有已加载模块,请使用此处的示例:http://msdn.microsoft.com/en-us/library/ms682621%28v=vs.85%29.aspx,如果您PrintModules使用GetCurrentProcessId(),它将枚举hMods[i]当前进程的所有HMODULE句柄(值为).您可以将它们与GetProcAddress一起使用来查找您的函数.

你必须意识到可以在不同的dll-s中找到相同的命名函数,大多数你知道WinAPI函数的dll名称.