一 test.exe中的函数
//安装钩子
m_Hook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,GetModuleHandle(_T("test.dll")),0);
二.test.dll中的函数(DLL静态加载的)
//键盘钩子过程函数 导出名为KeyboardProc
LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam)
{
if(wParam==VK_F12)
m_hHwnd=GetForegroundWindow();//获取当前窗口句柄
return CallNextHookEx(NULL,code,wParam,lParam);
}
//exe 中通过此函数返回值获得句柄
HWND m_fGetHwnd(void)
{
return m_hHwnd;
}
在这之前也做了个测试,在没有安装钩子的情况下,是可以通过调用DLL中的m_fGetHwnd获得当前窗口的句柄的。安装了钩子以后为神秘就获取不到句柄了。
用HOOK获取其他程序窗口句柄的问题
答案:2 悬赏:80 手机版
解决时间 2021-02-26 21:27
- 提问者网友:贪了杯
- 2021-02-26 02:12
最佳答案
- 五星知识达人网友:西风乍起
- 2021-02-26 02:40
'代码写得有些乱,凑合着看吧.
'模块代码,里面有一些没用到的API可以删了.
Option Explicit
Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.DLL" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Declare Function Process32First Lib "KERNEL32.DLL" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "KERNEL32.DLL" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Declare Function SetWinEventHook Lib "user32.dll" (ByVal eventMin As Long, ByVal eventMax As Long, ByVal hmodWinEventProc As Long, ByVal pfnWinEventProc As Long, ByVal IdProcess As Long, ByVal idThread As Long, ByVal dwFlags As Long) As Long
Public Declare Function UnhookWinEvent Lib "user32.dll" (ByVal hWinEventHook As Long) As Long
Public Const WINEVENT_OUTOFCONTEXT = &H0&
Public Const WINEVENT_SKIPOWNPROCESS = &H2&
Public Const EVENT_SYSTEM_MENUPOPUPSTART = &H6&
Public Const TH32CS_SNAPHEAPLIST = &H1
Public Const TH32CS_SNAPPROCESS = &H2
Public Const TH32CS_SNAPTHREAD = &H4
Public Const TH32CS_SNAPMODULE = &H8
Public Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Public Const TH32CS_INHERIT = &H80000000
Public Const MAX_PATH As Integer = 260
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Dim hEventHook As Long
Public Sub WINEVENTPROC(ByVal hWinEventHook As Long, _
ByVal lngevent As Long, _
ByVal hwnd As Long, _
ByVal idObject As Long, _
ByVal idChild As Long, _
ByVal idEventThread As Long, _
ByVal dwmsEventTime As Long)
'这里的 hwnd 就是 菜单窗体的句柄
Call MoveWindow(hwnd, 0, 0, 200, 200, -1) '测试
End Sub
Public Function SetHook(ByVal IdProcess As Long) As Long
hEventHook = SetWinEventHook(EVENT_SYSTEM_MENUPOPUPSTART, EVENT_SYSTEM_MENUPOPUPSTART, 0&, AddressOf WINEVENTPROC, IdProcess&, 0&, WINEVENT_OUTOFCONTEXT Or WINEVENT_SKIPOWNPROCESS)
SetHook = hEventHook
End Function
Public Sub UnSetHook()
If hEventHook Then
Call UnhookWinEvent(hEventHook)
hEventHook = 0
End If
End Sub
Function GetProcessID(ByVal sName As String) As Long
Dim myhProcess As Long
Dim mype As PROCESSENTRY32
Dim mybRet As Long
myhProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
mype.dwSize = Len(mype)
mybRet = Process32First(myhProcess, mype)
Do While mybRet
If InStr(LCase(mype.szExeFile), sName) Then
GetProcessID = mype.th32ProcessID
Call CloseHandle(myhProcess)
Exit Function
Else
mybRet = Process32Next(myhProcess, mype)
End If
Loop
Call CloseHandle(myhProcess)
End Function
'-------------------------------------
'窗体代码
Private Sub Command1_Click()
Dim IdProcess As Long
IdProcess = GetProcessID("notepad.exe")
If IdProcess Then
If SetHook(IdProcess) Then
Command1.Enabled = False
Command2.Enabled = True
End If
End If
End Sub
Private Sub Command2_Click()
Call UnSetHook
Command1.Enabled = True
Command2.Enabled = False
End Sub
'模块代码,里面有一些没用到的API可以删了.
Option Explicit
Public Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.DLL" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Declare Function Process32First Lib "KERNEL32.DLL" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "KERNEL32.DLL" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Declare Function SetWinEventHook Lib "user32.dll" (ByVal eventMin As Long, ByVal eventMax As Long, ByVal hmodWinEventProc As Long, ByVal pfnWinEventProc As Long, ByVal IdProcess As Long, ByVal idThread As Long, ByVal dwFlags As Long) As Long
Public Declare Function UnhookWinEvent Lib "user32.dll" (ByVal hWinEventHook As Long) As Long
Public Const WINEVENT_OUTOFCONTEXT = &H0&
Public Const WINEVENT_SKIPOWNPROCESS = &H2&
Public Const EVENT_SYSTEM_MENUPOPUPSTART = &H6&
Public Const TH32CS_SNAPHEAPLIST = &H1
Public Const TH32CS_SNAPPROCESS = &H2
Public Const TH32CS_SNAPTHREAD = &H4
Public Const TH32CS_SNAPMODULE = &H8
Public Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Public Const TH32CS_INHERIT = &H80000000
Public Const MAX_PATH As Integer = 260
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Dim hEventHook As Long
Public Sub WINEVENTPROC(ByVal hWinEventHook As Long, _
ByVal lngevent As Long, _
ByVal hwnd As Long, _
ByVal idObject As Long, _
ByVal idChild As Long, _
ByVal idEventThread As Long, _
ByVal dwmsEventTime As Long)
'这里的 hwnd 就是 菜单窗体的句柄
Call MoveWindow(hwnd, 0, 0, 200, 200, -1) '测试
End Sub
Public Function SetHook(ByVal IdProcess As Long) As Long
hEventHook = SetWinEventHook(EVENT_SYSTEM_MENUPOPUPSTART, EVENT_SYSTEM_MENUPOPUPSTART, 0&, AddressOf WINEVENTPROC, IdProcess&, 0&, WINEVENT_OUTOFCONTEXT Or WINEVENT_SKIPOWNPROCESS)
SetHook = hEventHook
End Function
Public Sub UnSetHook()
If hEventHook Then
Call UnhookWinEvent(hEventHook)
hEventHook = 0
End If
End Sub
Function GetProcessID(ByVal sName As String) As Long
Dim myhProcess As Long
Dim mype As PROCESSENTRY32
Dim mybRet As Long
myhProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
mype.dwSize = Len(mype)
mybRet = Process32First(myhProcess, mype)
Do While mybRet
If InStr(LCase(mype.szExeFile), sName) Then
GetProcessID = mype.th32ProcessID
Call CloseHandle(myhProcess)
Exit Function
Else
mybRet = Process32Next(myhProcess, mype)
End If
Loop
Call CloseHandle(myhProcess)
End Function
'-------------------------------------
'窗体代码
Private Sub Command1_Click()
Dim IdProcess As Long
IdProcess = GetProcessID("notepad.exe")
If IdProcess Then
If SetHook(IdProcess) Then
Command1.Enabled = False
Command2.Enabled = True
End If
End If
End Sub
Private Sub Command2_Click()
Call UnSetHook
Command1.Enabled = True
Command2.Enabled = False
End Sub
全部回答
- 1楼网友:撞了怀
- 2021-02-26 03:32
user32.dll的函数可能在一些非界面进程中无效
给个完整代码看看
wangtk1982@163.com
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