Solved

CallBack in Windows

Posted on 2001-06-13
10
404 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
[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
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
Technology Partners: 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 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

Technology Partners: 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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

728 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