1 // Written by Zoltan Csizmadia, zoltan_csizmadia@yahoo.com
2 // For companies(Austin,TX): If you would like to get my resume, send an email.
4 // The source is free, but if you want to use it, mention my name and e-mail
7 ///////////////////////////////////////////////////////////////////////////////
9 // WhoUses.cpp : Defines the entry point for the console application.
15 #include "SystemInfo.h"
17 LPCTSTR
GetFileNamePosition( LPCTSTR lpPath
)
19 LPCTSTR lpAct
= lpPath
+ _tcslen( lpPath
);
21 while ( lpAct
> lpPath
&& *lpAct
!= _T('\\') && *lpAct
!= _T('/') )
30 void WhoUsesModule( LPCTSTR lpFileName
, BOOL bFullPathCheck
)
34 SystemProcessInformation::SYSTEM_PROCESS_INFORMATION
* p
;
36 SystemProcessInformation pi
;
37 SystemModuleInformation mi
;
41 _tprintf( _T("SystemModulesInformation::Refresh() failed.\n") );
45 if ( mi
.m_ModuleInfos
.empty() )
47 _tprintf( _T("No module information\n") );
53 _tprintf( _T("%-6s %-20s %s\n"), _T("PID"), _T("Name"), _T("Path") );
54 _tprintf( _T("------------------------------------------------------------------\n") );
56 for (list
<SystemModuleInformation::MODULE_INFO
>::iterator iter2
= mi
.m_ModuleInfos
.begin(); iter2
!= mi
.m_ModuleInfos
.end(); iter2
++) {
57 SystemModuleInformation::MODULE_INFO
& m
= *iter2
;
60 bShow
= _tcsicmp( m
.FullPath
, lpFileName
) == 0;
62 bShow
= _tcsicmp( GetFileNamePosition(m
.FullPath
), lpFileName
) == 0;
66 p
= pi
.m_ProcessInfos
[m
.ProcessId
];
68 SystemInfoUtils::Unicode2string( &p
->usName
, processName
);
73 _tprintf( _T("0x%04X %-20s %s\n"),
81 void WhoUsesFile( LPCTSTR lpFileName
, BOOL bFullPathCheck
)
86 string deviceFileName
;
88 SystemProcessInformation::SYSTEM_PROCESS_INFORMATION
* p
;
89 SystemProcessInformation pi
;
90 SystemHandleInformation hi
;
94 if ( !SystemInfoUtils::GetDeviceFileName( lpFileName
, deviceFileName
) )
96 _tprintf( _T("GetDeviceFileName() failed.\n") );
101 hi
.SetFilter( _T("File"), TRUE
);
103 if ( hi
.m_HandleInfos
.empty() )
105 _tprintf( _T("No handle information\n") );
111 _tprintf( _T("%-6s %-20s %s\n"), _T("PID"), _T("Name"), _T("Path") );
112 _tprintf( _T("------------------------------------------------------\n") );
114 for (list
<SystemHandleInformation::SYSTEM_HANDLE
>::iterator iter
= hi
.m_HandleInfos
.begin(); iter
!= hi
.m_HandleInfos
.end(); iter
++) {
115 SystemHandleInformation::SYSTEM_HANDLE
& h
= *iter
;
117 p
= pi
.m_ProcessInfos
[h
.ProcessID
];
119 SystemInfoUtils::Unicode2string( &p
->usName
, processName
);
124 //NT4 Stupid thing if it is the services.exe and I call GetName :((
125 if ( INtDll::dwNTMajorVersion
== 4 && _tcsicmp( processName
.c_str(), _T("services.exe" ) ) == 0 )
128 hi
.GetName( (HANDLE
)h
.HandleNumber
, name
, (DWORD
)h
.ProcessID
);
130 if ( bFullPathCheck
)
131 bShow
= _tcsicmp( name
.c_str(), deviceFileName
.c_str() ) == 0;
133 bShow
= _tcsicmp( GetFileNamePosition(name
.c_str()), lpFileName
) == 0;
137 if ( !bFullPathCheck
)
140 SystemInfoUtils::GetFsFileName( name
.c_str(), fsFilePath
);
143 _tprintf( _T("0x%04X %-20s %s\n"),
146 !bFullPathCheck
? fsFilePath
.c_str() : lpFileName
);
151 void EnableDebugPriv( void )
154 LUID sedebugnameValue
;
155 TOKEN_PRIVILEGES tkp
;
157 // enable the SeDebugPrivilege
158 if ( ! OpenProcessToken( GetCurrentProcess(),
159 TOKEN_ADJUST_PRIVILEGES
| TOKEN_QUERY
, &hToken
) )
161 _tprintf( _T("OpenProcessToken() failed, Error = %d SeDebugPrivilege is not available.\n") , GetLastError() );
165 if ( ! LookupPrivilegeValue( NULL
, SE_DEBUG_NAME
, &sedebugnameValue
) )
167 _tprintf( _T("LookupPrivilegeValue() failed, Error = %d SeDebugPrivilege is not available.\n"), GetLastError() );
168 CloseHandle( hToken
);
172 tkp
.PrivilegeCount
= 1;
173 tkp
.Privileges
[0].Luid
= sedebugnameValue
;
174 tkp
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
176 if ( ! AdjustTokenPrivileges( hToken
, FALSE
, &tkp
, sizeof tkp
, NULL
, NULL
) )
177 _tprintf( _T("AdjustTokenPrivileges() failed, Error = %d SeDebugPrivilege is not available.\n"), GetLastError() );
179 CloseHandle( hToken
);
184 _tprintf( _T("WhoUses 1.0 for www.codeguru.com\n") );
185 _tprintf( _T("Written by Zoltan Csizmadia, zoltan_csizmadia@yahoo.com \n") );
186 _tprintf( _T("\n") );
187 _tprintf( _T("Usage: WhoUses.exe [-M] fileName\n") );
188 _tprintf( _T("\n") );
189 _tprintf( _T(" -M fileName is a module name ( EXE, DLL, ... )\n") );
190 _tprintf( _T(" fileName File name\n") );
191 _tprintf( _T("\n") );
192 _tprintf( _T("Examples:\n") );
193 _tprintf( _T("\n") );
194 _tprintf( _T(" WhoUses.exe -M kernel32.dll\n") );
195 _tprintf( _T(" WhoUses.exe -M c:\\test\\test.dll\n") );
196 _tprintf( _T(" WhoUses.exe yourTextFile.txt\n") );
197 _tprintf( _T(" WhoUses.exe c:\\pagefile.sys\n") );
198 _tprintf( _T(" WhoUses.exe Serial0\n") );
201 int _tmain(int argc
, TCHAR
* argv
[])
203 ULONG nonSwitchCount
= 0;
204 BOOL bModule
= FALSE
;
205 LPCTSTR lpPath
= NULL
;
206 BOOL bFullPathCheck
= FALSE
;
208 TCHAR lpFilePath
[_MAX_PATH
];
210 for ( int i
= 1; i
< argc
; i
++ )
212 if ( _tcsicmp( argv
[i
], _T("-h" ) ) == 0 || _tcsicmp( argv
[i
], _T("-?" ) ) == 0 )
218 if ( _tcsicmp( argv
[i
], _T("-m" ) ) == 0 || _tcsicmp( argv
[i
], _T("-m" ) ) == 0 )
224 if ( nonSwitchCount
!= 0 )
246 bFullPathCheck
= GetFileNamePosition( lpPath
) != lpPath
;
248 if ( bFullPathCheck
)
250 if ( GetFullPathName( lpPath
, _MAX_PATH
, lpFilePath
, NULL
) == 0 )
252 _tprintf( _T("GetFullPathName() failed. Error = %d\n"), GetLastError() );
257 _tcscpy( lpFilePath
, GetFileNamePosition( lpPath
) );
260 WhoUsesModule( lpFilePath
, bFullPathCheck
);
262 WhoUsesFile( lpFilePath
, bFullPathCheck
);