Process ID, Process handle, Window handle
I am confused about what you know and what you still need to know. Some of your statements seem to be contradicting, so I will tell you some possibilities:
1)
HAVE: Process ID, NEED: Process handle
Solution: OpenProcess()
2)
HAVE: Process handle, NEED: Process ID
Solution: GetProcessId()
3)
HAVE: Window handle, NEED: Process ID
Solution: GetWindowThreadProcessId()
4)
HAVE: Window handle, NEED: Process handle
Solution: Use 3) and then 1)
5)
HAVE: Process ID, NEED: Window handle
Solution: EnumWindows(), then in the callback function do 3) and check if it matches your process ID.
6)
HAVE: Process handle, NEED: Window handle
Solution: 2) and then 5)
http://www.programlife.net/get-main-window-handler-in-dll.html
有的时候难免需要在DLL中获取主进程的窗口句柄,比如在DLL注入的时候等等。那么如何在DLL中获取主进程的窗口句柄呢?可以通过EnumWindows来实现。先通过GetCurrentProcessId获取进程的PID,然后在EnumWindows中调用GetWindowThreadProcessId获得与窗口句柄关联的进程PID,然后对比PID,看是否相等,并判断是不是主窗口即可。
以上方法参考自网络,不一定很完善,但是通常情况下已经够用了。附上测试代码:
// Author: 代码疯子
// Blog: http://www.programlife.net/
#include <windows.h> BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD dwCurProcessId = *((DWORD*)lParam);
DWORD dwProcessId = ; GetWindowThreadProcessId(hwnd, &dwProcessId);
if(dwProcessId == dwCurProcessId && GetParent(hwnd) == NULL)
{
*((HWND *)lParam) = hwnd;
return FALSE;
}
return TRUE;
} HWND GetMainWindow()
{
DWORD dwCurrentProcessId = GetCurrentProcessId();
if(!EnumWindows(EnumWindowsProc, (LPARAM)&dwCurrentProcessId))
{
return (HWND)dwCurrentProcessId;
}
return NULL;
} BOOLEAN WINAPI DllMain(
IN HINSTANCE hDllHandle,
IN DWORD nReason,
IN LPVOID Reserved)
{
BOOLEAN bSuccess = TRUE; switch ( nReason )
{
case DLL_PROCESS_ATTACH:
MessageBox(GetMainWindow(), TEXT("OMG~ You are Attacked!"), TEXT("Warning"), MB_ICONWARNING);
break; case DLL_PROCESS_DETACH:
break;
} return bSuccess;
}
ProcessID, Process Handle, Window Handle 之间的互相转换
http://www.delphitop.com/html/jincheng/219.html
// Get ProcessID By ProgramName (Include Path or Not Include)
function GetPIDByProgramName(const APName: string): THandle;
// Get Window Handle By ProgramName (Include Path or Not Include)
function GetHWndByProgramName(const APName: string): THandle;
// Get Window Handle By ProcessID
function GetHWndByPID(const hPID: THandle): THandle;
// Get ProcessID By Window Handle
function GetPIDByHWnd(const hWnd: THandle): THandle;
// Get Process Handle By Window Handle
function GetProcessHndByHWnd(const hWnd: THandle): THandle;
// Get Process Handle By Process ID
function GetProcessHndByPID(const hAPID: THandle): THandle;
// Get Window Handle By ProgramName (Include Path or Not Include)
function GetHWndByProgramName(const APName: string): THandle;
begin
Result:=GetHWndByPID(GetPIDByProgramName(APName));
end; // Get Process Handle By Window Handle
function GetProcessHndByHWnd(const hWnd: THandle): THandle;
var
PID: DWORD;
AhProcess: THandle;
begin
if hWnd<> then
begin
GetWindowThreadProcessID(hWnd, @PID);
AhProcess := OpenProcess(PROCESS_ALL_ACCESS, false, PID);
Result:=AhProcess;
CloseHandle(AhProcess);
end
else
Result:=;
end; // Get Process Handle By Process ID
function GetProcessHndByPID(const hAPID: THandle): THandle;
var
AhProcess: THandle;
begin
if hAPID<> then
begin
AhProcess := OpenProcess(PROCESS_ALL_ACCESS, false, hAPID);
Result:=AhProcess;
CloseHandle(AhProcess);
end
else
Result:=;
end; // Get Window Handle By ProcessID
function GetPIDByHWnd(const hWnd: THandle): THandle;
var
PID: DWORD;
begin
if hWnd<> then
begin
GetWindowThreadProcessID(hWnd, @PID);
Result:=PID;
end
else
Result:=;
end; // Get Window Handle By ProcessID
function GetHWndByPID(const hPID: THandle): THandle;
type
PEnumInfo = ^TEnumInfo;
TEnumInfo = record
ProcessID: DWORD;
HWND: THandle;
end; function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
var
PID: DWORD;
begin
GetWindowThreadProcessID(Wnd, @PID);
Result := (PID <> EI.ProcessID) or
(not IsWindowVisible(WND)) or
(not IsWindowEnabled(WND)); if not Result then EI.HWND := WND; //break on return FALSE 所以要反向檢查
end; function FindMainWindow(PID: DWORD): DWORD;
var
EI: TEnumInfo;
begin
EI.ProcessID := PID;
EI.HWND := ;
EnumWindows(@EnumWindowsProc, Integer(@EI));
Result := EI.HWND;
end;
begin
if hPID<> then
Result:=FindMainWindow(hPID)
else
Result:=;
end; // Get ProcessID By ProgramName (Include Path or Not Include)
function GetPIDByProgramName(const APName: string): THandle;
var
isFound: boolean;
AHandle, AhProcess: THandle;
ProcessEntry32: TProcessEntry32;
APath: array[..MAX_PATH] of char;
begin
try
Result := ;
AHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, );
ProcessEntry32.dwSize := Sizeof(ProcessEntry32);
isFound := Process32First(AHandle, ProcessEntry32); while isFound do
begin
AhProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,
false, ProcessEntry32.th32ProcessID);
GetModuleFileNameEx(AhProcess, , @APath[], sizeof(APath)); if (UpperCase(StrPas(APath)) = UpperCase(APName)) or
(UpperCase(StrPas(ProcessEntry32.szExeFile)) = UpperCase(APName)) then
begin
Result := ProcessEntry32.th32ProcessID;
break;
end;
isFound := Process32Next(AHandle, ProcessEntry32);
CloseHandle(AhProcess);
end;
finally
CloseHandle(AHandle);
end;
end;
Process ID, Process handle, Window handle的更多相关文章
- Transaction (Process ID xxx) was deadlocked on lock
Transaction (Process ID 161) was deadlocked on lock | communication buffer resources with another pr ...
- 多线程处理sql server2008出现Transaction (Process ID) was deadlocked on lock resources with another process and has been chose问题
多线程处理sql server2008某个表中的数据时,在Update记录的时候出现了[Transaction (Process ID 146) was deadlocked on lock reso ...
- java代码中获取进程process id(转)
另一方面,线程ID=进程ID+内部线程对象ID并不成立, 参考: blog.csdn.net/heyetina/article/details/6633901 如何在java代码中获取进 ...
- check process id exists
kill -0 pid sending the signal 0 to a given PID just checks if any process with the given PID is run ...
- 进程ID[PID(Process ID)]与端口号[(Port ID)]的联系
1.首先声明一点:PID不是端口(port id),而是Process ID进程号的意思. 2.那么,什么是进程号? 采集网友的意见就是: 进程号,是系统分配给么一个进程的唯一标识符.PID就是各进程 ...
- 查看进程id, 父进程id _How do I get the parent process ID of a given child process?
How to get parent pid from a given children pid? I know I can mannully check it under /proc, I am wo ...
- open数据库报错ERROR at line 1: ORA-03113: end-of-file on communication channel Process ID: 3880 Session ID: 125 Serial number: 3
1.今天打开数据时,失败,报错 ERROR at line 1:ORA-03113: end-of-file on communication channelProcess ID: 3880Sessi ...
- [Java][Android][Process] 分享 Process 运行命令行封装类型
我在以前的文章中提到,使用Java不会有一个问题,创建运行命令来创建太多进程后创建进程行语句. [Android] ProcessBuilder与Runtime.getRuntime().exec分别 ...
- linux 终端报错 Out of memory: Kill process[PID] [process name] score问题分析
从Out of memory来看是内存超出了,后面的 Kill process[PID] [process name] score好像和进程有关了,下面我们就一起来看看linux 终端报错 Out o ...
随机推荐
- C/C++——C语言数组名与指针
版权声明:原创文章,转载请注明出处. 1. 一维数组名与指针 对于一维数组来说,数组名就是指向该数组首地址的指针,对于: ]; array就是该数组的首地址,如果我们想定义一个指向该数组的指针,我们可 ...
- Mybatis的初步使用
MyBatis 是当下最流行的持久层框架,也是ORM框架,本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google ...
- 转:google测试分享-测试经理
原文: http://blog.sina.com.cn/s/blog_6cf812be0102vode.html 前言:这个系列分享的内容大部分都是出自于<google是如何测试的>的书, ...
- SVN文件上感叹号、加号、问号等图标的原因
黄色感叹号(有冲突): --这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别 ...
- 20165301陈潭飞2017-2018-2 20165301 实验三《Java面向对象程序设计》实验报告
2017-2018-2 20165301 实验三<Java面向对象程序设计>实验报告 一.敏捷开发与XP实践-1 实验要求: 在IDEA中使用工具(Code->Reformate C ...
- Spring MVC数据绑定(二)
之前学习了SpringMVC数据绑定的基本知识和简单数据绑定以及POJO类型数据的绑定.接下来总结剩下的一些数据类型的绑定 1. 绑定包装POJO 所谓的包装POJO,就是在一个POJO中包含另一个简 ...
- 计算 Python (list or array) 相同索引元素相等的个数
上代码: a = [2, 3, 3, 1, 1, 3, 3, 3, 2, 1, 3, 1, 3, 2, 2, 2, 3, 1, 2, 3, 2, 3, 1, 1, 2, 1, 1, 1, 2, 2, ...
- Codeigniter的一些优秀实践
最近准备接手改进一个别人用Codeigniter写的项目,虽然之前也有用过CI,但是是完全按着自己的意思写的,没按CI的一些套路.用在公众的项目,最好还是按框架规范来,所以还是总结一下,免得以后别人再 ...
- 0ra-12170 tns 连接超时
https://blog.csdn.net/zhaoxiangchong/article/details/8296980
- win2008 r2 服务器php+mysql+sqlserver2008运行环境配置(从安装、优化、安全等)
这篇文章主要介绍了win2008 r2 服务器php+mysql+sqlserver2008运行环境配置(从安装.优化.安全等),需要的朋友可以参考下 win2008 r2 安装 http://www ...