c++实现创建,销毁,挂起和激活记事本进程
时间: 2023-05-31 21:06:56 浏览: 231
用C++做的记事本程序
5星 · 资源好评率100%
创建记事本进程:
```
#include <windows.h>
#include <stdio.h>
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess("notepad.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
printf("CreateProcess failed (%d).\n", GetLastError());
return 1;
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
```
销毁记事本进程:
```
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
if (hProcess == NULL)
{
printf("OpenProcess failed (%d).\n", GetLastError());
return 1;
}
if (!TerminateProcess(hProcess, 0))
{
printf("TerminateProcess failed (%d).\n", GetLastError());
return 1;
}
CloseHandle(hProcess);
return 0;
}
```
挂起记事本进程:
```
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
if (hProcess == NULL)
{
printf("OpenProcess failed (%d).\n", GetLastError());
return 1;
}
if (!SuspendThread(hProcess))
{
printf("SuspendThread failed (%d).\n", GetLastError());
return 1;
}
CloseHandle(hProcess);
return 0;
}
```
激活记事本进程:
```
#include <windows.h>
#include <stdio.h>
int main()
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
if (hProcess == NULL)
{
printf("OpenProcess failed (%d).\n", GetLastError());
return 1;
}
if (ResumeThread(hProcess) == -1)
{
printf("ResumeThread failed (%d).\n", GetLastError());
return 1;
}
CloseHandle(hProcess);
return 0;
}
```
阅读全文