xref: /aosp_15_r20/external/lzma/CPP/Windows/ProcessUtils.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // Windows/ProcessUtils.h
2 
3 #ifndef ZIP7_INC_WINDOWS_PROCESS_UTILS_H
4 #define ZIP7_INC_WINDOWS_PROCESS_UTILS_H
5 
6 #include "../Common/MyWindows.h"
7 
8 #ifndef Z7_OLD_WIN_SDK
9 
10 #if defined(__MINGW32__) || defined(__MINGW64__)
11 #include <psapi.h>
12 #else
13 #include <Psapi.h>
14 #endif
15 
16 #else // Z7_OLD_WIN_SDK
17 
18 typedef struct _MODULEINFO {
19     LPVOID lpBaseOfDll;
20     DWORD SizeOfImage;
21     LPVOID EntryPoint;
22 } MODULEINFO, *LPMODULEINFO;
23 
24 typedef struct _PROCESS_MEMORY_COUNTERS {
25     DWORD cb;
26     DWORD PageFaultCount;
27     SIZE_T PeakWorkingSetSize;
28     SIZE_T WorkingSetSize;
29     SIZE_T QuotaPeakPagedPoolUsage;
30     SIZE_T QuotaPagedPoolUsage;
31     SIZE_T QuotaPeakNonPagedPoolUsage;
32     SIZE_T QuotaNonPagedPoolUsage;
33     SIZE_T PagefileUsage;
34     SIZE_T PeakPagefileUsage;
35 } PROCESS_MEMORY_COUNTERS;
36 typedef PROCESS_MEMORY_COUNTERS *PPROCESS_MEMORY_COUNTERS;
37 
38 #endif // Z7_OLD_WIN_SDK
39 
40 #include "../Common/MyString.h"
41 
42 #include "Defs.h"
43 #include "Handle.h"
44 
45 namespace NWindows {
46 
47 class CProcess: public CHandle
48 {
49 public:
Open(DWORD desiredAccess,bool inheritHandle,DWORD processId)50   bool Open(DWORD desiredAccess, bool inheritHandle, DWORD processId)
51   {
52     _handle = ::OpenProcess(desiredAccess, inheritHandle, processId);
53     return (_handle != NULL);
54   }
55 
56   #ifndef UNDER_CE
57 
GetExitCodeProcess(LPDWORD lpExitCode)58   bool GetExitCodeProcess(LPDWORD lpExitCode) { return BOOLToBool(::GetExitCodeProcess(_handle, lpExitCode)); }
Terminate(UINT exitCode)59   bool Terminate(UINT exitCode) { return BOOLToBool(::TerminateProcess(_handle, exitCode)); }
60   #if (WINVER >= 0x0500)
GetGuiResources(DWORD uiFlags)61   DWORD GetGuiResources (DWORD uiFlags) { return ::GetGuiResources(_handle, uiFlags); }
62   #endif
SetPriorityClass(DWORD dwPriorityClass)63   bool SetPriorityClass(DWORD dwPriorityClass) { return BOOLToBool(::SetPriorityClass(_handle, dwPriorityClass)); }
GetPriorityClass()64   DWORD GetPriorityClass() { return ::GetPriorityClass(_handle); }
65   // bool GetIoCounters(PIO_COUNTERS lpIoCounters ) { return BOOLToBool(::GetProcessIoCounters(_handle, lpIoCounters )); }
66 
GetTimes(LPFILETIME creationTime,LPFILETIME exitTime,LPFILETIME kernelTime,LPFILETIME userTime)67   bool GetTimes(LPFILETIME creationTime, LPFILETIME exitTime, LPFILETIME kernelTime, LPFILETIME userTime)
68     { return BOOLToBool(::GetProcessTimes(_handle, creationTime, exitTime, kernelTime, userTime)); }
69 
WaitForInputIdle(DWORD milliseconds)70   DWORD WaitForInputIdle(DWORD milliseconds) { return ::WaitForInputIdle(_handle, milliseconds);  }
71 
72   // Debug
73 
ReadMemory(LPCVOID baseAddress,LPVOID buffer,SIZE_T size,SIZE_T * numberOfBytesRead)74   bool ReadMemory(LPCVOID baseAddress, LPVOID buffer, SIZE_T size, SIZE_T* numberOfBytesRead)
75     { return BOOLToBool(::ReadProcessMemory(_handle, baseAddress, buffer, size, numberOfBytesRead));  }
76 
WriteMemory(LPVOID baseAddress,LPCVOID buffer,SIZE_T size,SIZE_T * numberOfBytesWritten)77   bool WriteMemory(LPVOID baseAddress, LPCVOID buffer, SIZE_T size, SIZE_T* numberOfBytesWritten)
78     { return BOOLToBool(::WriteProcessMemory(_handle, baseAddress,
79       #ifdef Z7_OLD_WIN_SDK
80         (LPVOID)
81       #endif
82       buffer,
83       size, numberOfBytesWritten)); }
84 
85   bool FlushInstructionCache(LPCVOID baseAddress = NULL, SIZE_T size = 0)
86     { return BOOLToBool(::FlushInstructionCache(_handle, baseAddress, size)); }
87 
VirtualAlloc(LPVOID address,SIZE_T size,DWORD allocationType,DWORD protect)88   LPVOID VirtualAlloc(LPVOID address, SIZE_T size, DWORD allocationType, DWORD protect)
89     { return VirtualAllocEx(_handle, address, size, allocationType, protect);  }
90 
VirtualFree(LPVOID address,SIZE_T size,DWORD freeType)91   bool VirtualFree(LPVOID address, SIZE_T size, DWORD freeType)
92     { return BOOLToBool(::VirtualFreeEx(_handle, address, size, freeType)); }
93 
94   // Process Status API (PSAPI)
95 
96   /*
97   bool EmptyWorkingSet()
98     { return BOOLToBool(::EmptyWorkingSet(_handle)); }
99   bool EnumModules(HMODULE *hModules, DWORD arraySizeInBytes, LPDWORD receivedBytes)
100     { return BOOLToBool(::EnumProcessModules(_handle, hModules, arraySizeInBytes, receivedBytes)); }
101   DWORD MyGetModuleBaseName(HMODULE hModule, LPTSTR baseName, DWORD size)
102     { return ::GetModuleBaseName(_handle, hModule, baseName, size); }
103   bool MyGetModuleBaseName(HMODULE hModule, CSysString &name)
104   {
105     const unsigned len = MAX_PATH + 100;
106     const DWORD resultLen = MyGetModuleBaseName(hModule, name.GetBuf(len), len);
107     name.ReleaseBuf_CalcLen(len);
108     return (resultLen != 0);
109   }
110 
111   DWORD MyGetModuleFileNameEx(HMODULE hModule, LPTSTR baseName, DWORD size)
112     { return ::GetModuleFileNameEx(_handle, hModule, baseName, size); }
113   bool MyGetModuleFileNameEx(HMODULE hModule, CSysString &name)
114   {
115     const unsigned len = MAX_PATH + 100;
116     const DWORD resultLen = MyGetModuleFileNameEx(hModule, name.GetBuf(len), len);
117     name.ReleaseBuf_CalcLen(len);
118     return (resultLen != 0);
119   }
120 
121   bool GetModuleInformation(HMODULE hModule, LPMODULEINFO moduleInfo)
122     { return BOOLToBool(::GetModuleInformation(_handle, hModule, moduleInfo, sizeof(MODULEINFO))); }
123   bool GetMemoryInfo(PPROCESS_MEMORY_COUNTERS memCounters)
124     { return BOOLToBool(::GetProcessMemoryInfo(_handle, memCounters, sizeof(PROCESS_MEMORY_COUNTERS))); }
125   */
126 
127   #endif
128 
129   WRes Create(LPCWSTR imageName, const UString &params, LPCWSTR curDir);
130 
Wait()131   DWORD Wait() { return ::WaitForSingleObject(_handle, INFINITE); }
132 };
133 
134 WRes MyCreateProcess(LPCWSTR imageName, const UString &params);
135 
136 }
137 
138 #endif
139