MCPcopy Create free account
hub / github.com/EmmyLua/EmmyLuaDebugger / peLocateModule

Function peLocateModule

emmy_hook/src/libpe/libpe.cpp:328–400  ·  view source on GitHub ↗

* With both implicit and explicit linking, Windows first searches for "known DLLs", such as Kernel32.dll and User32.dll. * Windows then searches for the DLLs in the following sequence: * * - The directory where the executable module for the current process is located. * - The current directory. * - The Windows system directory. The GetSystemDirectory function retrieves the path of this direct

Source from the content-addressed store, hash-verified

326 * - The directories listed in the PATH environment variable.
327 */
328PE_STATUS peLocateModule( PE *exe, PE_IMPORT_MODULE *pModule, PE *peModule )
329{
330 PE_STATUS status = PE_NOT_FOUND;
331 char szExeFilePath[MAX_PATH + 1] = {0},
332 szModuleFilePath[MAX_PATH + 1] = {0},
333 szSystemDirectory[MAX_PATH + 1] = {0},
334 szWindowsDirectory[MAX_PATH + 1] = {0},
335 *pszPaths = NULL,
336 *pszPointer = NULL;
337
338 if( exe->szFileName[0] != '[' )
339 {
340 GetFullPathName( exe->szFileName, MAX_PATH, szExeFilePath, NULL );
341
342 pszPointer = strrchr( szExeFilePath, '\\' );
343 if( pszPointer )
344 *pszPointer = '\0';
345 }
346
347 sprintf_s( szModuleFilePath, "%s\\%s", szExeFilePath, pModule->Name );
348 if( peFileExists( szModuleFilePath ) )
349 {
350 return peOpenFile( peModule, szModuleFilePath );
351 }
352
353 GetSystemDirectory( szSystemDirectory, MAX_PATH );
354 sprintf_s( szModuleFilePath, "%s\\%s", szSystemDirectory, pModule->Name );
355 if( peFileExists( szModuleFilePath ) )
356 {
357 return peOpenFile( peModule, szModuleFilePath );
358 }
359
360 GetWindowsDirectory( szWindowsDirectory, MAX_PATH );
361 sprintf_s( szModuleFilePath, "%s\\%s", szWindowsDirectory, pModule->Name );
362 if( peFileExists( szModuleFilePath ) )
363 {
364 return peOpenFile( peModule, szModuleFilePath );
365 }
366
367 size_t len;
368 errno_t err = _dupenv_s( &pszPaths, &len, "PATH" );
369 if( err == 0 && pszPaths )
370 {
371 char *pszCurrentPath = pszPaths;
372 for(;;)
373 {
374 pszPointer = strchr( pszCurrentPath, ';' );
375 if( pszPointer )
376 {
377 *pszPointer = '\0';
378
379 sprintf_s( szModuleFilePath, "%s\\%s", pszCurrentPath, pModule->Name );
380 if( peFileExists( szModuleFilePath ) )
381 {
382 free( pszPaths );
383
384 return peOpenFile( peModule, szModuleFilePath );
385 }

Callers 1

PE_FOREACH_MODULE_SYMBOLFunction · 0.85

Calls 2

peFileExistsFunction · 0.85
peOpenFileFunction · 0.85

Tested by

no test coverage detected