Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Howto: Class member function as callback function for dialog box

Reply
Thread Tools

Howto: Class member function as callback function for dialog box

 
 
prettysmurfed
Guest
Posts: n/a
 
      07-19-2003
Hi all

I have a bit of a problem, the subject of this post is almost selfexplaing.
But here goes: Heres an example of the code I want to implement, its
all nice and simple, but the flaw is I can't seem to get the adress of the
member function stated properly in the DialogBox function. I thought it
was enough to write it as Dialog:lgProc, if it was a normal situation
and DlgProc was a normal function (not member of anything) then you
would simply do it like this:

DialogBox(hInstance,TEXT("DLG"),hwnd,DlgProc);

What is wrong with my current approach. I've already been googling
the issue all day, and the only thing I could come up with was making
the member function static, but thats not fun, as I can't change anything
within the callback procedure.

////////////////////////////////////////////////////////////////////////////
//////////////////////////////
class Dialog
{
public :
Dialog();
void Init(HINSTANCE, HWND);
~Dialog();
private :
BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
};

void Dialog::Init(HINSTANCE hInstance, HWND hwnd)
{
DialogBox(hInstance,TEXT("DLG"),hwnd,Dialog:lgPr oc);
}

BOOL CALLBACK Dialog:lgProc(HWND h, UINT m, WPARAM w, LPARAM l)
{
...
}
////////////////////////////////////////////////////////////////////////////
//////////////////////////////

I'm currently using Dev-c++ 4.9.8.0, and the error message generated
by the compiler is

