forked from git-for-windows/build-extra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhoUses.cpp
More file actions
265 lines (219 loc) · 6.5 KB
/
WhoUses.cpp
File metadata and controls
265 lines (219 loc) · 6.5 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Written by Zoltan Csizmadia, zoltan_csizmadia@yahoo.com
// For companies(Austin,TX): If you would like to get my resume, send an email.
//
// The source is free, but if you want to use it, mention my name and e-mail
// address
//
///////////////////////////////////////////////////////////////////////////////
//
// WhoUses.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "SystemInfo.h"
LPCTSTR GetFileNamePosition( LPCTSTR lpPath )
{
LPCTSTR lpAct = lpPath + _tcslen( lpPath );
while ( lpAct > lpPath && *lpAct != _T('\\') && *lpAct != _T('/') )
lpAct--;
if ( lpAct > lpPath )
lpAct++;
return lpAct;
}
void WhoUsesModule( LPCTSTR lpFileName, BOOL bFullPathCheck )
{
string processName;
BOOL bShow = FALSE;
SystemProcessInformation::SYSTEM_PROCESS_INFORMATION* p;
SystemProcessInformation pi;
SystemModuleInformation mi;
if ( !mi.Refresh() )
{
_tprintf( _T("SystemModulesInformation::Refresh() failed.\n") );
return;
}
if ( mi.m_ModuleInfos.empty() )
{
_tprintf( _T("No module information\n") );
return;
}
pi.Refresh();
_tprintf( _T("%-6s %-20s %s\n"), _T("PID"), _T("Name"), _T("Path") );
_tprintf( _T("------------------------------------------------------------------\n") );
for (list<SystemModuleInformation::MODULE_INFO>::iterator iter2 = mi.m_ModuleInfos.begin(); iter2 != mi.m_ModuleInfos.end(); iter2++) {
SystemModuleInformation::MODULE_INFO& m = *iter2;
if ( bFullPathCheck )
bShow = _tcsicmp( m.FullPath, lpFileName ) == 0;
else
bShow = _tcsicmp( GetFileNamePosition(m.FullPath), lpFileName ) == 0;
if ( bShow )
{
p = pi.m_ProcessInfos[m.ProcessId];
if (p) {
SystemInfoUtils::Unicode2string( &p->usName, processName );
}
else
processName = "";
_tprintf( _T("0x%04X %-20s %s\n"),
(unsigned int)m.ProcessId,
processName.c_str(),
m.FullPath );
}
}
}
void WhoUsesFile( LPCTSTR lpFileName, BOOL bFullPathCheck )
{
BOOL bShow = FALSE;
string name;
string processName;
string deviceFileName;
string fsFilePath;
SystemProcessInformation::SYSTEM_PROCESS_INFORMATION* p;
SystemProcessInformation pi;
SystemHandleInformation hi;
if ( bFullPathCheck )
{
if ( !SystemInfoUtils::GetDeviceFileName( lpFileName, deviceFileName ) )
{
_tprintf( _T("GetDeviceFileName() failed.\n") );
return;
}
}
hi.SetFilter( _T("File"), TRUE );
if ( hi.m_HandleInfos.empty() )
{
_tprintf( _T("No handle information\n") );
return;
}
pi.Refresh();
_tprintf( _T("%-6s %-20s %s\n"), _T("PID"), _T("Name"), _T("Path") );
_tprintf( _T("------------------------------------------------------\n") );
for (list<SystemHandleInformation::SYSTEM_HANDLE>::iterator iter = hi.m_HandleInfos.begin(); iter != hi.m_HandleInfos.end(); iter++) {
SystemHandleInformation::SYSTEM_HANDLE& h = *iter;
p = pi.m_ProcessInfos[h.ProcessID];
if (p) {
SystemInfoUtils::Unicode2string( &p->usName, processName );
}
else
processName = "";
//NT4 Stupid thing if it is the services.exe and I call GetName :((
if ( INtDll::dwNTMajorVersion == 4 && _tcsicmp( processName.c_str(), _T("services.exe" ) ) == 0 )
continue;
hi.GetName( (HANDLE)(DWORD_PTR)h.HandleNumber, name, (DWORD)h.ProcessID );
if ( bFullPathCheck )
bShow = _tcsicmp( name.c_str(), deviceFileName.c_str() ) == 0;
else
bShow = _tcsicmp( GetFileNamePosition(name.c_str()), lpFileName ) == 0;
if ( bShow )
{
if ( !bFullPathCheck )
{
fsFilePath = "";
SystemInfoUtils::GetFsFileName( name.c_str(), fsFilePath );
}
_tprintf( _T("0x%04X %-20s %s\n"),
(unsigned int)h.ProcessID,
processName.c_str(),
!bFullPathCheck ? fsFilePath.c_str() : lpFileName );
}
}
}
void EnableDebugPriv( void )
{
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tkp;
// enable the SeDebugPrivilege
if ( ! OpenProcessToken( GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
{
_tprintf( _T("OpenProcessToken() failed, Error = %d SeDebugPrivilege is not available.\n") , (int)GetLastError() );
return;
}
if ( ! LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue ) )
{
_tprintf( _T("LookupPrivilegeValue() failed, Error = %d SeDebugPrivilege is not available.\n"), (int)GetLastError() );
CloseHandle( hToken );
return;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = sedebugnameValue;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) )
_tprintf( _T("AdjustTokenPrivileges() failed, Error = %d SeDebugPrivilege is not available.\n"), (int)GetLastError() );
CloseHandle( hToken );
}
void ShowUsage()
{
_tprintf( _T("WhoUses 1.0 for www.codeguru.com\n") );
_tprintf( _T("Written by Zoltan Csizmadia, zoltan_csizmadia@yahoo.com \n") );
_tprintf( _T("\n") );
_tprintf( _T("Usage: WhoUses.exe [-M] fileName\n") );
_tprintf( _T("\n") );
_tprintf( _T(" -M fileName is a module name ( EXE, DLL, ... )\n") );
_tprintf( _T(" fileName File name\n") );
_tprintf( _T("\n") );
_tprintf( _T("Examples:\n") );
_tprintf( _T("\n") );
_tprintf( _T(" WhoUses.exe -M kernel32.dll\n") );
_tprintf( _T(" WhoUses.exe -M c:\\test\\test.dll\n") );
_tprintf( _T(" WhoUses.exe yourTextFile.txt\n") );
_tprintf( _T(" WhoUses.exe c:\\pagefile.sys\n") );
_tprintf( _T(" WhoUses.exe Serial0\n") );
}
int _tmain(int argc, TCHAR* argv[])
{
ULONG nonSwitchCount = 0;
BOOL bModule = FALSE;
LPCTSTR lpPath = NULL;
BOOL bFullPathCheck = FALSE;
BOOL bUsage = TRUE;
TCHAR lpFilePath[_MAX_PATH];
for ( int i = 1; i < argc; i++ )
{
if ( _tcsicmp( argv[i], _T("-h" ) ) == 0 || _tcsicmp( argv[i], _T("-?" ) ) == 0 )
{
bUsage = TRUE;
break;
}
else
if ( _tcsicmp( argv[i], _T("-m" ) ) == 0 || _tcsicmp( argv[i], _T("-m" ) ) == 0 )
{
bModule = TRUE;
}
else
{
if ( nonSwitchCount != 0 )
{
bUsage = TRUE;
break;
}
lpPath = argv[i];
bUsage = FALSE;
nonSwitchCount++;
}
}
if ( bUsage )
{
ShowUsage();
return -1;
}
EnableDebugPriv();
bFullPathCheck = GetFileNamePosition( lpPath ) != lpPath ;
if ( bFullPathCheck )
{
if ( GetFullPathName( lpPath, _MAX_PATH, lpFilePath, NULL ) == 0 )
{
_tprintf( _T("GetFullPathName() failed. Error = %d\n"), (int)GetLastError() );
return -2;
}
}
else
_tcscpy( lpFilePath, GetFileNamePosition( lpPath ) );
if ( bModule )
WhoUsesModule( lpFilePath, bFullPathCheck );
else
WhoUsesFile( lpFilePath, bFullPathCheck );
return 0;
}