Ciao! Ho creato una dll che contiene un hook globale ed uno di basso livello per intercettare il messaggio del mouse. Questo è il codice
Libreria.h
codice:
#ifndef LIBRERIA
#define LIBRERIA extern "C" __declspec(dllimport)
#endif
LRESULT CALLBACK MouseProc( __in int nCode, __in WPARAM wParam, __in LPARAM lParam);
LRESULT CALLBACK LowLevelMouseProc( __in int nCode, __in WPARAM wParam, __in LPARAM lParam);
Libreria.cpp
codice:
#include "stdafx.h"
#include <memory.h>
#define LIBRERIA extern "C" __declspec(dllexport)
#include "Libreria.h"
#pragma data_seg("Shared") static int messaggi;
#pragma data_seg( )
#pragma comment(linker,"/section:Shared,rws")
//hook globale per gli eventi di tipo mouse
LRESULT CALLBACK MouseProc( __in int nCode, __in WPARAM wParam, __in LPARAM lParam){
//deve incrementare il numero di messaggi presi dalle applicazioni
messaggi--;
return CallNextHookEx(0,nCode,wParam,lParam);
}
//hook a basso livello per gli eventi di tipo mouse
LRESULT CALLBACK LowLevelMouseProc( __in int nCode, __in WPARAM wParam, __in LPARAM lParam){
//deve incrementare il numero di messaggi a basso livello
messaggi++;
return CallNextHookEx(0,nCode,wParam,lParam);
}
Poi ho creato un'applicazione che importa dinamicamente la dll:
codice:
#include "stdafx.h"
#include "Applicazione.h"
#include "KStatistica.h"
#include <Winbase.h>
#define MAX_LOADSTRING 100
// Variabili globali:
HINSTANCE hInst; // istanza corrente
TCHAR szTitle[MAX_LOADSTRING]; // Testo della barra del titolo TCHAR szWindowClass[MAX_LOADSTRING]; // nome della classe di finestre principale
// Dichiarazioni con prototipo delle funzioni incluse in questo modulo di codice:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: inserire qui il codice.
MSG msg;
HACCEL hAccelTable; // Inizializzare le stringhe globali
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_APPLICAZIONE, szWindowClass, MAX_LOADSTRING); hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_APPLICAZIONE)); //creo la finestra KStatistica ks=KStatistica(szWindowClass,szTitle,40,40,500,500,NULL,hInstance);
//installo gli hook
HOOKPROC hkprcSysMsg;
static HINSTANCE hinstDLL;
static HHOOK hhookMouseProc;
hinstDLL = LoadLibrary(TEXT("Es3.dll"));
if(hinstDLL==NULL){
DWORD s=GetLastError();
return 0;
}
if((hkprcSysMsg=(HOOKPROC)GetProcAddress(hinstDLL, "MouseProc"))==NULL){
DWORD s=GetLastError();
return 0; //fallita installazione dell'hook
}
if(SetWindowsHookEx(WH_MOUSE,hkprcSysMsg,hinstDLL,0)==NULL){
DWORD s=GetLastError();
return 0; //fallita installazione dell'hook
}
hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "LowLevelMouseProc"); if(SetWindowsHookEx(WH_MOUSE_LL,hkprcSysMsg,hinstDLL,0)==NULL)
return 0; //fallita installazione dell'hook
// Ciclo di messaggi principale:
while (GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
Il problema è che quando faccio
codice:
hkprcSysMsg=(HOOKPROC)GetProcAddress(hinstDLL, "MouseProc")
mi viene restituito NULL!!!!!!
Accetto volentieri consigli perchè non riesco davvero a trovare l'errore!!! Grazie a tutti!