C++用PostMessage模拟按钮点击

本文介绍如何在C++中利用PostMessage函数模拟按钮点击事件,特别是在MFC程序中。通过指定进程名、消息类型、资源ID和事件类型,可以实现对目标窗口按钮的模拟点击操作。详细调用示例和所需库文件链接已提供。
摘要由CSDN通过智能技术生成

有时我们可能会在某个程序中用到模拟按钮点击事件。

本文中的例子在MFC程序中调试通过,duilib的没试过,还需探索

不多说,上代码:

  1 #include "stdafx.h"
  2 #include "windows.h"
  3 #include "Psapi.h"
  4 #include "atlstr.h"
  5 #include <string.h>
  6 #include<iostream>
  7 using namespace std;
  8 
  9 #pragma comment(lib,"Psapi");
 10 #define ID_EXEC_CURFILE 32807
 11 #define ID_RCL_HELP 32804
 12 CString str;
 13 
 14 //根据exe名字一部分查找进程id,返回
 15 DWORD FindProcess(char *strProcessName)  
 16 {  
 17     DWORD aProcesses[1024], cbNeeded, cbMNeeded;  
 18     HMODULE hMods[1024];  
 19     HANDLE hProcess;  
 20     char szProcessName[MAX_PATH];  
 21     if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return 0;  
 22     for(int i=0; i< (int) (cbNeeded / sizeof(DWORD)); i++)  
 23     {  
 24         hProcess = OpenProcess( /*PROCESS_QUERY_INFORMATION | PROCESS_VM_READ*/PROCESS_ALL_ACCESS, FALSE, aProcesses[i]);  
 25         if (hProcess == NULL)
 26             continue;
 27         EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbMNeeded);  
 28         GetModuleFileNameEx( hProcess, hMods[0], szProcessName,sizeof(szProcessName));  
 29 
 30         
The PostMessage function is used to post a message to the message queue of a thread. This function is one of the ways to communicate between different threads in Windows. To hook the PostMessage function, you can use the Detours library provided by Microsoft. Detours allows you to intercept function calls and redirect them to your own function. Here's an example of how to hook the PostMessage function using Detours: ```c++ #include <windows.h> #include <detours.h> // Define a function pointer for the original PostMessage function typedef BOOL (WINAPI *POSTMESSAGE)(HWND, UINT, WPARAM, LPARAM); // Define a global variable to hold the address of the original PostMessage function POSTMESSAGE g_pOrigPostMessage = NULL; // Define your own PostMessage function BOOL WINAPI MyPostMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { // Do something before calling the original PostMessage function // ... // Call the original PostMessage function BOOL bRet = g_pOrigPostMessage(hWnd, Msg, wParam, lParam); // Do something after calling the original PostMessage function // ... return bRet; } // Hook the PostMessage function BOOL HookPostMessage() { // Get the address of the original PostMessage function HMODULE hUser32 = GetModuleHandle("user32.dll"); g_pOrigPostMessage = (POSTMESSAGE)GetProcAddress(hUser32, "PostMessageA"); // Detour the PostMessage function to your own function if (DetourTransactionBegin() == NO_ERROR) { if (DetourUpdateThread(GetCurrentThread()) == NO_ERROR) { if (DetourAttach(&(PVOID&)g_pOrigPostMessage, MyPostMessage) == NO_ERROR) { if (DetourTransactionCommit() == NO_ERROR) { return TRUE; } } } } return FALSE; } // Unhook the PostMessage function BOOL UnhookPostMessage() { // Detour the PostMessage function back to the original function if (DetourTransactionBegin() == NO_ERROR) { if (DetourUpdateThread(GetCurrentThread()) == NO_ERROR) { if (DetourDetach(&(PVOID&)g_pOrigPostMessage, MyPostMessage) == NO_ERROR) { if (DetourTransactionCommit() == NO_ERROR) { return TRUE; } } } } return FALSE; } // Example usage int main() { // Hook the PostMessage function if (HookPostMessage()) { // Call the PostMessage function PostMessage(NULL, WM_USER, 0, 0); // Unhook the PostMessage function UnhookPostMessage(); } return 0; } ``` Note that the Detours library is not officially supported by Microsoft and is intended for research and experimentation purposes only. Use it at your own risk.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值