2000-08-25 03:03:12 +08:00
|
|
|
/* module_info.cc
|
|
|
|
|
2001-12-12 06:51:01 +08:00
|
|
|
Copyright 1999,2000,2001 Red Hat, Inc.
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
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>
|
|
|
|
#include <psapi.h>
|
|
|
|
|
|
|
|
static int psapi_loaded = 0;
|
|
|
|
static HMODULE psapi_module_handle = NULL;
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
typedef BOOL WINAPI (tf_EnumProcessModules) (HANDLE, HMODULE *, DWORD,
|
|
|
|
LPDWORD);
|
|
|
|
typedef BOOL WINAPI (tf_GetModuleInformation) (HANDLE, HMODULE, LPMODULEINFO,
|
|
|
|
DWORD);
|
|
|
|
typedef DWORD WINAPI (tf_GetModuleFileNameExA) (HANDLE, HMODULE, LPSTR,
|
|
|
|
DWORD);
|
2000-08-25 03:03:12 +08:00
|
|
|
|
|
|
|
static tf_EnumProcessModules *psapi_EnumProcessModules = NULL;
|
|
|
|
static tf_GetModuleInformation *psapi_GetModuleInformation = NULL;
|
|
|
|
static tf_GetModuleFileNameExA *psapi_GetModuleFileNameExA = NULL;
|
|
|
|
|
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 *
|
|
|
|
psapi_get_module_name (HANDLE hProcess, DWORD 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
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!psapi_loaded ||
|
|
|
|
psapi_EnumProcessModules == NULL ||
|
|
|
|
psapi_GetModuleInformation == NULL ||
|
|
|
|
psapi_GetModuleFileNameExA == NULL)
|
2000-08-25 03:03:12 +08:00
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (psapi_loaded)
|
|
|
|
goto failed;
|
2000-08-25 03:03:12 +08:00
|
|
|
psapi_loaded = 1;
|
2000-10-28 13:00:00 +08:00
|
|
|
psapi_module_handle = LoadLibrary ("psapi.dll");
|
|
|
|
if (!psapi_module_handle)
|
|
|
|
goto failed;
|
|
|
|
psapi_EnumProcessModules =
|
|
|
|
(tf_EnumProcessModules *) GetProcAddress (psapi_module_handle,
|
|
|
|
"EnumProcessModules");
|
|
|
|
psapi_GetModuleInformation =
|
|
|
|
(tf_GetModuleInformation *) GetProcAddress (psapi_module_handle,
|
|
|
|
"GetModuleInformation");
|
|
|
|
psapi_GetModuleFileNameExA =
|
|
|
|
(tf_GetModuleFileNameExA *) GetProcAddress (psapi_module_handle,
|
|
|
|
"GetModuleFileNameExA");
|
|
|
|
if (psapi_EnumProcessModules == NULL
|
|
|
|
|| psapi_GetModuleInformation == NULL
|
|
|
|
|| psapi_GetModuleFileNameExA == NULL)
|
|
|
|
goto failed;
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
|
2000-10-28 13:00:00 +08:00
|
|
|
ok = (*psapi_EnumProcessModules) (hProcess,
|
|
|
|
DllHandle, sizeof (HMODULE), &cbNeeded);
|
|
|
|
|
|
|
|
if (!ok || !cbNeeded)
|
|
|
|
goto failed;
|
|
|
|
DllHandle = (HMODULE *) malloc (cbNeeded);
|
|
|
|
if (!DllHandle)
|
|
|
|
goto failed;
|
|
|
|
ok = (*psapi_EnumProcessModules) (hProcess, DllHandle, cbNeeded, &cbNeeded);
|
|
|
|
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
|
|
|
{
|
2000-10-28 13:00:00 +08:00
|
|
|
if (!(*psapi_GetModuleInformation) (hProcess,
|
|
|
|
DllHandle[i], &mi, sizeof (mi)))
|
|
|
|
{
|
|
|
|
free (DllHandle);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = (*psapi_GetModuleFileNameExA) (hProcess,
|
|
|
|
DllHandle[i], name_buf, MAX_PATH);
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
free (DllHandle);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((DWORD) (mi.lpBaseOfDll) == BaseAddress)
|
|
|
|
{
|
|
|
|
free (DllHandle);
|
|
|
|
return strdup (name_buf);
|
|
|
|
}
|
2000-08-25 03:03:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
failed:
|
|
|
|
return NULL;
|
|
|
|
}
|