13 #define SYSTEM_MODULEINFO_CPP 27 #define strcasecmp _stricmp 28 #define strncasecmp _strnicmp 32 static PsApiFunctions _psApi;
33 #define getpid _getpid 39 #else // UNIX...: first the EGCS stuff, then the OS dependent includes 41 #include "sys/param.h" 42 #include "sys/times.h" 66 #elif defined( __linux ) || defined( __APPLE__ ) 83 char name[PATH_MAX] = {
"Unknown.module"};
92 #if defined( __linux ) || defined( __APPLE__ ) 97 if (::realpath( path, name ) ) module = name;
110 int loc = module.
rfind(
'.' ) + 1;
113 else if ( module[loc] ==
'e' || module[loc] ==
'E' )
116 else if ( module[loc] ==
'd' || module[loc] ==
'D' )
118 else if ( module[loc] ==
's' && module[loc + 1] ==
'o' )
130 static long pid = ::getpid();
132 static HANDLE hP = ::OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pid );
134 static void* hP = (
void*)pid;
143 if ( !ModuleHandle ) {
146 static HINSTANCE handle = 0;
148 if ( 0 == handle && _psApi ) {
149 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof( ModuleHandle ), &cbNeeded ) ) {
153 #elif defined( __linux ) || defined( __APPLE__ ) 168 static HINSTANCE handle = 0;
170 if ( 0 == handle && _psApi ) {
171 if ( _psApi.EnumProcessModules(
processHandle(), &handle,
sizeof( ModuleHandle ), &cbNeeded ) ) {
177 #elif defined( __linux ) || defined( __APPLE__ ) 179 static Dl_info infoBuf, *info = &infoBuf;
181 void* handle = ::dlopen(
nullptr, RTLD_LAZY );
184 void*
func = ::dlsym( handle,
"main" );
187 if ( 0 != ::dladdr( func, &infoBuf ) ) {
204 if ( module.
length() == 0 ) {
205 char name[PATH_MAX] = {
"Unknown.module"};
212 #elif defined( __linux ) || defined( __APPLE__ ) 214 ::sprintf( cmd,
"/proc/%d/exe", ::getpid() );
216 if (::readlink( cmd, name,
sizeof( name ) ) >= 0 ) module =
name;
218 if (::realpath( ( (HMODULE*)
exeHandle() )->dsc.filename, name ) ) module =
name;
226 if ( s_linkedModules.
size() == 0 ) {
230 HINSTANCE handle[1024];
232 if ( _psApi.EnumProcessModules(
processHandle(), handle,
sizeof( handle ), &cbNeeded ) ) {
233 for (
size_t i = 0; i < cbNeeded /
sizeof( HANDLE ); i++ ) {
234 if ( 0 < _psApi.GetModuleFileNameExA(
processHandle(), handle[i], name,
sizeof( name ) ) ) {
240 #elif defined( __linux ) || defined( __APPLE__ ) 241 char ff[512], cmd[1024], fname[1024], buf1[64], buf2[64], buf3[64], buf4[64];
242 ::sprintf( ff,
"/proc/%d/maps", ::getpid() );
243 FILE*
maps = ::fopen( ff,
"r" );
244 while (::fgets( cmd,
sizeof( cmd ), maps ) ) {
246 sscanf( cmd,
"%s %s %s %s %d %s", buf1, buf2, buf3, buf4, &len, fname );
247 if ( len > 0 && strncmp( buf2,
"r-xp", strlen(
"r-xp" ) ) == 0 ) {
254 return s_linkedModules;
GAUDI_API ModuleType moduleType()
Get type of the module.
GAUDI_API void setModuleHandle(ImageHandle handle)
Attach module handle.
GAUDI_API const std::string & moduleNameFull()
Get the full name of the (executable/DLL) file.
GAUDI_API ProcessHandle processHandle()
Handle to running process.
void * ImageHandle
Definition of an image handle.
GAUDI_API const std::string & exeName()
Name of the executable file running.
GAUDI_API ImageHandle moduleHandle()
Handle to currently executed module.
GAUDI_API ImageHandle exeHandle()
Handle to the executable file running.
GAUDI_API const std::vector< std::string > linkedModules()
Vector of names of linked modules.
GAUDI_API const std::string & moduleName()
Get the name of the (executable/DLL) file without file-type.