Доброго времени суток
Ставлю глобальный хук на окно приложения (функция-фильтр в dll). Надо записывать события мыши и клавиатуры для пересылки по сети и воспроизведения на другой машине. Пока работаю с записью. Прога и dll компилятся , но не работают как надо. Помогите найти ошибку, буду очень благодарна.
Вот dll:
#define _AFXDLL
#define EXPORT extern "C" __declspec(dllexport)
#include "afxwin.h"
#include "stdafx.h"
#include <windows.h>
#include <WinUser.h>
#include <stdio.h>
LPEVENTMSG EventMSG;
HHOOK hook;
__declspec(dllexport) LRESULT CALLBACK JournalRecordProc(int, WPARAM, LPEVENTMSG);
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
default:
return TRUE;
}
return FALSE;
}
__declspec(dllexport) LRESULT CALLBACK JournalRecordProc (int nCode, WPARAM wp, LPEVENTMSG EventMSG)
{
FILE* fp;
fprintf (fp, "nCode = %d\n", nCode);
if (nCode >= 0)
{
fp = fopen ("journal_temp.txt","a");
fprintf(fp, "%d\n", EventMSG->message);
fprintf(fp, "%d\n", EventMSG->paramL);
fprintf(fp, "%d\n", EventMSG->paramH);
fprintf(fp, "%s\n", EventMSG->time);
fprintf(fp, "%s\n", EventMSG->hwnd);
fprintf(fp, "%x\n", EventMSG->hwnd);
fclose (fp);
return CallNextHookEx(hook, nCode, wp, EventMSG);
}
else
{
fclose (fp);
return CallNextHookEx(hook, nCode, wp, EventMSG);
}
}
Вот прога:
#pragma once
#define _AFXDLL
#define WINVER 0x0501
#include "afxwin.h"
#include <windows.h>
//#include "stdafx.h"
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HBITMAP handle;
BITMAP bit;
CBitmap *bm;
/////////////////////////////////////////////////////////////////
HMODULE hdll;
HOOKPROC hkprc;
static HHOOK hook;
FILE *fp;
////////////////////////////////////////////////////////////////
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK EnumWndProc(HWND hwnd, LPARAM lParam)
{
if(GetWindowThreadProcessId(hwnd, NULL) == GetCurrentThreadId())
{
*(HWND*)lParam = hwnd;
return FALSE;
}
return TRUE;
}
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
fp = fopen ("file1.txt", "a");
// загрузка библиотеки
hdll = LoadLibrary ("hook_filtr.dll");
if (hdll == 0)
fprintf (fp, "SCUKKO!\n");
else
fprintf (fp, "hdll = %d\n", hdll);
MyRegisterClass(hInstance);
InitInstance(hInstance, nCmdShow);
//hkprcSysMsg = (HOOKPROC)GetProcAddress(hdll, "JournalRecordProc");
//hhookSysMsg = SetWindowsHookEx(WH_SYSMSGFILTER, hkprcSysMsg, hdll, 0);
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(hook);
FreeLibrary (hdll);
return 0;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASS wcex;
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = (LPCTSTR)"Class";
return RegisterClass(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
handle = (HBITMAP)LoadImage(GetModuleHandle(0), "pict1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
bm = CBitmap::FromHandle(handle);
bm->GetBitmap(&bit);
hWnd = CreateWindow((LPCTSTR)"Class", (LPCTSTR)"Title", WS_OVERLAPPEDWINDOW,
0, 0, bit.bmWidth, bit.bmHeight, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
BITMAP bmpInfo = {};
WINDOWINFO w_info;
switch (message)
{
case WM_CREATE:
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
{
CClientDC cdc(NULL);
CDC dc;
dc.CreateCompatibleDC(&cdc);
dc.SelectObject(bm);
GetWindowInfo(hWnd, &w_info);
cdc.StretchBlt (w_info.rcWindow.left, w_info.rcWindow.top, (w_info.rcWindow.right-w_info.rcWindow.left),
(w_info.rcWindow.bottom-w_info.rcWindow.top),
&dc, 0, 0, bit.bmWidth, bit.bmHeight, SRCCOPY);
dc.DeleteDC();
}
EndPaint(hWnd, &ps);
hkprc = (HOOKPROC)GetProcAddress(hdll, "JournalRecordProc");
fprintf (fp,"hkprc = %d\n", hkprc);
fclose (fp);
hook = SetWindowsHookEx(WH_JOURNALRECORD, hkprc, hdll, 0);
break;
case WM_DESTROY:
//UnhookWindowsHookEx(hhook);
//FreeLibrary (hdll);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}