Posted on 2000-02-23
Last Modified: 2010-04-10
Hi All !
can someone explain to me how to create a simple CallBack function using just C++ no MFC !!!
simple class that has a callback function
and how the client can use it

thank you very much

p.s: i know how to use the SetTimer in MFC, so don't even bother :-)
Question by:Shay050799
  • 3
  • 2
  • 2
  • +1
LVL 11

Expert Comment

ID: 2552786
A callback function is just a function with a well-known prototype that is passed to some other code with a pointer.

What do you want the callback to do,
specifically? What do you mean by "client" in this case?

Are you asking how to use SetTimer() to hit a callback function without using MFC?

..B ekiM


Accepted Solution

togmeister earned 150 total points
ID: 2553333
I guess you want a callback function for SetTimer in straight C/C++. Just define :

void CALLBACK MyTimerFunction(HWND hWnd, UINT iTimerMessage, UNIT iTimer, DWROD dwTime)
... check the correct iTimer is called back if you have more than one ...
... do something ..

and pass MyTimerFunction as the CALLBACK parameter to SetTimer.

Same applies to any callback function, but a) the parameters may be different and b) you may have to cast the callback to get it accepted, if the declaration is forsome reason different to the placeholder definition. Usually it's just a pointer to function (LONG *).

What's the problem?
LVL 22

Expert Comment

ID: 2554038
>> p.s: i know how to use the SetTimer in MFC,
>> so don't even bother :-)

>>I guess you want a callback function for
>>SetTimer in straight C/C++.

I think you bothered.

>> b) you may have to
>> cast the callback to get it accepted,
You shouldn't have to do that--at least I can't think of any case where that is necessary.  I would be suspicious of any case in which you have to do a cast like that.

>> What's the problem?
If you don't know, how could you answer it?
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


Expert Comment

ID: 2554056
You really need to specify what you want in more detail.  Do you really mean a windows callback of some kind, or a generic callback, where you are writing both the callback and the caller.

Its quite easy to right decent callback type code, with no nasty static mess if you control both ends of whats going on.
I can write down a simple example of this if you want, but let us know what you need.
LVL 11

Expert Comment

ID: 2555119
> at least I can't think of any case where that is necessary.  

Uh, well: try all over Windows! Without STRICT defined (that is to say, with NO_STRICT defined), most every callback in Windows is defined as FARPROC. FARPROC is pretty generic--you need to cast back to FARPROC to build without warnings.

Try this one on for size:

--- begin file fooey.cpp ---

// compile me with
//   cl /W4 /c fooey.cpp

#define NO_STRICT
#include <windows.h>

#ifdef STRICT
#pragma message("STRICT defined!")

   return 0;

void foo()
   DialogBox(NULL, "FOO", NULL, MyProc);

--- end file fooey.cpp ---

You'll get a nice big 'cannot convert' error on the dialog box proc callback parameter.  To fix the code, you have to cast:

   DialogBox(NULL, "FOO", NULL, (FARPROC) MyProc);

to make it work.

..B ekiM
LVL 22

Expert Comment

ID: 2555572
I guess I'm used to strict.  Weird that in without strict, you have to cast but with strict you don't.  With handles that is usually the reverse.

Expert Comment

ID: 2557367
What can I say? Try to be helpful, and all you do is get abuse. Never mind, got the answer to my question from some real pros at Microsoft in the UK. I take back all I said about Bill.

Won't be coming here again, I guess someone has had a sense of humour failure. Guess there's a language barrier across the miles :)

For anyone who's interested, its usually easy to answer a question, if you can find what the question is ...

Expert Comment

ID: 2557386
> got the answer to my question


What question.  You didn't ask this question?

> answer a question, if you can find what the question is

thats the whole point, we never did find out what the question was, and still don't know despite asking...hence why your 'answer' which appeared to completely ignore the details of the question got some criticism.
LVL 22

Expert Comment

ID: 2557895
togmeister, the question is very unclear, that is not your fault.  But one thing was clear was that he didn't want a callback for a timer.  That is what you provided.  So we don't know what he wants except know it isn't wnat you provided.  Then you even asked him what he was asking in the question, this indicates that you, like the rest of us, don't know what the question is.  If you don't know the question, you can't answer.  

If you are interested in participating in the site--which is very worthwhile--I recommend you just "watch" for a day or two to see how the site works.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question