C/C++ Callback function

Callback function are those functions which are called by framework or operating system or other external modules or any component which is not a part of the program. Programmer never call these functions directly. These are called asynchronously and parallely to the normal program flow. C signal handlers are good example of callback function. These are called by task scheduler of the operating system.


void sigint_handler(int signo)
  if (signo == SIGINT)
    printf("received SIGINT\n");

int main(void)
  if (signal(SIGINT, sigint_handler) == SIG_ERR)
  printf("\ncan't catch SIGINT\n");

  while(1) { 
  return 0;

COM/DCOM Callback function

COM client calls interface functions. These call goes to server and executes the function code. A return from the function move the execution context back to client code. This flow is good when we have couple of non-bloking functions or functions having short span of CPU time. There can be some function which deals with some longer and time taking tasks like downloading a file from internet, wait for message bytes in socket, copy a file etc. This type of tasks can not be fitted in normal interface design. Normal interface call will block the client until the actual task is ended.

COM/DCOM framework should support calling a client function from server side. Here the flow of the call is from server to client and this is the reverse direction compared to normal interface. These type of functions are known as events or notifications or callbacks. COM server often provides event interface with a couple of events. Client has to implement these events. COM server implements a non-blocking function to initiate a long task. This returns immediately creating a thread. This thread executes the long task and calls client events. Client gets notification of the status of the task through different evets.

COM/DCOM events example

C++ COM events are a bit complex. To understand callback mechanism here we have taken a VB example to demostrate a COM callback or event. We have taken WebBrowser COM object and one Text box with a button. Button click initiate Navigate() method to a URL which is a big winzip executable. WebBrowser server calls DownloadBegin() and then ProgressChange() events as the file downloads. We have implemented these events. We update text in the textbox as we receive events from COM server.

COM/DCOM callback in VB

Object = "{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}#1.1#0"; "ieframe.dll"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   3015
   ClientLeft      =   120
   ClientTop       =   465
   ClientWidth     =   4560
   LinkTopic       =   "Form1"
   ScaleHeight     =   3015
   ScaleWidth      =   4560
   StartUpPosition =   3  'Windows Default
   Begin SHDocVwCtl.WebBrowser WebBrowser1 
      Height          =   30
      Left            =   0
      TabIndex        =   0
      Top             =   10
      Visible         =   0   'False
      Width           =   10
      ExtentX         =   8070
      ExtentY         =   53
      ViewMode        =   0
      Offline         =   0
      Silent          =   0
      RegisterAsBrowser=   0
      RegisterAsDropTarget=   1
      AutoArrange     =   0   'False
      NoClientEdge    =   0   'False
      AlignLeft       =   0   'False
      NoWebView       =   0   'False
      HideFileNames   =   0   'False
      SingleClick     =   0   'False
      SingleSelection =   0   'False
      NoFolders       =   0   'False
      Transparent     =   0   'False
      ViewID          =   "{0057D0E0-3573-11CF-AE69-08002B2E1262}"
      Location        =   ""

Private Sub Command1_Click()
WebBrowser1.Navigate "http://download.winzip.com/gl/nkln/winzip21_downwz.exe?token=" & CInt(Time())
End Sub

Private Sub WebBrowser1_DownloadBegin()
Log.Text = "Download begins at " & Date & " " & Time() & vbCrLf
End Sub

Private Sub WebBrowser1_ProgressChange(ByVal Progress As Long, ByVal ProgressMax As Long)
Log.Text = Log.Text & "Download " & (Progress * 100) / ProgressMax & "%" & vbCrLf
End Sub

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.