-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllcheck.cpp
95 lines (78 loc) · 1.96 KB
/
dllcheck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <windows.h>
#include <tchar.h>
#include <string.h>
#include <stdio.h>
#include <psapi.h>
// To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
// and compile with -DPSAPI_VERSION=1
const wchar_t *GetWC(const char *c)
{
const size_t cSize = strlen(c) + 1;
wchar_t* wc = new wchar_t[cSize];
mbstowcs(wc, c, cSize);
return wc;
}
int PrintModules(DWORD processID, char *searchDll)
{
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;
// Get a handle to the process.
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID);
if (NULL == hProcess) {
return 0;
}
// Get a list of all the modules in this process.
if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
{
TCHAR szModName[MAX_PATH];
// Get the full path to the module's file.
if (GetModuleFileNameEx(hProcess, hMods[i], szModName,
sizeof(szModName) / sizeof(TCHAR)))
{
// Print the module name and handle value.
const wchar_t *wsdll = GetWC(searchDll);
if (wcsstr(szModName, wsdll) != NULL) {
printf("Process ID: %u\n", processID);
printf("Search DLL: %s\n", searchDll);
_tprintf(TEXT("\t%s (0x%08X)\n"), szModName, hMods[i]);
delete[] wsdll;
return 1;
}
delete[] wsdll;
}
}
}
// Release the handle to the process.
CloseHandle(hProcess);
return 0;
}
int main(int argc, char* argv[])
{
if (argc < 2) {
return 1;
}
DWORD aProcesses[1024];
DWORD cbNeeded;
DWORD cProcesses;
unsigned int i;
// Get the list of process identifiers.
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
return 0;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the names of the modules for each process.
for (i = 0; i < cProcesses; i++)
{
int ret = PrintModules(aProcesses[i], argv[1]);
if (ret != 0) {
return ret;
}
}
return 0;
}