aboutsummaryrefslogtreecommitdiff
path: root/CPP/Windows/ProcessUtils.h
diff options
context:
space:
mode:
Diffstat (limited to 'CPP/Windows/ProcessUtils.h')
-rw-r--r--CPP/Windows/ProcessUtils.h100
1 files changed, 100 insertions, 0 deletions
diff --git a/CPP/Windows/ProcessUtils.h b/CPP/Windows/ProcessUtils.h
new file mode 100644
index 0000000..e46f9ab
--- /dev/null
+++ b/CPP/Windows/ProcessUtils.h
@@ -0,0 +1,100 @@
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
13namespace NWindows {
14
15class CProcess: public CHandle
16{
17public:
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
26 bool GetExitCodeProcess(LPDWORD lpExitCode) { return BOOLToBool(::GetExitCodeProcess(_handle, lpExitCode)); }
27 bool Terminate(UINT exitCode) { return BOOLToBool(::TerminateProcess(_handle, exitCode)); }
28 #if (WINVER >= 0x0500)
29 DWORD GetGuiResources (DWORD uiFlags) { return ::GetGuiResources(_handle, uiFlags); }
30 #endif
31 bool SetPriorityClass(DWORD dwPriorityClass) { return BOOLToBool(::SetPriorityClass(_handle, dwPriorityClass)); }
32 DWORD GetPriorityClass() { return ::GetPriorityClass(_handle); }
33 // bool GetIoCounters(PIO_COUNTERS lpIoCounters ) { return BOOLToBool(::GetProcessIoCounters(_handle, lpIoCounters )); }
34
35 bool GetTimes(LPFILETIME creationTime, LPFILETIME exitTime, LPFILETIME kernelTime, LPFILETIME userTime)
36 { return BOOLToBool(::GetProcessTimes(_handle, creationTime, exitTime, kernelTime, userTime)); }
37
38 DWORD WaitForInputIdle(DWORD milliseconds) { return ::WaitForInputIdle(_handle, milliseconds); }
39
40 // Debug
41
42 bool ReadMemory(LPCVOID baseAddress, LPVOID buffer, SIZE_T size, SIZE_T* numberOfBytesRead)
43 { return BOOLToBool(::ReadProcessMemory(_handle, baseAddress, buffer, size, numberOfBytesRead)); }
44
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
51 LPVOID VirtualAlloc(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect)
52 { return VirtualAllocEx(_handle, address, size, allocationType, protect); }
53
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
59 bool EmptyWorkingSet()
60 { return BOOLToBool(::EmptyWorkingSet(_handle)); }
61 bool EnumModules(HMODULE *hModules, DWORD arraySizeInBytes, LPDWORD receivedBytes)
62 { return BOOLToBool(::EnumProcessModules(_handle, hModules, arraySizeInBytes, receivedBytes)); }
63
64 DWORD MyGetModuleBaseName(HMODULE hModule, LPTSTR baseName, DWORD size)
65 { return ::GetModuleBaseName(_handle, hModule, baseName, size); }
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
74 DWORD MyGetModuleFileNameEx(HMODULE hModule, LPTSTR baseName, DWORD size)
75 { return ::GetModuleFileNameEx(_handle, hModule, baseName, size); }
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
84 bool GetModuleInformation(HMODULE hModule, LPMODULEINFO moduleInfo)
85 { return BOOLToBool(::GetModuleInformation(_handle, hModule, moduleInfo, sizeof(MODULEINFO))); }
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
93 DWORD Wait() { return ::WaitForSingleObject(_handle, INFINITE); }
94};
95
96WRes MyCreateProcess(LPCWSTR imageName, const UString &params);
97
98}
99
100#endif