Skip to content

Commit 21b62f7

Browse files
authored
暂停和恢复进程
1 parent 652f881 commit 21b62f7

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include <windows.h>
2+
#include <Tlhelp32.h>
3+
#include <stdio.h>
4+
#include <conio.h>
5+
6+
typedef DWORD (WINAPI *SUSPENDPROCESS)(HANDLE);
7+
typedef DWORD (WINAPI *RESUMEPROCESS)(HANDLE);
8+
9+
SUSPENDPROCESS SuspendProcess;
10+
RESUMEPROCESS ResumeProcess;
11+
12+
13+
int ProcProcess(LPSTR lpFillName)
14+
{
15+
PROCESSENTRY32 stProcess;
16+
stProcess.dwSize = sizeof (PROCESSENTRY32);
17+
18+
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
19+
20+
if (NULL == hSnapShot) {
21+
return 0; //创建进程快照失败
22+
}
23+
24+
BOOL bLoop = Process32First(hSnapShot, &stProcess);
25+
while (bLoop)
26+
{
27+
CharLower(stProcess.szExeFile);
28+
if (0 == lstrcmp(lpFillName, stProcess.szExeFile))
29+
{
30+
//MessageBox(NULL, (LPCTSTR)TEXT("有进程"), (LPCTSTR)TEXT("信息"), MB_OK | MB_ICONINFORMATION);
31+
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, stProcess.th32ProcessID);
32+
if (NULL == hProcess) {
33+
//打开进程失败
34+
return 0;
35+
}
36+
SuspendProcess(hProcess);
37+
int ret = MessageBox(NULL, (LPCTSTR)TEXT("目标进程是否关闭?"), (LPCTSTR)TEXT("询问"), MB_YESNO | MB_ICONQUESTION);
38+
if (IDYES == ret) {
39+
//关闭
40+
TerminateProcess(hProcess, 0);
41+
} else {
42+
//不关闭, 取消进程挂起状态
43+
lpFillName[0] = 0;
44+
ResumeProcess(hProcess);
45+
}
46+
47+
CloseHandle(hProcess);
48+
49+
}
50+
bLoop = Process32Next(hSnapShot, &stProcess);
51+
}
52+
53+
CloseHandle(hSnapShot); //关闭进程快照
54+
return 1;
55+
}
56+
57+
HINSTANCE InitDll()
58+
{
59+
HINSTANCE hDllInstance = LoadLibrary((LPCSTR)TEXT("NTDLL.DLL"));
60+
if (NULL == hDllInstance) {
61+
MessageBox(NULL,
62+
(LPCSTR)TEXT("NTDll.dll文件丢失或装载失败, 程序功能无法实现"),
63+
(LPCSTR)TEXT("失败"), MB_OK | MB_ICONERROR);
64+
return NULL;
65+
}
66+
67+
SuspendProcess = (SUSPENDPROCESS)GetProcAddress(hDllInstance, (LPCSTR)TEXT
68+
69+
("ZwSuspendProcess"));
70+
if (NULL == SuspendProcess) {
71+
MessageBox(NULL,
72+
(LPCSTR)TEXT("ntdll.dll打不到函数ZwSuspendProcess"),
73+
(LPCSTR)TEXT("失败"), MB_OK | MB_ICONERROR);
74+
CloseHandle(hDllInstance);
75+
return NULL;
76+
}
77+
78+
ResumeProcess = (RESUMEPROCESS)GetProcAddress(hDllInstance, (LPCSTR)TEXT
79+
80+
("ZwResumeProcess"));
81+
if (NULL == ResumeProcess) {
82+
MessageBox(NULL,
83+
(LPCSTR)TEXT("ntdll.dll打不到函数ResumeProcess"),
84+
(LPCSTR)TEXT("失败"), MB_OK | MB_ICONERROR);
85+
CloseHandle(hDllInstance);
86+
return NULL;
87+
}
88+
return hDllInstance;
89+
}
90+
91+
void Exit(HINSTANCE hDllInstance)
92+
{
93+
FreeLibrary(hDllInstance);
94+
}
95+
96+
DWORD WINAPI ThreadProc(LPVOID lpParameter)
97+
{
98+
while (TRUE)
99+
{
100+
ProcProcess((LPSTR)lpParameter);
101+
Sleep(100);
102+
}
103+
return 0;
104+
}
105+
106+
107+
int main(int argc, char* argv[])
108+
{
109+
TCHAR lpFileName[MAX_PATH];
110+
printf("请输入进程名称(会转成小写):");
111+
scanf("%s", lpFileName);
112+
113+
CharLower(lpFileName); //转成小写
114+
115+
HINSTANCE hDllInstance = InitDll();
116+
117+
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, (LPVOID)lpFileName, NULL, NULL);
118+
CloseHandle(hThread);
119+
Sleep(20000);
120+
Exit(hDllInstance);
121+
return 0;
122+
}

0 commit comments

Comments
 (0)