获取其他进程

在一般实战中,我们都需要去控制已有的正常程序的运行流程,那么首先要获取其他进程的句柄

获取系统里正在运行的进程

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可以指定任意进程

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值