Solved

CALLBACK to a Function

Posted on 2000-03-12
7
528 Views
Last Modified: 2010-04-02
What is the meaning of CALLBACK to a
function in C? Is it same as a function
pointer? How & when a CALLBACK is implemented (written)?  Why are they needed? Can you please explain with an example?
0
Comment
Question by:mansi
7 Comments
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2610065
Quick answer: yes, a callback is just a function pointer. Callbacks can have any number of parameters and return any sort of value. I don't know where you'd use them in normal C coding, but under Windows (for example) they're used quite a lot--if you wanted to get a list of all the fonts on the system you'd call the EnumFontFamiliesEx() function and pass it a callback which would get the names of the fonts. This is useful because you have no idea how many fonts are installed before you call the function and therefore could not easily allocate space for the information.
0
 
LVL 32

Accepted Solution

by:
jhance earned 400 total points
ID: 2610087
Strictly speaking, a callback function is any function that is available to be called directly by the operating system.

In a Windows application, for instance, the best example of a CALLBACK function is the main message processing loop:

LRESULT CALLBACK MainWndProc(
    HWND hwnd,        // handle to window
    UINT uMsg,        // message identifier
    WPARAM wParam,    // first message parameter
    LPARAM lParam)    // second message parameter
{
 
    switch (uMsg)
    {
        case WM_CREATE:
            // Initialize the window.
            return 0;
 
        case WM_PAINT:
            // Paint the window's client area.
            return 0;
 
        case WM_SIZE:
            // Set the size and position of the window.
            return 0;
 
        case WM_DESTROY:
            // Clean up window-specific data objects.
            return 0;
 
        //
        // Process other messages.
        //
 
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

Windows calls the function (in this case MainWndProc) whenever there are messages for this application to process.   Some other API functions also have callback features.  For example the asynchronous sockets functions can be use dwith callbacks so that a particular function will get called whenever the socket needs attention.  

Bottom line is that this is just a method of marking a function so that it will be available and callable from the OS whenever the appliction is running.
0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 2613440
Callback is also used to export a function from a dll. As in;
EXPORT char* CALLBACK dosomething( char* incoming_parameter)
{
// body of dosomething
return "Hello"
}
0
New! My Passport Wireless Pro Wi-Fi Mobile Storage

Portable wireless storage to offload, edit, and stream anywhere.

High-capacity, wireless mobile storage designed to accompany professional photographers and videographers in the field to easily offload, edit and stream captured photos and high-definition videos.

 

Author Comment

by:mansi
ID: 2616239
Dear jhance,

     some of my confusion is cleared.
but still some points are remaining about callback. I am editing the question. so Please bear with me.

                  With Thanks,
                      mansi.
0
 
LVL 32

Expert Comment

by:jhance
ID: 2616309
mansi,

Please don't edit the original question.  This makes it confusing for others who might read this question later.

Just add followup questions as comments here.

0
 

Author Comment

by:mansi
ID: 2660946
Dear jhance,
      first of all sicerely sorry for the delay in responding.
Now back to callback-
      In your first response, you had written-
Strictly speaking, a callback function is any function that is
available to be called directly by the operating system.
      Now I am told that callback is like a function pointer.Now please let me know which
of the following is correct-
      1) The function pointers used by operating systems are called
as CALLBACKs. The normal function pointer used in developing process
by a programmer are called just "normal function pointers." & not CALLBACKs.
          hence technically there is no diff. between a function pointer & a CALLBACK.

      2) CALLBACK means a function pointer only or it may be a function also
some times.
      3) Is CALLBACK a reserved word in C? Can a programmer use it when
developing an application? or only a system programmer has this previlege?

      I was given the following example for explaining CALLBACK-

file: callback.h

typedef float(*fptr) (float a,float b);
void register_me(fptr func);
void print_result(float a,float b);


file : callback.c

#include<stdio.h>
#include"callback.h"
static fptr current_func = 0;
void register_me(fptr func)
{
      current_func = func;
}
void print_result(float a , float b)
{
      float c;
      if (current_func)
      {
            c=(*current_func) (a,b);
            printf("The result is %f ",c);
      }
      else
      {
            printf("\n no function to call");
      }
}
file: myapp.c

#include"callback.h"
#include"myapp.h"

int main()
{
      register_me(&subtr);
      register_me(&add);      
      print_result(a,b);
      return 0;
}

file: myapp.h

float a=25.0,b=15.0;
float add(float,float);
float subtr(float,float);

float subtr(float a,float b)
      {
            return(a-b);
      }

float add(float a,float b)
      {
            return(a+b);
      }


Is it right? Please let me know.
                                    -Mansi.
0
 

Author Comment

by:mansi
ID: 2673332
Adjusted points from 350 to 400
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using unmanaged DLL from managed application 14 290
Trouble linking program with -lcrypt 3 142
List out all word 7 260
valid enum? 6 73
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

920 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

14 Experts available now in Live!

Get 1:1 Help Now