Callbacks
-
In a function, I create a font chooer dialog for my user, for which I'm trying to add a callback function, like so: //create the pointer LPCFHOOKPROC FontHook; //create the font chooser dialog CFontDialog dlg(&LogFont,CF_ENABLEHOOK|CF_EFFECTS|CF_BOTH|CF_NOVECTORFONTS|CF_NOSCRIPTSEL|CF_INITTOLOGFONTSTRUCT,NULL,NULL); //tell it to hook to the function dlg.m_cf.lpfnHook = FontHook; //display the font dialog if(dlg.DoModal() == IDOK) { //blah blah } Then, elsewhere in the view I added the actual callback function, like so: UINT CALLBACK FontHook(HWND hdlg,UINT uiMsg,WPARAM wParam,LPARAM lParam) { //blah blah return TRUE; } It compiles fine, but when I try to use the font dialog is gives me an application error: "The instruction at '0xcccccccc' referenced memory at '0xcccccccc'. The memory could not be 'read'." It desplays this three times, before quitting out. I figured out it's on the .DoModal line where it's having the issue, I think. It gives me this error when debugging: "Unhandled exception in Application.exe: 0xC0000005: Access violation." Can anyone please tell me what I'm doing wrong or forgetting to do? halblonious
-
In a function, I create a font chooer dialog for my user, for which I'm trying to add a callback function, like so: //create the pointer LPCFHOOKPROC FontHook; //create the font chooser dialog CFontDialog dlg(&LogFont,CF_ENABLEHOOK|CF_EFFECTS|CF_BOTH|CF_NOVECTORFONTS|CF_NOSCRIPTSEL|CF_INITTOLOGFONTSTRUCT,NULL,NULL); //tell it to hook to the function dlg.m_cf.lpfnHook = FontHook; //display the font dialog if(dlg.DoModal() == IDOK) { //blah blah } Then, elsewhere in the view I added the actual callback function, like so: UINT CALLBACK FontHook(HWND hdlg,UINT uiMsg,WPARAM wParam,LPARAM lParam) { //blah blah return TRUE; } It compiles fine, but when I try to use the font dialog is gives me an application error: "The instruction at '0xcccccccc' referenced memory at '0xcccccccc'. The memory could not be 'read'." It desplays this three times, before quitting out. I figured out it's on the .DoModal line where it's having the issue, I think. It gives me this error when debugging: "Unhandled exception in Application.exe: 0xC0000005: Access violation." Can anyone please tell me what I'm doing wrong or forgetting to do? halblonious
The problem is to do with your
FontHook
pointer. To get this working you need to remove the following from your code://create the pointer LPCFHOOKPROC FontHook;
Then ensure that you have a prototype for the callback fn somewhere before where you create your dialog. Prototype will look something like:UINT CALLBACK FontHook( HWND, UINT, WPARAM, LPARAM );
What you are doing wrong is creating an uninitialised local pointer in your dialog creation fn and you specify this pointer as the address of the callback fn. In debug mode, items such as un-initialised pointers always get set to 0xCCCCCCCC so that it's easy to recognise them as such. Phil -
In a function, I create a font chooer dialog for my user, for which I'm trying to add a callback function, like so: //create the pointer LPCFHOOKPROC FontHook; //create the font chooser dialog CFontDialog dlg(&LogFont,CF_ENABLEHOOK|CF_EFFECTS|CF_BOTH|CF_NOVECTORFONTS|CF_NOSCRIPTSEL|CF_INITTOLOGFONTSTRUCT,NULL,NULL); //tell it to hook to the function dlg.m_cf.lpfnHook = FontHook; //display the font dialog if(dlg.DoModal() == IDOK) { //blah blah } Then, elsewhere in the view I added the actual callback function, like so: UINT CALLBACK FontHook(HWND hdlg,UINT uiMsg,WPARAM wParam,LPARAM lParam) { //blah blah return TRUE; } It compiles fine, but when I try to use the font dialog is gives me an application error: "The instruction at '0xcccccccc' referenced memory at '0xcccccccc'. The memory could not be 'read'." It desplays this three times, before quitting out. I figured out it's on the .DoModal line where it's having the issue, I think. It gives me this error when debugging: "Unhandled exception in Application.exe: 0xC0000005: Access violation." Can anyone please tell me what I'm doing wrong or forgetting to do? halblonious
Try this instead of ur function write UINT_PTR CALLBACK CFHookProc( HWND hdlg, // handle to dialog box UINT uiMsg, // message identifier WPARAM wParam, // message parameter LPARAM lParam // message parameter ); and then try out if not write back Thanx TAKE CARE
-
In a function, I create a font chooer dialog for my user, for which I'm trying to add a callback function, like so: //create the pointer LPCFHOOKPROC FontHook; //create the font chooser dialog CFontDialog dlg(&LogFont,CF_ENABLEHOOK|CF_EFFECTS|CF_BOTH|CF_NOVECTORFONTS|CF_NOSCRIPTSEL|CF_INITTOLOGFONTSTRUCT,NULL,NULL); //tell it to hook to the function dlg.m_cf.lpfnHook = FontHook; //display the font dialog if(dlg.DoModal() == IDOK) { //blah blah } Then, elsewhere in the view I added the actual callback function, like so: UINT CALLBACK FontHook(HWND hdlg,UINT uiMsg,WPARAM wParam,LPARAM lParam) { //blah blah return TRUE; } It compiles fine, but when I try to use the font dialog is gives me an application error: "The instruction at '0xcccccccc' referenced memory at '0xcccccccc'. The memory could not be 'read'." It desplays this three times, before quitting out. I figured out it's on the .DoModal line where it's having the issue, I think. It gives me this error when debugging: "Unhandled exception in Application.exe: 0xC0000005: Access violation." Can anyone please tell me what I'm doing wrong or forgetting to do? halblonious
In addition to what Phil has already pointed out, by using
CF_BOTH
, I believe that thehDC
member of yourLOGFONT
structure must also be initialized. Are the other members properly initialized?
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
The problem is to do with your
FontHook
pointer. To get this working you need to remove the following from your code://create the pointer LPCFHOOKPROC FontHook;
Then ensure that you have a prototype for the callback fn somewhere before where you create your dialog. Prototype will look something like:UINT CALLBACK FontHook( HWND, UINT, WPARAM, LPARAM );
What you are doing wrong is creating an uninitialised local pointer in your dialog creation fn and you specify this pointer as the address of the callback fn. In debug mode, items such as un-initialised pointers always get set to 0xCCCCCCCC so that it's easy to recognise them as such. PhilThank you very much, that seemed to work. Now, I'm off to figure out how to make do what I want... halblonious
-
In addition to what Phil has already pointed out, by using
CF_BOTH
, I believe that thehDC
member of yourLOGFONT
structure must also be initialized. Are the other members properly initialized?
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
They must be, because it seems to work now that I changed that line. Thank you. halblonious
-
Try this instead of ur function write UINT_PTR CALLBACK CFHookProc( HWND hdlg, // handle to dialog box UINT uiMsg, // message identifier WPARAM wParam, // message parameter LPARAM lParam // message parameter ); and then try out if not write back Thanx TAKE CARE
I've got it working now. Thank you for your comment. halblonious