起因发现有些进程窗口名随着自己的角色变化,或者每次启动都给一个随机窗口名称?
当然无所谓了,进程名称没换就行-。-
没什么技术可言。见笑了。
为了方便大家代码贴上
源码上传。
这是头文件。
[C++] 纯文本查看 复制代码 #pragma once
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
BOOL IsMainWindow(HWND handle);
BOOL CALLBACK EnumWindowsCallback(HWND handle, LPARAM lParam);
HWND FindMainWindow(unsigned long process_id);
PROCESSENTRY32 FindProcessname(const wchar_t *name);
BOOL check64ProcessEx(HANDLE hProcess);
CPP文件.
[C++] 纯文本查看 复制代码
#include "FindPID.h"
struct DATA
{
unsigned long process_id;
HWND Handle;
};
BOOL IsMainWindow(HWND handle)
{
return GetWindow(handle, GW_OWNER) == (HWND)0 && IsWindowVisible(handle);
}
BOOL CALLBACK EnumWindowsCallback(HWND handle, LPARAM lParam)
{
DATA& data = *(DATA*)lParam;
unsigned long process_id = 0;
GetWindowThreadProcessId(handle, &process_id);
if (data.process_id != process_id || !IsMainWindow(handle)) {
return TRUE;
}
data.Handle = handle;
return FALSE;
}
HWND FindMainWindow(unsigned long process_id)
{
DATA data;
data.process_id = process_id;
data.Handle = 0;
EnumWindows(EnumWindowsCallback, (LPARAM)&data);
return data.Handle;
}
PROCESSENTRY32 FindProcessname(const wchar_t *name)
{
int count = 0;
PROCESSENTRY32 c_Process;
c_Process.dwSize = sizeof(c_Process);
HANDLE hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcess == INVALID_HANDLE_VALUE)
return c_Process = {};
bool bMore = Process32First(hProcess, &c_Process);
while (bMore)
{
if (_tcsstr(c_Process.szExeFile, name))
return c_Process;
bMore = Process32Next(hProcess, &c_Process);
count++;
}
CloseHandle(hProcess);
return c_Process = {};
}
BOOL check64ProcessEx(HANDLE hProcess)
{
bool X86 = false;
#ifndef _WIN64
X86 = GetProcAddress(GetModuleHandle(TEXT("ntdll")), "NtWow64DebuggerCall") == nullptr ? TRUE : FALSE;
#endif
if (X86)
return FALSE;
typedef BOOL(WINAPI *ISWOW64PROCESS)(HANDLE, PBOOL);
ISWOW64PROCESS W64Process;
BOOL check64 = TRUE;
W64Process = (ISWOW64PROCESS)GetProcAddress(GetModuleHandle(L"kernel32"), "IsWow64Process");
if (W64Process != nullptr)
W64Process(hProcess, &check64);
return !check64;
}
调用方法。
[C++] 纯文本查看 复制代码 #include "FindPID.h"
int main()
{
//需要模糊查找的文件
const wchar_t *name = L"note";
//返回一个结构体
PROCESSENTRY32 C_Process= FindProcessname(name);
if (C_Process.th32ProcessID && C_Process.szExeFile)
{
wcout << "PID=" << C_Process.th32ProcessID << "\tname=" << C_Process.szExeFile << endl;
}
//返回进程窗口句柄
HWND hwnd = FindMainWindow(C_Process.th32ProcessID);
if (hwnd)
cout << hwnd << endl;
//返回进程句柄
HANDLE hpros = OpenProcess(PROCESS_ALL_ACCESS, FALSE, C_Process.th32ProcessID);
if (hpros)
cout << hpros << endl;
//返回真为64位
//这里有点逻辑错误,单独拿出来的话。因为只有真假。除非和上面的返回值一起判断。
bool _ture_64 = check64ProcessEx(hpros);
if (_ture_64)
cout << "64位程序" << endl;
else
cout << "32位程序" << endl;
system("pause");
return 0;
}
源码下载。
https://pan.lanzou.com/i0tuu2b
密码:wuai |