Posted on 2000-02-23
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

719 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