Discussion:
debug assertion problem
(too old to reply)
Sridhar
2009-08-17 09:17:01 UTC
Permalink
char messs[200]="";
EVENTMSG *mesg=NULL;


---------
--------
GetClassName(mesg->hwnd,messs,200);
if(messs[0]!=NULL)
{

}


is the code is write? but 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.
it is vc++6.0 code.
--
--------------------------------------------
At last i did something for today...:)
Ulrich Eckhardt
2009-08-17 09:35:21 UTC
Permalink
Post by Sridhar
char messs[200]="";
Fixed-size strings are always dangerous.
Post by Sridhar
EVENTMSG *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 Sridhar
if(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 Sridhar
is the code is write?
Probably not.
Post by Sridhar
but 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 Sridhar
it 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
Sridhar
2009-08-20 06:01:01 UTC
Permalink
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 Eckhardt
Post by Sridhar
char messs[200]="";
Fixed-size strings are always dangerous.
Post by Sridhar
EVENTMSG *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 Sridhar
if(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 Sridhar
is the code is write?
Probably not.
Post by Sridhar
but 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 Sridhar
it 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
Ulrich Eckhardt
2009-08-20 08:57:36 UTC
Permalink
Post by Sridhar
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.
No, I'm not going to read 300+ lines of code unless you pay me for it. If
you want any help, be prepared to do your own homework first, i.e. reduce
the problematic code to the bare minimum.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Tim Roberts
2009-08-22 19:49:38 UTC
Permalink
Post by Sridhar
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.
Are you really unable to figure out how to debug this? I find that
astonishing. Wouldn't the first question you asked yourself be "I wonder
which messages trigger this problem, and which windows they are going to?"

Had you done so, by just adding a few more pieces of information to your
log, you'd discover that many of the events you intercept are targeted at
the desktop window (handle 0), and the desktop window does not have a
window class.

By the way, what led you to write code like this:
fprintf(fp,"Class Name=");
fprintf(fp,"\"");
fprintf(fp,messs);
fprintf(fp,"\"\t");

Instead of the more efficient and just as easy to read:
fprintf( fp, "Class Name=\"%s\"\t", messs );
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Ismo Salonen
2009-08-24 12:50:24 UTC
Permalink
Post by Tim Roberts
Post by Sridhar
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.
Are you really unable to figure out how to debug this? I find that
astonishing. Wouldn't the first question you asked yourself be "I wonder
which messages trigger this problem, and which windows they are going to?"
Had you done so, by just adding a few more pieces of information to your
log, you'd discover that many of the events you intercept are targeted at
the desktop window (handle 0), and the desktop window does not have a
window class.
fprintf(fp,"Class Name=");
fprintf(fp,"\"");
fprintf(fp,messs);
fprintf(fp,"\"\t");
fprintf( fp, "Class Name=\"%s\"\t", messs );
Actually the latter is only acceptable solution.
If one uses "fprintf(fp,messs);" it may crash the application if the
messs contains %-characters followed valid formatting sequence.
One could use fputs() but fprintf is better style when more than one
string is printed.

ismo

David Lowndes
2009-08-17 09:49:17 UTC
Permalink
Post by Sridhar
char messs[200]="";
EVENTMSG *mesg=NULL;
---------
Presumably something here sets mesg to something valid?
Post by Sridhar
--------
GetClassName(mesg->hwnd,messs,200);
if(messs[0]!=NULL)
{
is the code is write?
There's nothing essentially wrong (for a non-Unicode version).
Post by Sridhar
but 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.
How can you get an assertion when there's no assert statement?

Dave
Continue reading on narkive:
Loading...