EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of COM/DCOM Questions #Next question

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.


#include<stdio.h>
#include<signal.h>
#include<unistd.h>

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) { 
    sleep(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

VERSION 5.00
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        =   ""
   End
End

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

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

 Vote 0

Similar topics related to this section

# COM DCOM Interview Questions and Answers :: ALL Interview .com
# COM DCOM - COM DCOM Interview Questions
# COM interview questions | TechInterviews
# COM Interview Questions - CodeProject
# Com Dcom Interview Questions - Com and Dcom in Asp Net
# ATL Interview Questions and Answers :: ALL Interview .com
# ActiveX Interview Questions and Answers :: ALL Interview .com

* #1 webmaster Wed 30 Apr/2014 01:22:24(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.