23 #include "usModuleUtils_p.h"
27 #include <usUtils_p.h>
32 #ifdef US_BUILD_SHARED_LIBS
33 const bool sharedLibMode =
true;
35 const bool sharedLibMode =
false;
50 if (dladdr(symbol, &info))
52 return info.dli_fname;
56 US_DEBUG <<
"GetLibraryPath_impl() failed for address " << symbol;
61 void*
GetSymbol_impl(
const ModuleInfo& moduleInfo,
const char* symbol)
67 if (!sharedLibMode || moduleInfo.name ==
"main")
70 selfHandle = dlopen(0, RTLD_LAZY);
74 selfHandle = dlopen(moduleInfo.location.c_str(), RTLD_LAZY);
79 void* addr = dlsym(selfHandle, symbol);
82 const char* dlerrorMsg = dlerror();
85 US_DEBUG <<
"GetSymbol_impl() failed: " << dlerrorMsg;
93 US_DEBUG <<
"GetSymbol_impl() dlopen() failed: " << dlerror();
105 BOOL handleError = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
106 static_cast<LPCTSTR>(symbol), &handle);
110 US_DEBUG <<
"GetLibraryPath_impl():GetModuleHandle() " <<
GetLastErrorStr();
114 char modulePath[512];
115 if (GetModuleFileName(handle, modulePath, 512))
120 US_DEBUG <<
"GetLibraryPath_impl():GetModuleFileName() " <<
GetLastErrorStr();
124 void*
GetSymbol_impl(
const ModuleInfo& moduleInfo,
const char* symbol)
126 HMODULE handle = NULL;
127 if (!sharedLibMode || moduleInfo.name ==
"main")
129 handle = GetModuleHandle(NULL);
133 handle = GetModuleHandle(moduleInfo.location.c_str());
138 US_DEBUG <<
"GetSymbol_impl():GetModuleHandle() " <<
GetLastErrorStr();
142 void* addr = (
void*)GetProcAddress(handle, symbol);
145 US_DEBUG <<
"GetSymbol_impl():GetProcAddress(handle," << symbol <<
") " <<
GetLastErrorStr();
161 std::string ModuleUtils::GetLibraryPath(
void* symbol)
166 void* ModuleUtils::GetSymbol(
const ModuleInfo& module,
const char* symbol)
static void info(const char *fmt,...)
static std::string GetLastErrorStr()
std::string GetLibraryPath_impl(void *)
void * GetSymbol_impl(const ModuleInfo &, const char *symbol)