Win32 - Windows Messages problems
Bonjour a tous !
J'ai commencé un projet ou je souhaiterai pouvoir faire poper le clavier virtuel ( sa cest la partie facile ) au moment ou l'utilisateur clique sur un controleur ou il est possible d'ecrire ( textbox dans une page web, notepad, words, cellule excel, .... )( sa cest la partie ou j'ai des soucis ).
Apres enquete j'ai decouvert les "Windows Messages", j'arrive à recuperer le processus sur lequel le focus est :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| HWND GetGlobalFocus()
{
// remember focus window for the current thread
HWND hWndLocalFocus = GetFocus();
// find foreground window
HWND hWndFore = GetForegroundWindow();
if (hWndFore == NULL)
return NULL;
// get IDs of the current thread and the thread that
// owns foreground window
DWORD dwCurrID = GetCurrentThreadId();
DWORD dwForeID = GetWindowThreadProcessId(hWndFore, NULL);
// if the current thread owns foreground window then just
// return hWndLocalFocus
if (dwForeID == dwCurrID)
return hWndLocalFocus;
// attach input states of the current thread and the foreground
// thread
if (!AttachThreadInput(dwCurrID, dwForeID, TRUE))
return NULL;
// now the current thread and the foreground thread have common
// input state and we can query for a focus window
HWND hWndGlobalFocus = GetFocus();
// detach threads
AttachThreadInput(dwCurrID, dwForeID, FALSE);
// restore local focus
SetFocus(hWndLocalFocus);
return hWndGlobalFocus;
}
unsigned long GetTargetThreadIdFromWindow()
{
HWND targetWnd;
HANDLE hProcess;
unsigned long processID = 0;
targetWnd = GetGlobalFocus();
return GetWindowThreadProcessId(targetWnd, &processID);
} |
et donc j'injecte ma petite DLL dedans :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| HINSTANCE hinst = LoadLibrary(L"WindowsHookDLL.dll");
MSG uMsg;
cout << "load DLL: " << hinst << endl;
system("PAUSE");
if(!hinst){
cout << "ERROR DLL" << endl;
system("PAUSE");
return 0;
}
typedef void (*Install)(unsigned long);
typedef void (*Uninstall)();
Install install = (Install) GetProcAddress(hinst, "install");
Uninstall uninstall = (Uninstall) GetProcAddress(hinst, "uninstall");
while(true){
system("CLS");
PrintActualWindow();
unsigned long threadID = GetTargetThreadIdFromWindow();
cout << "ThreadID: "<< threadID<<endl;
install(threadID);
//Sleep(2000);
}
uninstall();
system("PAUSE");
return 0; |
petite DLL qui ecoute les windows messages :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <iostream>
//#include <string>
#include <windows.h>
#include <fstream>
#include "WindowsMessages.h";
using namespace std;
HINSTANCE hinst;
#pragma data_seg(".shared")
HHOOK hhk = NULL;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.shared,RWS")
LRESULT CALLBACK MessageHookProcedure(int code, WPARAM wParam, LPARAM lParam){
WindowsMessages WM = WindowsMessages(((CWPSTRUCT*)lParam)->message);
boolean jump = WM.getMessageJump();
if(!jump){
ofstream myfile;
myfile.open ("D:/test.txt", ios::out | ios::app );
myfile << "get: " << WM.getMessageCode() << " " << WM.getMessageName() << "\n";
myfile.close();
}
return CallNextHookEx(hhk,code,wParam,lParam);
}
extern "C" __declspec(dllexport) void install() {
hhk = SetWindowsHookEx(WH_CALLWNDPROC, MessageHookProcedure, hinst, NULL);
}
extern "C" __declspec(dllexport) void uninstall() {
UnhookWindowsHookEx(hhk);
}
BOOL WINAPI DllMain( __in HINSTANCE hinstDLL, __in DWORD fdwReason, __in LPVOID lpvReserved){
hinst = hinstDLL;
return TRUE;
} |
ma class WindowsMessages cest juste histoire d'avoir tous les windows messages ID ( le numero hexa ) et le nom en chaine de caractere, histoire de pouvoir lire le log.
Le truc cest que je recoie des milliards de messages different , je sais pas lequel je dois attraper ou meme si ce message existe.
Des idées ?
Merci !
J'ai posté la aussi parce que je sais pas trop ou est le meilleur endroit :
http://www.developpez.net/forums/d12...s/#post6764361