]> sourceware.org Git - newlib-cygwin.git/blob - winsup/utils/module_info.cc
Cygwin: add 3.2.1 release file and add fixes up to this point
[newlib-cygwin.git] / winsup / utils / module_info.cc
1 /* module_info.cc
2
3 Written by Egor Duda <deo@logos-m.ru>
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include <stdlib.h>
12 #include <windows.h>
13 #include <psapi.h>
14 #include "loadlib.h"
15
16 /* Returns full name of Dll, which is loaded by hProcess at BaseAddress.
17 Uses psapi.dll. */
18
19 char *
20 psapi_get_module_name (HANDLE hProcess, LPVOID BaseAddress)
21 {
22 DWORD len;
23 MODULEINFO mi;
24 unsigned int i;
25 HMODULE dh_buf[1];
26 HMODULE *DllHandle = dh_buf;
27 DWORD cbNeeded;
28 BOOL ok;
29
30 char name_buf[MAX_PATH + 1];
31
32 ok = EnumProcessModules (hProcess, DllHandle, sizeof (HMODULE), &cbNeeded);
33
34 if (!ok || !cbNeeded)
35 goto failed;
36 DllHandle = (HMODULE *) malloc (cbNeeded);
37 if (!DllHandle)
38 goto failed;
39 ok = EnumProcessModules (hProcess, DllHandle, cbNeeded, &cbNeeded);
40 if (!ok)
41 {
42 free (DllHandle);
43 goto failed;
44 }
45
46 for (i = 0; i < cbNeeded / sizeof (HMODULE); i++)
47 {
48 if (!GetModuleInformation (hProcess, DllHandle[i], &mi, sizeof (mi)))
49 {
50 free (DllHandle);
51 goto failed;
52 }
53
54 len = GetModuleFileNameExA (hProcess, DllHandle[i], name_buf, MAX_PATH);
55 if (len == 0)
56 {
57 free (DllHandle);
58 goto failed;
59 }
60
61 if (mi.lpBaseOfDll == BaseAddress)
62 {
63 free (DllHandle);
64 return strdup (name_buf);
65 }
66 }
67
68 failed:
69 return NULL;
70 }
This page took 0.038535 seconds and 5 git commands to generate.