2000-08-25 03:03:12 +08:00
|
|
|
/* module_info.cc
|
|
|
|
|
|
|
|
Written by Egor Duda <deo@logos-m.ru>
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <windows.h>
|
2021-05-21 01:28:01 +08:00
|
|
|
#define PSAPI_VERSION 1
|
2000-08-25 03:03:12 +08:00
|
|
|
#include <psapi.h>
|
2010-08-28 19:22:37 +08:00
|
|
|
#include "loadlib.h"
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
/* Returns full name of Dll, which is loaded by hProcess at BaseAddress.
|
|
|
|
Uses psapi.dll. */
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
char *
|
2013-04-23 17:44:36 +08:00
|
|
|
psapi_get_module_name (HANDLE hProcess, LPVOID BaseAddress)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
MODULEINFO mi;
|
|
|
|
unsigned int i;
|
2000-10-28 13:00:00 +08:00
|
|
|
HMODULE dh_buf[1];
|
|
|
|
HMODULE *DllHandle = dh_buf;
|
2000-08-25 03:03:12 +08:00
|
|
|
DWORD cbNeeded;
|
|
|
|
BOOL ok;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
char name_buf[MAX_PATH + 1];
|
2000-08-25 03:03:12 +08:00
|
|
|
|
2013-11-19 19:14:36 +08:00
|
|
|
ok = EnumProcessModules (hProcess, DllHandle, sizeof (HMODULE), &cbNeeded);
|
2000-10-28 13:00:00 +08:00
|
|
|
|
|
|
|
if (!ok || !cbNeeded)
|
|
|
|
goto failed;
|
|
|
|
DllHandle = (HMODULE *) malloc (cbNeeded);
|
|
|
|
if (!DllHandle)
|
|
|
|
goto failed;
|
2013-11-19 19:14:36 +08:00
|
|
|
ok = EnumProcessModules (hProcess, DllHandle, cbNeeded, &cbNeeded);
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!ok)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
free (DllHandle);
|
2000-08-25 03:03:12 +08:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
for (i = 0; i < cbNeeded / sizeof (HMODULE); i++)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2013-11-19 19:14:36 +08:00
|
|
|
if (!GetModuleInformation (hProcess, DllHandle[i], &mi, sizeof (mi)))
|
2000-10-28 13:00:00 +08:00
|
|
|
{
|
|
|
|
free (DllHandle);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2013-11-19 19:14:36 +08:00
|
|
|
len = GetModuleFileNameExA (hProcess, DllHandle[i], name_buf, MAX_PATH);
|
2000-10-28 13:00:00 +08:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
free (DllHandle);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
if (mi.lpBaseOfDll == BaseAddress)
|
2000-10-28 13:00:00 +08:00
|
|
|
{
|
|
|
|
free (DllHandle);
|
|
|
|
return strdup (name_buf);
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
failed:
|
|
|
|
return NULL;
|
|
|
|
}
|