在一般实战中,我们都需要去控制已有的正常程序的运行流程,那么首先要获取其他进程的句柄
文章目录
获取系统里正在运行的进程
CreateToolhelp32Snapshot
#include<stdio.h>
#include<windows.h>
#include<tlhelp32.h>
int main() {
WCHAR ProcessName[] = L"1.exe";
HANDLE hSnapShot= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
if (hSnapShot == INVALID_HANDLE_VALUE) {
printf("获取快照失败:%d", GetLastError());
return 0;
}
PROCESSENTRY32 p32;
ZeroMemory(&p32, sizeof(p32));
p32.dwSize = sizeof(p32);
Process32First(hSnapShot, &p32);
DWORD ProcessId = 0;
do {
if (!wcscmp(p32.szExeFile, ProcessName)) {
ProcessId = p32.th32ProcessID;
break;
}
} while (Process32Next(hSnapShot, &p32));
CloseHandle(hSnapShot);
if (!ProcessId) {
printf("获取进程失败:%d", GetLastError());
return 0;
}
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId);
if (!hProcess) {
printf("获取进程Handle失败:%d", GetLastError());
return 0;
}
CloseHandle(hProcess);
return 0;
}
EnumProcesses
这个相当于我们可以直接获取到PID,但需要反查名字,反查名字有两个api
GetProcessImageFileName
这个返回的是带有路径的应用名称
但这个路径格式略有奇怪
\Device\HarddiskVolume3\Users\test\Desktop\1.exe
#include<windows.h>
#include<stdio.h>
#include<Psapi.h>
int main() {
DWORD ProcessId[1024]={0};
DWORD cbNeed;
WCHAR appname[] = L"1.exe";
if (!EnumProcesses(ProcessId, sizeof(ProcessId), &cbNeed)) {
printf("枚举进程失败:%d", GetLastError());
return 0;
}
WCHAR Name[1024];
WCHAR s[] = L"%s\n";
LPVOID hProcess = 0;
for (int i = 0; i < cbNeed / sizeof(DWORD); i++) {
if (ProcessId[i]) {
HANDLE Process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId[i]);
if (!Process) {
continue;
}
GetProcessImageFileName(Process, Name, sizeof(Name)/sizeof(*Name));
if (wcsstr(Name, appname)) {
hProcess =Process;
CloseHandle(Process);
break;
}
CloseHandle(Process);
}
}
if (!hProcess) {
printf("没有所要查找的应用");
return 0;
}
printf("%d", hProcess);
return 0;
}
GetModuleBaseName
这个返回的Name只有应用的名字,没有路径
#include<windows.h>
#include<stdio.h>
#include<Psapi.h>
int main() {
DWORD ProcessId[1024]={0};
DWORD cbNeed;
WCHAR appname[] = L"1.exe";
if (!EnumProcesses(ProcessId, sizeof(ProcessId), &cbNeed)) {
printf("枚举进程失败:%d", GetLastError());
return 0;
}
WCHAR Name[1024];
WCHAR s[] = L"%s\n";
LPVOID hProcess = 0;
for (int i = 0; i < cbNeed / sizeof(DWORD); i++) {
if (ProcessId[i]) {
HANDLE Process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId[i]);
if (!Process) {
continue;
}
GetModuleBaseName(Process, NULL, Name, sizeof(Name) / sizeof(*Name));
if (!wcscmp(Name, appname)) {
hProcess =Process;
CloseHandle(Process);
break;
}
CloseHandle(Process);
}
}
if (!hProcess) {
printf("没有所要查找的应用");
return 0;
}
printf("%d", hProcess);
return 0;
}
GetModuleFileNameEx
这个获取的是完整的路径名
#include<windows.h>
#include<stdio.h>
#include<Psapi.h>
int main() {
DWORD ProcessId[1024] = { 0 };
DWORD cbNeed;
WCHAR appname[] = L"1.exe";
if (!EnumProcesses(ProcessId, sizeof(ProcessId), &cbNeed)) {
printf("枚举进程失败:%d", GetLastError());
return 0;
}
WCHAR Name[1024];
WCHAR s[] = L"%s\n";
LPVOID hProcess = 0;
for (int i = 0; i < cbNeed / sizeof(DWORD); i++) {
if (ProcessId[i]) {
HANDLE Process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId[i]);
if (!Process) {
continue;
}
GetModuleFileNameEx(Process, NULL, Name, sizeof(Name) / sizeof(*Name));
if (wcsstr(Name, appname)) {
hProcess = Process;
CloseHandle(Process);
break;
}
CloseHandle(Process);
}
}
if (!hProcess) {
printf("没有所要查找的应用");
return 0;
}
printf("%d", hProcess);
return 0;
}
这里提醒一下,一般windows api是对当前进程使用,但一些Ex的api可以指定任意进程