In member function `void OOWrapper::start(HINSTANCE__*,
cannot convert `BOOL (OOWrapper::*)(HWND__*, unsigned int,

I hope theres a way out this,
Go steady
Martin


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-19-2003

"prettysmurfed" <(E-Mail Removed)> wrote in message
news:3f19089c$0$22649$(E-Mail Removed) ...
> Hi all
>
> I have a bit of a problem, the subject of this post is almost

selfexplaing.
> But here goes: Heres an example of the code I want to implement, its
> all nice and simple, but the flaw is I can't seem to get the adress of the
> member function stated properly in the DialogBox function. I thought it
> was enough to write it as Dialog:lgProc, if it was a normal situation
> and DlgProc was a normal function (not member of anything) then you
> would simply do it like this:
>
> DialogBox(hInstance,TEXT("DLG"),hwnd,DlgProc);
>
> What is wrong with my current approach. I've already been googling
> the issue all day, and the only thing I could come up with was making
> the member function static, but thats not fun, as I can't change anything
> within the callback procedure.
>
>

////////////////////////////////////////////////////////////////////////////
> //////////////////////////////
> class Dialog
> {
> public :
> Dialog();
> void Init(HINSTANCE, HWND);
> ~Dialog();
> private :
> BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
> };
>
> void Dialog::Init(HINSTANCE hInstance, HWND hwnd)
> {
> DialogBox(hInstance,TEXT("DLG"),hwnd,Dialog:lgPr oc);
> }
>
> BOOL CALLBACK Dialog:lgProc(HWND h, UINT m, WPARAM w, LPARAM l)
> {
> ...
> }
>

////////////////////////////////////////////////////////////////////////////
> //////////////////////////////
>
> I'm currently using Dev-c++ 4.9.8.0, and the error message generated
> by the compiler is
>
> In member function `void OOWrapper::start(HINSTANCE__*,
> cannot convert `BOOL (OOWrapper::*)(HWND__*, unsigned int,
>
> I hope theres a way out this,
> Go steady
> Martin
>


Making the callback static is the only solution.

Instead of using DialogBox, you should use DialogBoxParam, this takes a
fifth parameter. Use that fifth parameter as a pointer to your OOWrapper
object. The static callback function will receive that pointer as part of
the WM_INITDIALOG message. You can store that pointer some where and use it
to call non-static member functions on your OOWrapper object.

This is the standard way of dealing with the 'how do I use a C++ member
function as a callback' issue.

Also see the FAQ

http://www.parashift.com/c++-faq-lit...o-members.html

john


 
Reply With Quote
 
 
 
 
prettysmurfed
Guest
Posts: n/a
 
      07-19-2003
OOPS, I'm sorry for sending the reply to your mailbox!!

Thanks for the reply John, though not a very helpful one

I was afraid this was the only solution, so I have
already taken the full step and start removing any parameter
altering code in my callback func. So be it <sigh>.
I really hope theres a good explanation for this because
I don't like having those functions messing around out of
scope, and I'm certainly not going to pick up MFC
programming. I just want it to be possible to do my
own OOD within the boundaries of normal windows
programming - and when I'm at it I would really like a million£,
a new car and peace throughout the world...

Thanks again, I hope you have a nice and sunny day
Martin


 
Reply With Quote
 
prettysmurfed
Guest
Posts: n/a
 
      07-19-2003
Thanks again for the help John

I've read through the
http://www.parashift.com/c++-faq-lit...o-members.html
and it really gave me the insight, in what the heck I was doing.
Now I have encapsulated the whole thing into a neat little package, with
only
two calls in my main function, thats beautiful! Instead of before, where the
object was scattered all over the place, with not so few globals!
I had a slight problem with the passed object-reference in DialogBoxParam,
but it turned out to be that I had forgot to make the variable static in the
callback
routine, resulting in alot of teeth-grinding, shame-on-you's and other stuff
I have
edited out.

go steady
Martin


 
Reply With Quote
 
prettysmurfed
Guest
Posts: n/a
 
      07-22-2003
"John Harrison" <(E-Mail Removed)> skrev i en meddelelse news:bfb3ra$c8a1o$(E-Mail Removed)-berlin.de...
:
: Making the callback static is the only solution.
:
: Instead of using DialogBox, you should use DialogBoxParam, this takes a
: fifth parameter. Use that fifth parameter as a pointer to your OOWrapper
: object. The static callback function will receive that pointer as part of
: the WM_INITDIALOG message. You can store that pointer some where and use it
: to call non-static member functions on your OOWrapper object.

Yes, but you can only make one instance of the object or am I wrong? The second time you
call it, the pointer will be overwritten with a new pointer to the new
OOWrapper object. So even though I can create many dialogues with the OOWrapper
object, theres only one callback function, which is common to all of them...?
Or is there some smart way of differentiating among the various dialogues when
the callback function is called?

: This is the standard way of dealing with the 'how do I use a C++ member
: function as a callback' issue.
:
: Also see the FAQ
:
: http://www.parashift.com/c++-faq-lit...o-members.html
:
: john
:
:


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-22-2003
On Tue, 22 Jul 2003 19:42:28 +0200, "prettysmurfed" <(E-Mail Removed)> wrote:

>is there some smart way of differentiating among the various dialogues when
>the callback function is called?


Using standard C++ without any platform-specific code you can do that via
e.g. a std::map in a namespace scope variable.

Using platform-dependent functionality you can (1) store a pointer to the
C++ object in the API-level object, (2) associate a pointer to the C++ object
with the API-level object, e.g. via Windows property lists, or (3) replace
the API-level object's message processing function with a pointer to a
dynamically created thunk that calls a C++ object method.

 
Reply With Quote
 
prettysmurfed
Guest
Posts: n/a
 
      07-22-2003
What! I'm sorry you must've mistaken me for someone who's a bit more
intelligent. That description went straight over/through my head.
I've already solved the problem, actually I was going to post that no bother
answering my previous question, as the solution was right in front of me!
The solution I came up with is probably a bit off-topic, more a windows
issue, but solutions never seem to care what topic they are defined within.
The different dialogues has different hwnd's so I just differentiate among
them with the calling hwnd.

But Alf I'm really happy that you responded anyway.
- Martin

"Alf P. Steinbach" <(E-Mail Removed)> skrev i en meddelelse news:(E-Mail Removed)...
: On Tue, 22 Jul 2003 19:42:28 +0200, "prettysmurfed" <(E-Mail Removed)> wrote:
:
: >is there some smart way of differentiating among the various dialogues when
: >the callback function is called?
:
: Using standard C++ without any platform-specific code you can do that via
: e.g. a std::map in a namespace scope variable.
:
: Using platform-dependent functionality you can (1) store a pointer to the
: C++ object in the API-level object, (2) associate a pointer to the C++ object
: with the API-level object, e.g. via Windows property lists, or (3) replace
: the API-level object's message processing function with a pointer to a
: dynamically created thunk that calls a C++ object method.
:


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing a callback function as a class member Nathan Huesken Python 0 07-07-2010 09:48 PM
can a class member function be used as a callback function? JDT C++ 6 03-29-2007 12:45 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Pointer-to-member-function pointing to a member function of an inherited class akiriwas@gmail.com C++ 12 02-11-2005 05:15 PM



Advertisments