By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,221 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,221 IT Pros & Developers. It's quick & easy.

Howto: Class member function as callback function for dialog box

P: n/a
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::DlgPr oc);
}

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
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"prettysmurfed" <pr***********@yahoo.dk> wrote in message
news:3f***********************@nntp04.dk.telia.net ...
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::DlgPr oc);
}

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-lit...o-members.html

john
Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

P: n/a
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
Jul 19 '05 #4

P: n/a
"John Harrison" <jo*************@hotmail.com> skrev i en meddelelse news:bf************@ID-196037.news.uni-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
:
:
Jul 19 '05 #5

P: n/a
On Tue, 22 Jul 2003 19:42:28 +0200, "prettysmurfed" <pr***********@yahoo.dk> 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.

Jul 19 '05 #6

P: n/a
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" <al***@start.no> skrev i en meddelelse news:3f****************@News.CIS.DFN.DE...
: On Tue, 22 Jul 2003 19:42:28 +0200, "prettysmurfed" <pr***********@yahoo.dk> 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.
:
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.