473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Howto: Class member function as callback function for dialog box

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
6 10841

"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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: MacFly | last post by:
Hi everyone, HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer) I want that method to be class member method so it could have access to class...
6
by: Christian Buckl | last post by:
Hi, I try to implement my own thread class based on POSIX threads. I want my class to manage everything (creation of threads, exception handling...). This includes also some functions that need to...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
2
by: Kimmo Laine | last post by:
I tried to use a class member function as a callback in preg_replace_callback, but failed. It announces: " Warning: preg_replace_callback() : requires argument 2, 'myclass::myfunction', to be a...
3
by: tom | last post by:
How to get access to veriable from static class member? class cTest { public: int a; void Set_a(){a = 1;} static int dlgTest()
3
by: drummond.ian | last post by:
Hello Everyone, This problem's been causing me a lot of trouble and I'm hoping somebody can help me out!! I have a dialog-based MFC application in visual studio 2003. I want to call a...
2
by: tendots | last post by:
Hi all, I have come up against a problem when I am registering a callback within a constructor. The code that I am writing contains a class that deals with everything to do with bringing up a...
6
by: JDT | last post by:
Hi, Can we pass a member function in a class as a callback function? Someone instucted me that I can only use a static functon or a global function as a callback. Your help is appreciated. JD
3
by: Vulcan | last post by:
<code> class Base { }; class Class1 : public Base{ public: void Function1(); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.