tags:

views:

101

answers:

4

I want to do some processing when a particular dialog is opened but I am not able to find any way to get notification when that dialog is opened.

Is there any way to get notification in application for opening of a particular windows dialog?

The only available information about the dialog is its title and its unique.

+2  A: 

The general solution is to use windows hooks, filter to WH_CBT, filter to WM_CREATE, or something like that, get the window text and see if it is the one of your interest.

For additional info see: http://msdn.microsoft.com/en-us/library/ms997537.aspx

One more important point: in hook you should use SetWindowLongPtr() to set window process to your own function, that will actually receive WM_CREATE event. In all calls this function should call the original window procedure.

ironic
-1, WH_CALLWINDOWPROC is a bad solution for this problem. You would have to basically listen to EVERY window message system wide since your only choices are local hooks (which won't work here) or global ones. You might as well just create a thread and SPIN on FindWindowit would be cheaper than listening to every send message system wide.
John Knoeller
yes. i'm sorry, it is definitely not WH_CALLWNDPROC. WH_CBT is what I wanted to say.
ironic
A: 

EDIT: sorry didn't notice that you don't have the code yourself but only the title. So I think the other posts solution is what you need

The event handling in win32 applications is done via a so called windows procedure which is a callback function with the following signature:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

This callback gets called by windows every time there is a message for windows which are registered with this callback function. One of the first messages send to a new window is the WM_CREATE message.

If you are creating your windows "by hand" with win32 API, then there should be a static callback function like the one below where you can filter for the WM_CREATE message.

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
  switch( message )
  {
    case WM_CREATE:
      // do what ever you want 
      return 0;

    case default:
      return DefWndProc( hwnd, message, wParam, lParam );

  }
}

If you use MFC dialogs (CDialog) then you can overwrite the function CDialog::OnInitDialog().

Holger Kretzschmar
A: 

OK, the way to do this is to use SetWindowsHookEx(WH_SYSMSGFILTER,...) You'll be getting a lot more callbacks than you really need. and global hooks are a real drain on system performance (they can force the system to serialize things that would normally run independently)

be sure to read the Remarks, especially this part:

SetWindowsHookEx can be used to inject a DLL into another process. A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. If an application requires the use of hooks in other processes, it is required that a 32-bit application call SetWindowsHookEx to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call SetWindowsHookEx to inject a 64-bit DLL into 64-bit processes. The 32-bit and 64-bit DLLs must have different names.

Your hook must live in a dll, and the dll will end up loaded into other process's address space, so you won't it won't have access to your procees's address space, you will have to set up some sort of interprocess communication between your hook and your app.

On the whole I'd say this sounds like a really bad idea.

John Knoeller
+1  A: 

You can also use a CBT Hook to watch window creation messages. You'll have access to the CREATSTRUCT used to create the actual window, eg, the title and class name. You can prevent the window from being created in your hook, modify the size, etc.

Jeff Paquette
this only works if the window title is set on window creation. Which is pretty unlikely.
John Knoeller
Jeff Paquette