?
Solved

CREATING CALLBACK FUNCTION

Posted on 2000-02-23
9
Medium Priority
?
432 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
[X]
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
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
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!

 
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

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