Sridhar
2009-08-13 06:35:01 UTC
hi i am new to vc++ and i am using vc++6.0. my intention is to create log
file i want to write a program which has to collect all information of mouse
and keyboard events.for ex: if user click on some running application (ex:
vs2005 or sql server 2005)menu item or some button or some tool bar icon.i
want to gather the information like which button it is clicked,which icon is
clicked(in standard tool bar) what is the classname of that button or toolbar
or menu. and i also want to gather the information like text of the menu item
on which it is clicked.
i write some code but i used WH_JOURNALRECORD hook. up to now when the mouse
is clicked i am able to gather the information like classname and text name
of the toolbars(ex:application class name or toolbar class name)but i am not
able to gather the menu name,menu item name(ex:if save is clicked in file
menu i have to gather the information like save menu item is clicked etc)and
which button is clicked on standard tool bar etc.
i am posting the code which i developed, please any one suggest the
modifications in my code and how to collect the remaining information. in my
program WM_SYSCOMMAND is not working i think.is that correct what i wrote?
please help me..
Thanks in advance..
// exper1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "windows.h"
#include "winuser.h"
#include "stdio.h"
#include "assert.h"
const char g_szClassName[] = "myWindowClass";
HHOOK hook_handle;
//HINSTANCE hinstance;
//*********************************************
//****************************************
LRESULT CALLBACK JournalRecordProc(int code,WPARAM wparam,LPARAM lparam)
{
//The recording callback hook function
//SHORT val=GetKeyState(VK_PAUSE);
//If Pause/Break key is pressed, stop recording
//The other key sequence to stop recording, Ctrl+Esc automatically evicts
the hook
//so all we have to do is update the UI accordingly, that code is there in
//the GetMsgProc function
char messs[200]="classnames";
char szText[41]="assertion";
EVENTMSG *mesg=NULL;
HWND hwnd;
HMENU hMenubar;
UINT cmdmsg;
switch (code)
{
case HC_ACTION:
FILE *fp;
fp=fopen("C:\\Log.txt","ab");
mesg=(EVENTMSG *)lparam;
switch(mesg->message)
{
case WM_MOUSEMOVE:
return CallNextHookEx(hook_handle,code,wparam,lparam);
break;
case WM_SYSCOMMAND:
hMenubar=GetMenu(mesg->hwnd);
if(hMenubar!=NULL && IsMenu(hMenubar))
{
fprintf(fp,"Menu Name=");
}
GetMenuString(hMenubar,LOWORD(wparam),szText,40,MF_BYCOMMAND);
fprintf(fp,"\nSTART RECORDING....\n ");
if(szText[0]!=NULL)
{
fprintf(fp,"Menu Name=");
fprintf(fp,"\"",1);
fprintf(fp,szText,strlen(szText));
fprintf(fp,"\"\t",1);
}
else
fprintf(fp,"Menu
Name=NULL");
fprintf(fp," />\n");
//fprintf(fp1,"\nDONE\n",4);
fflush(fp);
fclose(fp);
break;
default:
fprintf(fp,"\nSTART RECORDING....\n ");
GetClassName(mesg->hwnd,messs,200);
fprintf(fp,"<log ");
assert(messs[0]!=NULL);
if(messs[0]!=NULL)
{
fprintf(fp,"Class Name=");
fprintf(fp,"\"");
fprintf(fp,messs,strlen(messs));
fprintf(fp,"\"\t");
}
else
fprintf(fp,"Class Name=NULL");
GetWindowText(mesg->hwnd,messs,200);
fprintf(fp,"\t",1);
assert(messs[0]!=NULL);
if(messs[0]==NULL)
{
fprintf(fp,"Window Text=NULL");
}
else
{
fprintf(fp,"Window Text=");
fprintf(fp,"\"",1);
fprintf(fp,messs,strlen(messs));
fprintf(fp,"\"");
}
fprintf(fp,"\t");
hwnd = GetParent(mesg->hwnd);
while (hwnd!=0)
{
GetClassName(hwnd,messs,200);
assert(messs[0]!=NULL);
if(messs[0]!=NULL)
{
fprintf(fp,"Parent Class Name=");
fprintf(fp,"\"");
fprintf(fp,messs,strlen(messs));
fprintf(fp,"\"");
}
else
fprintf(fp,"Parent Class Name=NULL");
GetWindowText(hwnd,messs,200);
fprintf(fp,"\t");
assert(messs[0]!=NULL);
if(messs[0]==NULL)
{
fprintf(fp,"Parent Window Text=NULL");
}
else
{
fprintf(fp,"Parent Window Text=");
fprintf(fp,"\"");
fprintf(fp,messs,strlen(messs));
fprintf(fp,"\"");
}
fprintf(fp,"\t");
hwnd = GetParent (hwnd);
}
fprintf(fp," />\n");
//fprintf(fp,"\nDONE\n",4);
fflush(fp);
fclose(fp);
break;
}
default:
return CallNextHookEx(hook_handle,code,wparam,lparam);
}
return 0;
}
//***************************************
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
//DoMouseClick(hwnd,msg,wParam,lParam);
//hook_handle=SetWindowsHookEx(WH_JOURNALRECORD,JournalRecordProc,hinstance,0);
}
break;
case WM_CLOSE:
UnhookWindowsHookEx(hook_handle);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, SW_MINIMIZE);
UpdateWindow(hwnd);
hook_handle=SetWindowsHookEx(WH_JOURNALRECORD,JournalRecordProc,hInstance,0);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
//UnhookWindowsHookEx(g_hMouseHook);
//return (int) Msg.wParam;
return 0;
}
file i want to write a program which has to collect all information of mouse
and keyboard events.for ex: if user click on some running application (ex:
vs2005 or sql server 2005)menu item or some button or some tool bar icon.i
want to gather the information like which button it is clicked,which icon is
clicked(in standard tool bar) what is the classname of that button or toolbar
or menu. and i also want to gather the information like text of the menu item
on which it is clicked.
i write some code but i used WH_JOURNALRECORD hook. up to now when the mouse
is clicked i am able to gather the information like classname and text name
of the toolbars(ex:application class name or toolbar class name)but i am not
able to gather the menu name,menu item name(ex:if save is clicked in file
menu i have to gather the information like save menu item is clicked etc)and
which button is clicked on standard tool bar etc.
i am posting the code which i developed, please any one suggest the
modifications in my code and how to collect the remaining information. in my
program WM_SYSCOMMAND is not working i think.is that correct what i wrote?
please help me..
Thanks in advance..
// exper1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "windows.h"
#include "winuser.h"
#include "stdio.h"
#include "assert.h"
const char g_szClassName[] = "myWindowClass";
HHOOK hook_handle;
//HINSTANCE hinstance;
//*********************************************
//****************************************
LRESULT CALLBACK JournalRecordProc(int code,WPARAM wparam,LPARAM lparam)
{
//The recording callback hook function
//SHORT val=GetKeyState(VK_PAUSE);
//If Pause/Break key is pressed, stop recording
//The other key sequence to stop recording, Ctrl+Esc automatically evicts
the hook
//so all we have to do is update the UI accordingly, that code is there in
//the GetMsgProc function
char messs[200]="classnames";
char szText[41]="assertion";
EVENTMSG *mesg=NULL;
HWND hwnd;
HMENU hMenubar;
UINT cmdmsg;
switch (code)
{
case HC_ACTION:
FILE *fp;
fp=fopen("C:\\Log.txt","ab");
mesg=(EVENTMSG *)lparam;
switch(mesg->message)
{
case WM_MOUSEMOVE:
return CallNextHookEx(hook_handle,code,wparam,lparam);
break;
case WM_SYSCOMMAND:
hMenubar=GetMenu(mesg->hwnd);
if(hMenubar!=NULL && IsMenu(hMenubar))
{
fprintf(fp,"Menu Name=");
}
GetMenuString(hMenubar,LOWORD(wparam),szText,40,MF_BYCOMMAND);
fprintf(fp,"\nSTART RECORDING....\n ");
if(szText[0]!=NULL)
{
fprintf(fp,"Menu Name=");
fprintf(fp,"\"",1);
fprintf(fp,szText,strlen(szText));
fprintf(fp,"\"\t",1);
}
else
fprintf(fp,"Menu
Name=NULL");
fprintf(fp," />\n");
//fprintf(fp1,"\nDONE\n",4);
fflush(fp);
fclose(fp);
break;
default:
fprintf(fp,"\nSTART RECORDING....\n ");
GetClassName(mesg->hwnd,messs,200);
fprintf(fp,"<log ");
assert(messs[0]!=NULL);
if(messs[0]!=NULL)
{
fprintf(fp,"Class Name=");
fprintf(fp,"\"");
fprintf(fp,messs,strlen(messs));
fprintf(fp,"\"\t");
}
else
fprintf(fp,"Class Name=NULL");
GetWindowText(mesg->hwnd,messs,200);
fprintf(fp,"\t",1);
assert(messs[0]!=NULL);
if(messs[0]==NULL)
{
fprintf(fp,"Window Text=NULL");
}
else
{
fprintf(fp,"Window Text=");
fprintf(fp,"\"",1);
fprintf(fp,messs,strlen(messs));
fprintf(fp,"\"");
}
fprintf(fp,"\t");
hwnd = GetParent(mesg->hwnd);
while (hwnd!=0)
{
GetClassName(hwnd,messs,200);
assert(messs[0]!=NULL);
if(messs[0]!=NULL)
{
fprintf(fp,"Parent Class Name=");
fprintf(fp,"\"");
fprintf(fp,messs,strlen(messs));
fprintf(fp,"\"");
}
else
fprintf(fp,"Parent Class Name=NULL");
GetWindowText(hwnd,messs,200);
fprintf(fp,"\t");
assert(messs[0]!=NULL);
if(messs[0]==NULL)
{
fprintf(fp,"Parent Window Text=NULL");
}
else
{
fprintf(fp,"Parent Window Text=");
fprintf(fp,"\"");
fprintf(fp,messs,strlen(messs));
fprintf(fp,"\"");
}
fprintf(fp,"\t");
hwnd = GetParent (hwnd);
}
fprintf(fp," />\n");
//fprintf(fp,"\nDONE\n",4);
fflush(fp);
fclose(fp);
break;
}
default:
return CallNextHookEx(hook_handle,code,wparam,lparam);
}
return 0;
}
//***************************************
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
//DoMouseClick(hwnd,msg,wParam,lParam);
//hook_handle=SetWindowsHookEx(WH_JOURNALRECORD,JournalRecordProc,hinstance,0);
}
break;
case WM_CLOSE:
UnhookWindowsHookEx(hook_handle);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, SW_MINIMIZE);
UpdateWindow(hwnd);
hook_handle=SetWindowsHookEx(WH_JOURNALRECORD,JournalRecordProc,hInstance,0);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
//UnhookWindowsHookEx(g_hMouseHook);
//return (int) Msg.wParam;
return 0;
}
--
--------------------------------------------
At last i did something for today...:)
--------------------------------------------
At last i did something for today...:)