/[ProcessMemoryDumper]/branches/pmd_18_x86/ProcListP.cpp
ViewVC logotype

Annotation of /branches/pmd_18_x86/ProcListP.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (hide annotations) (download)
Fri Aug 31 04:08:07 2012 UTC (7 years, 11 months ago) by william
File size: 11812 byte(s)
commit upstream branch code
1 william 3 #include "stdafx.h"
2     #include "psapi.h"
3     #include "ProcListP.h"
4    
5     CEnumProcessList::CEnumProcessList()
6     :m_hProcess(0),m_dwError(0)
7     {
8     CModuleDetails *pThisModule = NULL;
9     DWORD dwNeeded = 0;
10     DWORD dwSize = 0;
11     DWORD dwProcId[MAXCOUNT] = { 0 };
12     TCHAR szName[MAX_PATH] = { 0 };
13     EnumProcesses( dwProcId, MAXCOUNT, &dwSize );
14     if( !dwSize )
15     {
16     return;
17     }
18    
19     for( DWORD dwIndex = 0; dwIndex < dwSize; ++dwIndex )
20     {
21     HANDLE hProcModule = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
22     FALSE,
23     dwProcId[dwIndex] );
24     if( !hProcModule )
25     {
26     continue;
27     }
28    
29     DWORD dwModuleSize = 0;
30     HMODULE hModules[MAXCOUNT] = { 0 };
31    
32     bool bDeviceDriver = false;
33    
34     LPVOID lpvLoadAddrs[MAXCOUNT ] = { 0 };
35    
36     EnumProcessModules( hProcModule, hModules, MAXCOUNT, &dwModuleSize );
37     dwModuleSize /= sizeof( HMODULE );
38     if( !dwModuleSize )
39     {
40     EnumDeviceDrivers( lpvLoadAddrs, MAXCOUNT, &dwModuleSize );
41     dwModuleSize /= sizeof( LPVOID );
42    
43     if( !dwSize )
44     {
45     continue;
46     }
47    
48     bDeviceDriver = true;
49     }
50    
51     if( !bDeviceDriver )
52     {
53     pThisModule = new CModuleDetails( hProcModule, hModules[0]);
54     pThisModule->pProcessID = dwProcId[dwIndex];
55     pThisModule->m_bIsProcess = TRUE;
56     m_ModulesList.AddTail(pThisModule);
57     }
58    
59     CloseHandle( hProcModule );
60     }
61     }
62    
63     CEnumProcessList::CEnumProcessList(DWORD dwProcessID): m_hProcess(0),m_dwError(0)
64     {
65     HMODULE hModules[1024];
66     int iHandleCount = 0;
67     int iResult = 0;
68     CModuleDetails * pThisModule = NULL;
69     DWORD dwNeeded = 0;
70    
71     m_hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,dwProcessID);
72    
73     if (m_hProcess == FALSE)
74     {
75     m_dwError = ::GetLastError();
76     }
77     else {
78     // Get the modules for this process
79     iResult = ::EnumProcessModules(m_hProcess,hModules,sizeof(hModules),&dwNeeded);
80     if(iResult == 0) {
81     m_dwError = ::GetLastError();
82     }
83     else {
84     bool bDeviceDriver = false;
85     iHandleCount = (dwNeeded/sizeof(HMODULE));
86    
87     for(int i=1;i<iHandleCount;i++)
88     {
89     pThisModule = new CModuleDetails(m_hProcess,hModules[i]);
90     pThisModule->pProcessID = NULL;
91     pThisModule->m_bIsProcess = FALSE;
92     m_ModulesList.AddTail(pThisModule);
93     }
94     }
95     }
96    
97     if(::CloseHandle(m_hProcess) == 0) {
98     m_dwError = ::GetLastError();
99     }
100     }
101    
102    
103     CEnumProcessList::~CEnumProcessList()
104     {
105     POSITION pPos = NULL;
106     CModuleDetails* pModule = NULL;
107     CString Key;
108    
109     if (m_ModulesList.IsEmpty() == FALSE)
110     {
111     for(pPos = m_ModulesList.GetHeadPosition();pPos != NULL;)
112     {
113     pModule = (CModuleDetails *)m_ModulesList.GetNext(pPos);
114     ASSERT(pModule != NULL);
115    
116     delete pModule;
117     }
118     m_ModulesList.RemoveAll();
119     }
120     }
121    
122    
123     BOOL CEnumProcessList::GetModuleVersion(CString ModuleName,CString &VersionString)
124     {
125     CModuleDetails* pModuleData = NULL;
126     BOOL bResult = FALSE;
127     POSITION pPos = NULL;
128     ModuleName.MakeUpper();
129    
130     if (m_ModulesList.IsEmpty() == FALSE)
131     {
132     for(pPos = m_ModulesList.GetHeadPosition();pPos != NULL;)
133     {
134     pModuleData = (CModuleDetails *)m_ModulesList.GetNext(pPos);
135     ASSERT(pModuleData != NULL);
136     if(ModuleName.CompareNoCase(pModuleData->GetModuleName())!=0)
137     {
138     bResult = TRUE;
139     break;
140     }
141     }
142     }
143    
144     if(bResult == TRUE) {
145     ASSERT(pModuleData != NULL);
146     VersionString = pModuleData->GetFileVersion();
147     return(TRUE);
148     }
149     else {
150     VersionString = "";
151     return(FALSE);
152     }
153     }
154    
155     BOOL CEnumProcessList::SearchProcessOrModule(CString ModuleName,CModuleDetails* pModuleData)
156     {
157     BOOL bResult = FALSE;
158     POSITION pPos = NULL;
159     ModuleName.MakeUpper();
160    
161     if (m_ModulesList.IsEmpty() == FALSE)
162     {
163     for(pPos = m_ModulesList.GetHeadPosition();pPos != NULL;)
164     {
165     pModuleData = (CModuleDetails *)m_ModulesList.GetNext(pPos);
166     ASSERT(pModuleData != NULL);
167     if(ModuleName.CompareNoCase(pModuleData->GetModuleName())!=0)
168     {
169     bResult = TRUE;
170     break;
171     }
172     }
173     }
174    
175     if(bResult == TRUE) {
176     ASSERT(pModuleData != NULL);
177     return(TRUE);
178     }
179     else {
180     pModuleData = NULL;
181     return(FALSE);
182     }
183     }
184    
185    
186     POSITION CEnumProcessList::GetHeadPosition()
187     {
188     //ASSERT(m_ModulesList.IsEmpty() == FALSE);
189     // m_ModulesList.IsEmpty() == FALSE;
190     return(m_ModulesList.GetHeadPosition());
191     }
192    
193    
194     CModuleDetails* CEnumProcessList::GetNext(POSITION &Pos)
195     {
196     CString Key;
197     CModuleDetails* pTemp = NULL;
198    
199     ASSERT(Pos != NULL);
200     if(Pos != NULL) {
201     pTemp = (CModuleDetails *)m_ModulesList.GetNext(Pos);
202     ASSERT(pTemp != NULL);
203     }
204    
205     return (pTemp);
206     }
207    
208     BOOL CEnumProcessList::HasFailed()
209     {
210     if(m_dwError != 0) {
211     return(TRUE);
212     }
213     else {
214     return(FALSE);
215     }
216     }
217    
218    
219     DWORD CEnumProcessList::LastError()
220     {
221     return(m_dwError);
222     }
223    
224     CString CEnumProcessList::FormatError(DWORD dwError)
225     {
226     LPTSTR lpBuffer;
227     CString Temp;
228     int iReturn = 0;
229    
230     iReturn = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
231     NULL, dwError,MAKELANGID(LANG_NEUTRAL,SUBLANG_SYS_DEFAULT),
232     (LPTSTR) &lpBuffer,0,NULL);
233    
234     if(iReturn != 0) {
235     Temp = lpBuffer;
236     ::LocalFree(lpBuffer);
237     }
238     else {
239     Temp = "Unknown error";
240     }
241    
242     return(Temp);
243     }
244    
245    
246    
247    
248     /*******************************************************************************/
249    
250     #pragma comment (lib, "version.lib")
251    
252     CModuleDetails::CModuleDetails(const HANDLE hProcessID,const HMODULE hModuleID)
253     :
254     m_hProcessHandle(hProcessID),
255     m_hModuleHandle(hModuleID),
256     m_ModuleName(""),
257     m_FullFileName(""),
258     m_BaseName(""),
259     m_lFileSize(0),
260     m_wLangID(0),
261     m_wCharSet(0),
262     m_Comments(""),
263     m_CompanyName(""),
264     m_FileDescription(""),
265     m_FileVersion(""),
266     m_InternalName(""),
267     m_LegalCopyright(""),
268     m_LegalTrademarks(""),
269     m_OriginalFilename(""),
270     m_PrivateBuild(""),
271     m_ProductName(""),
272     m_ProductVersion(""),
273     m_SpecialBuild(""),
274     m_dwError(0)
275     {
276     char cBuffer[MAX_PATH];
277     char* pVersionInfo = NULL;
278     DWORD dwVersionSize = 0;
279     DWORD dwResult;
280     DWORD dwVersionHandle;
281     void* pFixedPointer = NULL;
282     UINT uFixedLength = 0;
283     TRANSLATIONS* pTranslations = NULL;
284     CFileStatus FileStatus;
285    
286     //Get the module details based on the handle passed
287     memset(cBuffer,0x00,MAX_PATH);
288     dwResult = ::GetModuleBaseName(m_hProcessHandle,m_hModuleHandle,cBuffer,MAX_PATH-1);
289     if(dwResult == 0) {
290     m_dwError = ::GetLastError();
291     }
292     else {
293     m_BaseName = cBuffer;
294     }
295    
296     // Module Filename Full path
297     memset(cBuffer,0x00,MAX_PATH);
298     dwResult = ::GetModuleFileNameEx(m_hProcessHandle,m_hModuleHandle,cBuffer,MAX_PATH-1);
299     if(dwResult == 0) {
300     m_dwError = ::GetLastError();
301     }
302     else {
303     m_FullFileName = cBuffer;
304     m_FullFileName.MakeUpper();
305     }
306     m_ModuleName = m_BaseName;
307     m_ModuleName.MakeUpper();
308    
309     // Get the file details
310     CFile::GetStatus(m_FullFileName,FileStatus);
311     m_lFileSize = FileStatus.m_size;
312     m_FileDate = FileStatus.m_mtime;
313    
314     TRACE(GetModuleName());
315     TRACE("\n");
316    
317     // Get the version information
318     dwVersionSize = GetFileVersionInfoSize(cBuffer,&dwVersionHandle);
319     if (GetFileVersionInfoSize == 0) {
320     m_dwError = ::GetLastError();
321     }
322     else {
323     pVersionInfo = (char *)malloc(dwVersionSize);
324     if (pVersionInfo == NULL) {
325     m_dwError = ::GetLastError();
326     }
327     else {
328     if (::GetFileVersionInfo(cBuffer,dwVersionHandle,dwVersionSize,pVersionInfo) == FALSE) {
329     m_dwError = ::GetLastError();
330     }
331     else {
332     if(::VerQueryValue(pVersionInfo,"\\VarFileInfo\\Translation",&pFixedPointer,&uFixedLength)==FALSE) {
333     m_dwError = ::GetLastError();
334     }
335     else {
336     pTranslations = (TRANSLATIONS *)pFixedPointer;
337    
338     m_wLangID = pTranslations[0].m_wLangID;
339     m_wCharSet = pTranslations[0].m_wCharSet;
340    
341     m_Comments = GetVersionString(pVersionInfo,"Comments");
342     m_CompanyName = GetVersionString(pVersionInfo,"CompanyName");
343     m_FileDescription = GetVersionString(pVersionInfo,"FileDescription");
344     m_FileVersion = GetVersionString(pVersionInfo,"FileVersion");
345     m_InternalName = GetVersionString(pVersionInfo,"InternalName");
346     m_LegalCopyright = GetVersionString(pVersionInfo,"LegalCopyright");
347     m_LegalTrademarks = GetVersionString(pVersionInfo,"LegalTrademarks");
348     m_OriginalFilename = GetVersionString(pVersionInfo,"OriginalFilename");
349     m_PrivateBuild = GetVersionString(pVersionInfo,"PrivateBuild");
350     m_ProductName = GetVersionString(pVersionInfo,"ProductName");
351     m_ProductVersion = GetVersionString(pVersionInfo,"ProductVersion");
352     m_SpecialBuild = GetVersionString(pVersionInfo,"SpecialBuild");
353     }
354     }
355     }
356     ASSERT(pVersionInfo!=NULL);
357     free(pVersionInfo);
358     }
359     }
360    
361    
362     CModuleDetails::CModuleDetails(CModuleDetails&)
363     {
364     }
365    
366     CModuleDetails::CModuleDetails()
367     {
368     }
369    
370     CModuleDetails::~CModuleDetails()
371     {
372     }
373    
374    
375     CString CModuleDetails::GetVersionString(char *pVersionInfo, char *pKey)
376     {
377     void *pFixedPointer = NULL;
378     UINT uFixedLength = 0;
379     char cQuery[64];
380    
381     sprintf(cQuery,"\\StringFileInfo\\%04x%04x\\%s",m_wLangID,m_wCharSet,pKey);
382     if(::VerQueryValue(pVersionInfo,cQuery,&pFixedPointer,&uFixedLength)==TRUE) {
383     if(uFixedLength > 0) {
384     // uFixedLength includes the trialling "\0"
385     return(CString((char *)pFixedPointer,((int)uFixedLength)-1));
386     }
387     else {
388     return("");
389     }
390     }
391     else {
392     m_dwError = ::GetLastError();
393     return("");
394     }
395     return("");
396     }
397    
398    
399     BOOL CModuleDetails::HasFailed() const
400     {
401     if(m_dwError != 0) {
402     return(TRUE);
403     }
404     else {
405     return(FALSE);
406     }
407     }
408    
409    
410    
411     CString CModuleDetails::GetComments() const
412     {
413     return(m_Comments);
414     }
415    
416     CString CModuleDetails::GetCompanyName() const
417     {
418     return(m_CompanyName);
419     }
420    
421     CString CModuleDetails::GetFileDescription() const
422     {
423     return(m_FileDescription);
424     }
425    
426     CString CModuleDetails::GetFileVersion() const
427     {
428     return(m_FileVersion);
429     }
430    
431     CString CModuleDetails::GetInternalName() const
432     {
433     return(m_InternalName);
434     }
435    
436     CString CModuleDetails::GetLegalCopyright() const
437     {
438     return(m_LegalCopyright);
439     }
440    
441     CString CModuleDetails::GetLegalTrademarks() const
442     {
443     return(m_LegalTrademarks);
444     }
445    
446     CString CModuleDetails::GetOriginalFilename() const
447     {
448     return(m_OriginalFilename);
449     }
450    
451     CString CModuleDetails::GetPrivateBuild() const
452     {
453     return(m_PrivateBuild);
454     }
455    
456     CString CModuleDetails::GetProductName() const
457     {
458     return(m_ProductName);
459     }
460    
461     CString CModuleDetails::GetProductVersion() const
462     {
463     return(m_ProductVersion);
464     }
465    
466     CString CModuleDetails::GetSpecialBuild() const
467     {
468     return(m_SpecialBuild);
469     }
470    
471     CString CModuleDetails::GetFullFileName() const
472     {
473     return(m_FullFileName);
474     }
475    
476     CString CModuleDetails::GetModuleName() const
477     {
478     return(m_ModuleName);
479     }
480    
481     long CModuleDetails::GetFileSize() const
482     {
483     return(m_lFileSize);
484     }
485    
486     CTime CModuleDetails::GetFileDate() const
487     {
488     return(m_FileDate);
489     }
490    
491     DWORD CModuleDetails::GetLastError() const
492     {
493     return(m_dwError);
494     }
495    
496    
497     HICON CModuleDetails::GetAssociatedIcon()
498     {
499     SHFILEINFO shFileInfo = { 0 };
500     SHGetFileInfo( m_FullFileName,
501     FILE_ATTRIBUTE_NORMAL,
502     &shFileInfo,
503     sizeof( shFileInfo ),
504     SHGFI_SMALLICON | SHGFI_ICON | SHGFI_USEFILEATTRIBUTES );
505    
506     return shFileInfo.hIcon;
507     }
508    
509     BOOL CModuleDetails::IsProcessTrue()
510     {
511     return m_bIsProcess;
512     }
513    
514     BOOL CModuleDetails::TerminateProcess()
515     {
516     if(IsProcessTrue())
517     {
518     HANDLE hProcess=OpenProcess(PROCESS_TERMINATE, FALSE, pProcessID);
519     return ::TerminateProcess(hProcess,1);
520     }
521     else
522     return FALSE;
523     }

  ViewVC Help
Powered by ViewVC 1.1.22