Solved

CallBack in Windows

Posted on 2001-06-13
10
400 Views
Last Modified: 2010-04-02
Can anybody explain how call backs work in Windows Programming? Since I am new, a good example would be very helpful.
What I am trying to do is I have total three processes running:
Application one has a thread running continuously and getting events from a second application.
When the thread receives certain events I want my third application to know about those events.

I think I can setup callback functions, so the thread will notify the third application using callback.
0
Comment
Question by:Nutty104
10 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6188576
VC++ is a compiler from Microsoft.  CALLBACK is a function type modifier for specifying a function that can be "called back" by another function in Windows.

The two are unrelated concepts.

What is it you are trying to do?  Ask a better question and you'll get a better answer....
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6188698
callbacks are function that get called when something happens.

I think you mean "windows programming" when you say "Visual C++"....

for example, when you register your window you have to fill in a WNDCLASS structure

http://msdn.microsoft.com/library/psdk/winui/winclass_8yk2.htm

one of the field in the structure is lpfnWndProc.
you set this to point a function and whenever your window gets a message that function gets called... that's the basic idea.
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6188703
play around with the wizard to create a win32 hello world application from Visual C++. You'll find functions that
fill in the WNDCLASS structure and also the function that gets called when your window receives a message.
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 2

Expert Comment

by:GloriousRain
ID: 6189293
A callback function is essentially an event handler that is implemented by an application and called by the system. Microsoft? Windows? applications typically implement multiple callback functions, each one designed for a particular set of events. When an event occurs, the system notifies the application by calling the appropriate callback function. The callback function also usually has a parameter list that the system can use to pass the application more detailed information about the event. The most common example of a callback function is the window procedure. This function is used by the system to pass Windows messages to the applications that owns the window.
Example:
typedef void (__cdecl *lpCallback)();

void _cdecl CallBack();
void Function(lpCallback);
void Test_Func();

void _cdecl CallBack()
{
}

void Function(lpCallback)
{
}

void Test_Func()
{
     Function(CallBack);
}
0
 
LVL 22

Expert Comment

by:ambience
ID: 6189368
Application one has a thread running continuously and getting events from a second application.

>>> How is that application getting events ? are you using connection points for this ?

When the thread receives certain events I want my third application to know about those events.

>>> Sure you can do this

I think I can setup callback functions, so the thread will notify the third application using callback.

>>> Callbacks require you to pass address of a function to the server which the srver saves and the srever later calls the function thru that pointer when required and this way notifies the client of something .. this is the jist of call backs.. note that in Win32 a pointer to some function in one process is meaningless to the other one and atleast is invalid (due to private address space) so you cannot directly call a function in some other process from another process.

Using callbacks maynot be the best approach in your case , if you can get away by using windows messages that is a good way out.

Incase you are alerady using connection points for communication between App1 and App2 , why not do the same for App2 and App3 (ofcouse if you are willing to make App2 a COM server :)) .


0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6189392
whoa... how did we move onto COM all of a sudden? ^^

there's really no reason to make matters complicated just to do some simple IPC.

Nutty, look into the following sections of the MSDN.

http://msdn.microsoft.com/library/psdk/winbase/ipc_57qr.htm
0
 
LVL 1

Accepted Solution

by:
ua1zcl earned 80 total points
ID: 6220423

Nutty, for simplicity you can make 3 applications
Hello World. Assign wcl.lpszClassName with different
names. Then use FindWindow to get window handle
 and PostMessage function to post a message.

in message loop of window proc:

// add to app2 "App2class")
case WM_LBUTTONDOWN:
  {HWND hw=FindWindow("App1class",0);
    if(hw)PostMessage(hw,WM_COMMAND,IDYES,0);//to app1
  }
  break;
 
// add to app1 ("App1class")
  case WM_COMMAND:
  switch (LOWORD(wParam))
  {
    case IDYES:
    {
     HWND hw=FindWindow("App3class",0);
     if(hw)PostMessage(hw,WM_COMMAND,IDYES,0);//to app
    }
    break;

// add to app3 ( "App3class")
  case WM_COMMAND:
  switch (LOWORD(wParam))
  {
    case IDYES:
     MessageBox(0,"Message received ","App3",MB_OK);
     
    break;

There in Windows is many other possibilities to
communicate , this is very simple example.



0
 

Author Comment

by:Nutty104
ID: 6222058
Hi All,

Thanx for all your help and comments. ua1zcl and ambience have provided quite a good explanation of my question. So I would like to split the points btwn them.

Thanx all.......
0
 

Author Comment

by:Nutty104
ID: 6222065
How do u split points?

It seems like all the points are given to ua1zcl.....

0
 
LVL 30

Expert Comment

by:Axter
ID: 6222080
How do u split points?
Reequest that the points be split in the Community Support topic area.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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 difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

821 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