Howto: Class member function as callback function for dialog box

P

prettysmurfed

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::DlgProc, 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::DlgProc);
}

BOOL CALLBACK Dialog::DlgProc(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
 
J

John Harrison

prettysmurfed said:
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::DlgProc, 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::DlgProc);
}

BOOL CALLBACK Dialog::DlgProc(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-lite/pointers-to-members.html

john
 
P

prettysmurfed

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
 
P

prettysmurfed

Thanks again for the help John

I've read through the
http://www.parashift.com/c++-faq-lite/pointers-to-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
 
P

prettysmurfed

"John Harrison" <[email protected]> skrev i en meddelelse :
: 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-lite/pointers-to-members.html
:
: john
:
:
 
A

Alf P. Steinbach

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.
 
P

prettysmurfed

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" <[email protected]> skrev i en meddelelse :
: >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.
:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,006
Latest member
LauraSkx64

Latest Threads

Top