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
|
// Windows/ProcessUtils.h
#ifndef ZIP7_INC_WINDOWS_PROCESS_UTILS_H
#define ZIP7_INC_WINDOWS_PROCESS_UTILS_H
#include "../Common/MyWindows.h"
#ifndef Z7_OLD_WIN_SDK
#if defined(__MINGW32__) || defined(__MINGW64__)
#include <psapi.h>
#else
#include <Psapi.h>
#endif
#else // Z7_OLD_WIN_SDK
typedef struct _MODULEINFO {
LPVOID lpBaseOfDll;
DWORD SizeOfImage;
LPVOID EntryPoint;
} MODULEINFO, *LPMODULEINFO;
typedef struct _PROCESS_MEMORY_COUNTERS {
DWORD cb;
DWORD PageFaultCount;
SIZE_T PeakWorkingSetSize;
SIZE_T WorkingSetSize;
SIZE_T QuotaPeakPagedPoolUsage;
SIZE_T QuotaPagedPoolUsage;
SIZE_T QuotaPeakNonPagedPoolUsage;
SIZE_T QuotaNonPagedPoolUsage;
SIZE_T PagefileUsage;
SIZE_T PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS;
typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS;
#endif // Z7_OLD_WIN_SDK
#include "../Common/MyString.h"
#include "Defs.h"
#include "Handle.h"
namespace NWindows {
class CProcess: public CHandle
{
public:
bool Open(DWORD desiredAccess, bool inheritHandle, DWORD processId)
{
_handle = ::OpenProcess(desiredAccess, inheritHandle, processId);
return (_handle != NULL);
}
#ifndef UNDER_CE
bool GetExitCodeProcess(LPDWORD lpExitCode) { return BOOLToBool(::GetExitCodeProcess(_handle, lpExitCode)); }
bool Terminate(UINT exitCode) { return BOOLToBool(::TerminateProcess(_handle, exitCode)); }
#if (WINVER >= 0x0500)
DWORD GetGuiResources (DWORD uiFlags) { return ::GetGuiResources(_handle, uiFlags); }
#endif
bool SetPriorityClass(DWORD dwPriorityClass) { return BOOLToBool(::SetPriorityClass(_handle, dwPriorityClass)); }
DWORD GetPriorityClass() { return ::GetPriorityClass(_handle); }
// bool GetIoCounters(PIO_COUNTERS lpIoCounters ) { return BOOLToBool(::GetProcessIoCounters(_handle, lpIoCounters )); }
bool GetTimes(LPFILETIME creationTime, LPFILETIME exitTime, LPFILETIME kernelTime, LPFILETIME userTime)
{ return BOOLToBool(::GetProcessTimes(_handle, creationTime, exitTime, kernelTime, userTime)); }
DWORD WaitForInputIdle(DWORD milliseconds) { return ::WaitForInputIdle(_handle, milliseconds); }
// Debug
bool ReadMemory(LPCVOID baseAddress, LPVOID buffer, SIZE_T size, SIZE_T* numberOfBytesRead)
{ return BOOLToBool(::ReadProcessMemory(_handle, baseAddress, buffer, size, numberOfBytesRead)); }
bool WriteMemory(LPVOID baseAddress, LPCVOID buffer, SIZE_T size, SIZE_T* numberOfBytesWritten)
{ return BOOLToBool(::WriteProcessMemory(_handle, baseAddress,
#ifdef Z7_OLD_WIN_SDK
(LPVOID)
#endif
buffer,
size, numberOfBytesWritten)); }
bool FlushInstructionCache(LPCVOID baseAddress = NULL, SIZE_T size = 0)
{ return BOOLToBool(::FlushInstructionCache(_handle, baseAddress, size)); }
LPVOID VirtualAlloc(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect)
{ return VirtualAllocEx(_handle, address, size, allocationType, protect); }
bool VirtualFree(LPVOID address, SIZE_T size, DWORD freeType)
{ return BOOLToBool(::VirtualFreeEx(_handle, address, size, freeType)); }
// Process Status API (PSAPI)
/*
bool EmptyWorkingSet()
{ return BOOLToBool(::EmptyWorkingSet(_handle)); }
bool EnumModules(HMODULE *hModules, DWORD arraySizeInBytes, LPDWORD receivedBytes)
{ return BOOLToBool(::EnumProcessModules(_handle, hModules, arraySizeInBytes, receivedBytes)); }
DWORD MyGetModuleBaseName(HMODULE hModule, LPTSTR baseName, DWORD size)
{ return ::GetModuleBaseName(_handle, hModule, baseName, size); }
bool MyGetModuleBaseName(HMODULE hModule, CSysString &name)
{
const unsigned len = MAX_PATH + 100;
const DWORD resultLen = MyGetModuleBaseName(hModule, name.GetBuf(len), len);
name.ReleaseBuf_CalcLen(len);
return (resultLen != 0);
}
DWORD MyGetModuleFileNameEx(HMODULE hModule, LPTSTR baseName, DWORD size)
{ return ::GetModuleFileNameEx(_handle, hModule, baseName, size); }
bool MyGetModuleFileNameEx(HMODULE hModule, CSysString &name)
{
const unsigned len = MAX_PATH + 100;
const DWORD resultLen = MyGetModuleFileNameEx(hModule, name.GetBuf(len), len);
name.ReleaseBuf_CalcLen(len);
return (resultLen != 0);
}
bool GetModuleInformation(HMODULE hModule, LPMODULEINFO moduleInfo)
{ return BOOLToBool(::GetModuleInformation(_handle, hModule, moduleInfo, sizeof(MODULEINFO))); }
bool GetMemoryInfo(PPROCESS_MEMORY_COUNTERS memCounters)
{ return BOOLToBool(::GetProcessMemoryInfo(_handle, memCounters, sizeof(PROCESS_MEMORY_COUNTERS))); }
*/
#endif
WRes Create(LPCWSTR imageName, const UString ¶ms, LPCWSTR curDir);
DWORD Wait() { return ::WaitForSingleObject(_handle, INFINITE); }
};
WRes MyCreateProcess(LPCWSTR imageName, const UString ¶ms);
}
#endif
|