Hi Ulrich, thanks for ur reply..
actally why i am writing this program is to get the classname and text where
the mouse is clicked or if any menu item is activated by shortcut keys...i
want to record which menu item and class name of that menu etc.. simply
speacking i want ot capture all the events that are occured with their
classname and text(if exists) and write in to a file.
i think it is better to provide my code so that you can understand by
code..please see below complete code. copy the code and execute it.
Please help me on this..
// 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]="";
char szText[41]="";
EVENTMSG *mesg=NULL;
HWND hwnd;
HMENU hMenubar;
// UINT cmdmsg;
switch (code)
{
case HC_ACTION:
//The main part. HC_ACTION is called whenever there is a mouse or keyboard
event.
//lparam is a pointer to EVENTMSG struct, which contains the necessary
information
//to replay the message.
mesg=(EVENTMSG *)lparam;
switch(mesg->message)
{
/*
case WM_MOUSEMOVE:
FILE *fp1;
fp1=fopen("C:\\Log.txt","ab");
hMenubar=GetMenu(mesg->hwnd);
//if(hMenubar!=NULL && IsMenu(hMenubar))
//{
// fprintf(fp,"Menu Name=");
//}
fprintf(fp1,"Menu Name=");
if(IsMenu(hMenubar)!=0)
{
GetMenuString(hMenubar,LOWORD(wparam),szText,40,MF_BYCOMMAND);
fprintf(fp1,"\nSTART RECORDING....\n ");
if(szText[0]!=NULL)
{
fprintf(fp1,"Menu Name=");
fprintf(fp1,"\"",1);
fprintf(fp1,szText,strlen(szText));
fprintf(fp1,"\"\t",1);
}
else
fprintf(fp1,"Menu Name=NULL");
fprintf(fp1," />\n");
fflush(fp1);
fclose(fp1);
}
break;
//return CallNextHookEx(hook_handle,code,wparam,lparam);
*/
/*
case WM_COMMAND:
FILE *fp1;
fp1=fopen("C:\\Log.txt","ab");
hMenubar=GetMenu(mesg->hwnd);
//if(hMenubar!=NULL && IsMenu(hMenubar))
//{
// fprintf(fp,"Menu Name=");
//}
fprintf(fp1,"Menu Name=");
if(IsMenu(hMenubar)!=0)
{
GetMenuString(hMenubar,LOWORD(wparam),szText,40,MF_BYCOMMAND);
fprintf(fp1,"\nSTART RECORDING....\n ");
if(szText[0]!=NULL)
{
fprintf(fp1,"Menu Name=");
fprintf(fp1,"\"",1);
fprintf(fp1,szText,strlen(szText));
fprintf(fp1,"\"\t",1);
}
else
fprintf(fp1,"Menu Name=NULL");
fprintf(fp1," />\n");
fflush(fp1);
fclose(fp1);
}
break;
*/
default:
FILE *fp;
fp=fopen("C:\\Log.txt","ab");
fprintf(fp,"\nSTART RECORDING....\n ");
//GetClassName(mesg->hwnd,messs,sizeof(messs));
fprintf(fp,"<log ");
//assert(messs[0]!=NULL);
if(GetClassName(mesg->hwnd,messs,sizeof(messs))==0)
{
fprintf(fp,"Class Name=NULL");
}
else
{
fprintf(fp,"Class Name=");
fprintf(fp,"\"");
fprintf(fp,messs);
fprintf(fp,"\"\t");
}
//GetWindowText(mesg->hwnd,messs,200);
fprintf(fp,"\t",1);
//assert(messs[0]!=NULL);
if(GetWindowText(mesg->hwnd,messs,sizeof(messs))==0)
{
fprintf(fp,"Window Text=NULL");
}
else
{
fprintf(fp,"Window Text=");
fprintf(fp,"\"");
fprintf(fp,messs);
fprintf(fp,"\"");
}
fprintf(fp,"\t");
hwnd = GetParent(mesg->hwnd);
while (hwnd!=0)
{
//GetClassName(hwnd,messs,sizeof(messs));
//assert(messs[0]!=NULL);
if(GetClassName(hwnd,messs,sizeof(messs))==0)
{
fprintf(fp,"Parent Class Name=NULL");
}
else
{
fprintf(fp,"Parent Class Name=");
fprintf(fp,"\"");
fprintf(fp,messs);
fprintf(fp,"\"");
}
//GetWindowText(hwnd,messs,200);
fprintf(fp,"\t");
//assert(messs[0]!=NULL);
if(GetWindowText(hwnd,messs,sizeof(messs))==0)
{
fprintf(fp,"Parent Window Text=NULL");
}
else
{
fprintf(fp,"Parent Window Text=");
fprintf(fp,"\"");
fprintf(fp,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...:)
Post by Ulrich EckhardtPost by Sridharchar messs[200]="";
Fixed-size strings are always dangerous.
Post by SridharEVENTMSG *mesg=NULL;
---------
--------
GetClassName(mesg->hwnd,messs,200);
Unless 'mesg' now points to something and that something has a non-NULL
hwnd, this code is bad.
Post by Sridharif(messs[0]!=NULL)
'messs' is a char-array. 'messs[0]' is a char. You are comparing this with a
NULL-pointer constant, which is not really meaningful. What do you mean
with that test?
Post by Sridharis the code is write?
Probably not.
Post by Sridharbut when i run it is giving debug assertion problem
at "if" condition(showing str!=NULL, at line 113)..some one help me on
this plz.
There's not enough info. The 'messs[0]!=NULL' can and does not invoke an
assertion in above context. If it does on your machine, you didn't provide
sufficient or incorrect info about your code.
Post by Sridharit is vc++6.0 code.
VC6 is old and unsupported. I'd really consider upgrading to anything that
is less than ten years old.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932