-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.c
107 lines (98 loc) · 2.94 KB
/
process.c
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
#include "process.h"
#include "data.h"
BOOL CALLBACK EnumWindowsProc(HWND hWnd, struct processData * pData)
{
DWORD pid;
GetWindowThreadProcessId(hWnd, &pid);
if(pid == pData->pid){
if(pData->windowAmount >= 255 || !IsWindowVisible(hWnd)){
return 1;
}
ShowWindow(hWnd,SW_HIDE);
pData->windowHandles[pData->windowAmount] = hWnd;
pData->windowAmount++;
}
return 1;
}
BOOL CALLBACK EnumWindowsProcNoPause(HWND hWnd, struct processData * pData)
{
DWORD pid;
GetWindowThreadProcessId(hWnd, &pid);
if(pid == pData->pid){
if(pData->windowAmount >= 255 || !IsWindowVisible(hWnd)){
return 1;
}
pData->windowHandles[pData->windowAmount] = hWnd;
pData->windowAmount++;
}
return 1;
}
struct processData* suspendProcessByWindow()
{
struct processData* pData = malloc(sizeof(struct processData));
GetWindowThreadProcessId(GetForegroundWindow(), &pData->pid);
pData->windowAmount = 0;
pData->windowHandles = malloc(sizeof(HWND)*256);
EnumWindows(EnumWindowsProc,pData);
pData->windowHandles = realloc(pData->windowHandles,pData->windowAmount*sizeof(HWND));
suspendProcess(pData->pid, 1);
pData->paused = 1;
return pData;
}
struct processData* pDataByWindow()
{
struct processData* pData = malloc(sizeof(struct processData));
GetWindowThreadProcessId(GetForegroundWindow(), &pData->pid);
pData->windowAmount = 0;
pData->windowHandles = malloc(sizeof(HWND)*256);
EnumWindows(EnumWindowsProcNoPause,pData);
pData->windowHandles = realloc(pData->windowHandles,pData->windowAmount*sizeof(HWND));
pData->paused = 0;
return pData;
}
void pauseProcess(struct processData* pData)
{
if(pData->paused == 1){
return;
}
int i;
for(i = 0; i != pData->windowAmount; ++i){
ShowWindow(pData->windowHandles[i],SW_HIDE);
}
suspendProcess(pData->pid,1);
pData->paused = 1;
return;
}
void resumeProcess(struct processData* pData)
{
suspendProcess(pData->pid,0);
int i;
for(i = 0; i != pData->windowAmount; ++i){
ShowWindow(pData->windowHandles[i],SW_SHOW);
}
pData->paused = 0;
return;
}
void suspendProcess(DWORD ProcessId, int Suspend)
{
HANDLE snHandle = NULL;
THREADENTRY32 te32 = {0};
snHandle = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (snHandle == INVALID_HANDLE_VALUE) return;
te32.dwSize = sizeof(THREADENTRY32);
if (Thread32First(snHandle, &te32)){
do{
if (te32.th32OwnerProcessID == ProcessId){
HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, FALSE, te32.th32ThreadID);
if (!Suspend){
ResumeThread(hThread);
}else{
SuspendThread(hThread);
}
CloseHandle(hThread);
}
}
while (Thread32Next(snHandle, &te32));
}
CloseHandle (snHandle);
}