14 #define SYSTEM_MODULEINFO_CPP 25 # define strcasecmp _stricmp 26 # define strncasecmp _strnicmp 30 static PsApiFunctions _psApi;
31 #define getpid _getpid 32 #else // UNIX...: first the EGCS stuff, then the OS dependent includes 35 #include "sys/times.h" 36 #include "sys/param.h" 48 static std::string module(
"");
54 if ( _psApi.isValid() ) {
58 #elif defined(__linux__) || defined(linux) 59 std::string mod = ::basename((
char*)((Dl_info*)
moduleHandle())->dli_fname);
61 std::string mod = ::basename(((HMODULE*)
moduleHandle())->dsc.filename);
63 module = mod.substr(0, mod.rfind(
'.'));
71 static std::string module(
"");
74 char name[512] = {
"Unknown.module"};
77 if ( _psApi.isValid() ) {
80 #elif defined(linux) || defined(__APPLE__) 83 ::realpath(((HMODULE*)
moduleHandle())->dsc.filename, name);
96 int loc = module.rfind(
'.')+1;
99 else if ( module[loc] ==
'e' || module[loc] ==
'E' )
102 else if ( module[loc] ==
'd' || module[loc] ==
'D' )
104 else if ( module[loc] ==
's' && module[loc+1] ==
'o' )
115 static long pid = ::getpid();
117 static HANDLE hP = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,
FALSE,pid);
119 static void* hP = (
void*)pid;
132 static HINSTANCE handle = 0;
134 if ( 0 == handle && _psApi.isValid() ) {
139 #elif defined(linux) || defined(__APPLE__) 155 static HINSTANCE handle = 0;
157 if ( 0 == handle && _psApi.isValid() ) {
163 #elif defined(linux) || defined(__APPLE__) 165 static Dl_info infoBuf, *info = &infoBuf;
167 void* handle = ::dlopen(0, RTLD_LAZY);
170 void* func = ::dlsym(handle,
"main");
173 if ( 0 != ::dladdr(func, &infoBuf) ) {
188 static std::string module(
"");
189 if ( module.length() == 0 ) {
190 char name[512] = {
"Unknown.module"};
196 #elif defined(linux) || defined(__APPLE__) 198 ::sprintf(cmd,
"/proc/%d/exe", ::getpid());
200 ::readlink(cmd, name,
sizeof(name));
202 ::realpath(((HMODULE*)
exeHandle())->dsc.filename, name);
214 HINSTANCE handle[1024];
215 if ( _psApi.isValid() ) {
216 if ( _psApi.EnumProcessModules(
processHandle(),handle,
sizeof(handle),&cbNeeded) ) {
217 for (
size_t i = 0; i < cbNeeded/
sizeof(
HANDLE); i++ ) {
218 if ( 0 < _psApi.GetModuleFileNameExA(
processHandle(), handle[i], name,
sizeof(name)) ) {
224 #elif defined(linux) || defined(__APPLE__) 225 char ff[512], cmd[1024], fname[1024], buf1[64], buf2[64], buf3[64], buf4[64];
226 ::sprintf(ff,
"/proc/%d/maps", ::getpid());
227 FILE* maps = ::fopen(ff,
"r");
228 while( ::fgets(cmd,
sizeof(cmd), maps) ) {
230 sscanf(cmd,
"%s %s %s %s %d %s", buf1, buf2, buf3, buf4, &len, fname);
231 if ( len > 0 && strncmp(buf2,
"r-xp",strlen(
"r-xp")) == 0 ) {
void setModuleHandle(ImageHandle handle)
Attach module handle.
const ModuleType moduleType()
Get type of the module.
const std::string & moduleNameFull()
Get the full name of the (executable/DLL) file.
const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.
ImageHandle exeHandle()
Handle to the executable file running.
ImageHandle moduleHandle()
Handle to currently executed module.
ProcessHandle processHandle()
Handle to running process.
void * ImageHandle
Definition of an image handle.
const std::vector< std::string > linkedModules()
Vector of names of linked modules.
const std::string & exeName()
Name of the executable file running.
static System::ImageHandle ModuleHandle
static std::vector< std::string > s_linkedModules