Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CREATING CALLBACK FUNCTION

Posted on 2000-02-23
9
Medium Priority
?
434 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
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

0
 

Accepted Solution

by:
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 ..
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
ID: 2554038
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
Technology Partners: 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!

 
LVL 9

Expert Comment

by:jasonclarke
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.
0
 
LVL 11

Expert Comment

by:mikeblas
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!")
#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
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.
0
 

Expert Comment

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

Expert Comment

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

Expert Comment

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

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

885 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