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