2000-08-24 21:03:12 +02:00
|
|
|
/* module_info.cc
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
Copyright 1999, 2000, 2001, 2010, 2013 Red Hat, Inc.
|
2000-08-24 21:03:12 +02: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>
|
2010-08-28 13:22:37 +02:00
|
|
|
#include "loadlib.h"
|
2000-08-24 21:03:12 +02:00
|
|
|
|
2000-10-28 07:00:00 +02:00
|
|
|
/* Returns full name of Dll, which is loaded by hProcess at BaseAddress.
|
|
|
|
Uses psapi.dll. */
|
2000-08-24 21:03:12 +02:00
|
|
|
|
2000-10-28 07:00:00 +02:00
|
|
|
char *
|
2013-04-23 11:44:36 +02:00
|
|
|
psapi_get_module_name (HANDLE hProcess, LPVOID BaseAddress)
|
2000-08-24 21:03:12 +02:00
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
MODULEINFO mi;
|
|
|
|
unsigned int i;
|
2000-10-28 07:00:00 +02:00
|
|
|
HMODULE dh_buf[1];
|
|
|
|
HMODULE *DllHandle = dh_buf;
|
2000-08-24 21:03:12 +02:00
|
|
|
DWORD cbNeeded;
|
|
|
|
BOOL ok;
|
|
|
|
|
2000-10-28 07:00:00 +02:00
|
|
|
char name_buf[MAX_PATH + 1];
|
2000-08-24 21:03:12 +02:00
|
|
|
|
2013-11-19 12:14:36 +01:00
|
|
|
ok = EnumProcessModules (hProcess, DllHandle, sizeof (HMODULE), &cbNeeded);
|
2000-10-28 07:00:00 +02:00
|
|
|
|
|
|
|
if (!ok || !cbNeeded)
|
|
|
|
goto failed;
|
|
|
|
DllHandle = (HMODULE *) malloc (cbNeeded);
|
|
|
|
if (!DllHandle)
|
|
|
|
goto failed;
|
2013-11-19 12:14:36 +01:00
|
|
|
ok = EnumProcessModules (hProcess, DllHandle, cbNeeded, &cbNeeded);
|
2000-10-28 07:00:00 +02:00
|
|
|
if (!ok)
|
2000-08-24 21:03:12 +02:00
|
|
|
{
|
2000-10-28 07:00:00 +02:00
|
|
|
free (DllHandle);
|
2000-08-24 21:03:12 +02:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2000-10-28 07:00:00 +02:00
|
|
|
for (i = 0; i < cbNeeded / sizeof (HMODULE); i++)
|
2000-08-24 21:03:12 +02:00
|
|
|
{
|
2013-11-19 12:14:36 +01:00
|
|
|
if (!GetModuleInformation (hProcess, DllHandle[i], &mi, sizeof (mi)))
|
2000-10-28 07:00:00 +02:00
|
|
|
{
|
|
|
|
free (DllHandle);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2013-11-19 12:14:36 +01:00
|
|
|
len = GetModuleFileNameExA (hProcess, DllHandle[i], name_buf, MAX_PATH);
|
2000-10-28 07:00:00 +02:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
free (DllHandle);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
if (mi.lpBaseOfDll == BaseAddress)
|
2000-10-28 07:00:00 +02:00
|
|
|
{
|
|
|
|
free (DllHandle);
|
|
|
|
return strdup (name_buf);
|
|
|
|
}
|
2000-08-24 21:03:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
failed:
|
|
|
|
return NULL;
|
|
|
|
}
|