Solved

CallBack in Windows

Posted on 2001-06-13
10
396 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:GloriousRain
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Expert Comment

by:djbusychild
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
How do u split points?

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

0
 
LVL 30

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 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

10 Experts available now in Live!

Get 1:1 Help Now