1 // Windows/ProcessUtils.h
2 
3 #ifndef __WINDOWS_PROCESS_UTILS_H
4 #define __WINDOWS_PROCESS_UTILS_H
5 
6 #include <psapi.h>
7 
8 #include "../Common/MyString.h"
9 
10 #include "Defs.h"
11 #include "Handle.h"
12 
13 namespace NWindows {
14 
15 class CProcess: public CHandle
16 {
17 public:
Open(DWORD desiredAccess,bool inheritHandle,DWORD processId)18   bool Open(DWORD desiredAccess, bool inheritHandle, DWORD processId)
19   {
20     _handle = ::OpenProcess(desiredAccess, inheritHandle, processId);
21     return (_handle != 0);
22   }
23 
24   #ifndef UNDER_CE
25 
GetExitCodeProcess(LPDWORD lpExitCode)26   bool GetExitCodeProcess(LPDWORD lpExitCode) { return BOOLToBool(::GetExitCodeProcess(_handle, lpExitCode)); }
Terminate(UINT exitCode)27   bool Terminate(UINT exitCode) { return BOOLToBool(::TerminateProcess(_handle, exitCode)); }
28   #if (WINVER >= 0x0500)
GetGuiResources(DWORD uiFlags)29   DWORD GetGuiResources (DWORD uiFlags) { return ::GetGuiResources(_handle, uiFlags); }
30   #endif
SetPriorityClass(DWORD dwPriorityClass)31   bool SetPriorityClass(DWORD dwPriorityClass) { return BOOLToBool(::SetPriorityClass(_handle, dwPriorityClass)); }
GetPriorityClass()32   DWORD GetPriorityClass() { return ::GetPriorityClass(_handle); }
33   // bool GetIoCounters(PIO_COUNTERS lpIoCounters ) { return BOOLToBool(::GetProcessIoCounters(_handle, lpIoCounters )); }
34 
GetTimes(LPFILETIME creationTime,LPFILETIME exitTime,LPFILETIME kernelTime,LPFILETIME userTime)35   bool GetTimes(LPFILETIME creationTime, LPFILETIME exitTime, LPFILETIME kernelTime, LPFILETIME userTime)
36     { return BOOLToBool(::GetProcessTimes(_handle, creationTime, exitTime, kernelTime, userTime)); }
37 
WaitForInputIdle(DWORD milliseconds)38   DWORD WaitForInputIdle(DWORD milliseconds) { return ::WaitForInputIdle(_handle, milliseconds);  }
39 
40   // Debug
41 
ReadMemory(LPCVOID baseAddress,LPVOID buffer,SIZE_T size,SIZE_T * numberOfBytesRead)42   bool ReadMemory(LPCVOID baseAddress, LPVOID buffer, SIZE_T size, SIZE_T* numberOfBytesRead)
43     { return BOOLToBool(::ReadProcessMemory(_handle, baseAddress, buffer, size, numberOfBytesRead));  }
44 
WriteMemory(LPVOID baseAddress,LPCVOID buffer,SIZE_T size,SIZE_T * numberOfBytesWritten)45   bool WriteMemory(LPVOID baseAddress, LPCVOID buffer, SIZE_T size, SIZE_T* numberOfBytesWritten)
46     { return BOOLToBool(::WriteProcessMemory(_handle, baseAddress, buffer, size, numberOfBytesWritten)); }
47 
48   bool FlushInstructionCache(LPCVOID baseAddress = 0, SIZE_T size = 0)
49     { return BOOLToBool(::FlushInstructionCache(_handle, baseAddress, size)); }
50 
VirtualAlloc(LPVOID address,SIZE_T size,DWORD allocationType,DWORD protect)51   LPVOID VirtualAlloc(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect)
52     { return VirtualAllocEx(_handle, address, size, allocationType, protect);  }
53 
VirtualFree(LPVOID address,SIZE_T size,DWORD freeType)54   bool VirtualFree(LPVOID address, SIZE_T size, DWORD freeType)
55     { return BOOLToBool(::VirtualFreeEx(_handle, address, size, freeType)); }
56 
57   // Process Status API (PSAPI)
58 
EmptyWorkingSet()59   bool EmptyWorkingSet()
60     { return BOOLToBool(::EmptyWorkingSet(_handle)); }
EnumModules(HMODULE * hModules,DWORD arraySizeInBytes,LPDWORD receivedBytes)61   bool EnumModules(HMODULE *hModules, DWORD arraySizeInBytes, LPDWORD receivedBytes)
62     { return BOOLToBool(::EnumProcessModules(_handle, hModules, arraySizeInBytes, receivedBytes)); }
63 
MyGetModuleBaseName(HMODULE hModule,LPTSTR baseName,DWORD size)64   DWORD MyGetModuleBaseName(HMODULE hModule, LPTSTR baseName, DWORD size)
65     { return ::GetModuleBaseName(_handle, hModule, baseName, size); }
MyGetModuleBaseName(HMODULE hModule,CSysString & name)66   bool MyGetModuleBaseName(HMODULE hModule, CSysString &name)
67   {
68     const unsigned len = MAX_PATH + 100;
69     DWORD resultLen = MyGetModuleBaseName(hModule, name.GetBuf(len), len);
70     name.ReleaseBuf_CalcLen(len);
71     return (resultLen != 0);
72   }
73 
MyGetModuleFileNameEx(HMODULE hModule,LPTSTR baseName,DWORD size)74   DWORD MyGetModuleFileNameEx(HMODULE hModule, LPTSTR baseName, DWORD size)
75     { return ::GetModuleFileNameEx(_handle, hModule, baseName, size); }
MyGetModuleFileNameEx(HMODULE hModule,CSysString & name)76   bool MyGetModuleFileNameEx(HMODULE hModule, CSysString &name)
77   {
78     const unsigned len = MAX_PATH + 100;
79     DWORD resultLen = MyGetModuleFileNameEx(hModule, name.GetBuf(len), len);
80     name.ReleaseBuf_CalcLen(len);
81     return (resultLen != 0);
82   }
83 
GetModuleInformation(HMODULE hModule,LPMODULEINFO moduleInfo)84   bool GetModuleInformation(HMODULE hModule, LPMODULEINFO moduleInfo)
85     { return BOOLToBool(::GetModuleInformation(_handle, hModule, moduleInfo, sizeof(MODULEINFO))); }
GetMemoryInfo(PPROCESS_MEMORY_COUNTERS memCounters)86   bool GetMemoryInfo(PPROCESS_MEMORY_COUNTERS memCounters)
87     { return BOOLToBool(::GetProcessMemoryInfo(_handle, memCounters, sizeof(PROCESS_MEMORY_COUNTERS))); }
88 
89   #endif
90 
91   WRes Create(LPCWSTR imageName, const UString &params, LPCWSTR curDir);
92 
Wait()93   DWORD Wait() { return ::WaitForSingleObject(_handle, INFINITE); }
94 };
95 
96 WRes MyCreateProcess(LPCWSTR imageName, const UString &params);
97 
98 }
99 
100 #endif
101