Solved

CREATING CALLBACK FUNCTION

Posted on 2000-02-23
9
417 Views
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 :-)
0
Comment
Question by:Shay050799
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
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

0
 

Accepted Solution

by:
togmeister earned 150 total points
Comment Utility
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 ..
return;
}

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?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
question:
>> p.s: i know how to use the SetTimer in MFC,
>> so don't even bother :-)

answer:
>>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?
0
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:mikeblas
Comment Utility
> 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!")
#endif

BOOL CALLBACK MyProc(HWND, UINT, WPARAM, LPARAM)
{
   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
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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.
0
 

Expert Comment

by:togmeister
Comment Utility
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 ...
0
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
> 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.
 
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now