Win32 message events

I have a MFC based application where various dialogs are implemented as
extension DLLs. I've made a
special extension DLL which loads Python and exposes some of the
applications objects using Boost.
The Python code loaded is a wxPython based dialog (wxFrame). There is one
feature in the current
application I don't see how to 'integrate' into the python extension DLL.

The application is constructed so that if there is no (user) activity for a
defined time period, the
'kernel' closes down all open dialogs and presents a new login-window. So,
whenever an extension DLL
opens a dialog (MFC CDialog), the extension DLL notifies the 'kernel' that
the dialog is open. When
the dialog is closed (user terminates) the extension DLL notifies the
'kernel' again. If there is a
timeout condition, the 'kernel' will force the closing of the current open
dialog(s) by sending a
WM_COMMAND / IDCANCEL (win32) message to the dialogs.

My special extension DLL also opens a CDialog (not visible though) and in
the OnInitDialog, I create a
thread which executes the Python code. When the Python dialog is closed,
the thread-code sends a
WM_COMMAND / IDOK to the invisible dialog before ending.

Given that I in my OnCancel method in the invisible dialog would propagate
the message to the Python
code, how would I set up the Python code (wxFrame) so that I'm able to
receive (and process) such
a (win32) message - and also how would I send the message from the C++ code.

I was thinking of user a timer (in the Python code) to read a flag (which
would be set by the OnCancel
method in the invisible dialog) at regular intervals and upon correct
condition initiate the
termination, however I would prefer not to have a 'polled' system.

Thanks for any help.
Nikolai

Kirsebom Nikolai wrote:

I have a MFC based application where various dialogs are implemented
as extension DLLs. I've made a special extension DLL which loads
Python and exposes some of the applications objects using Boost. The
Python code loaded is a wxPython based dialog (wxFrame). There is
one feature in the current application I don't see how to 'integrate'
into the python extension DLL.

The application is constructed so that if there is no (user) activity
for a defined time period, the 'kernel' closes down all open dialogs
and presents a new login-window. So, whenever an extension DLL opens
a dialog (MFC CDialog), the extension DLL notifies the 'kernel' that the dialog is open. When the dialog is closed (user terminates) the
extension DLL notifies the 'kernel' again. If there is a timeout
condition, the 'kernel' will force the closing of the current open dialog(s) by sending a WM_COMMAND / IDCANCEL (win32) message to the
dialogs.

My special extension DLL also opens a CDialog (not visible though)
and in the OnInitDialog, I create a thread which executes the Python
code. When the Python dialog is closed, the thread-code sends a WM_COMMAND / IDOK to the invisible dialog before ending.

Given that I in my OnCancel method in the invisible dialog would
propagate the message to the Python code, how would I set up the
Python code (wxFrame) so that I'm able to receive (and process) such a (win32) message - and also how would I send the message from the
C++ code.

You can get the HWND of the wxWindow with GetHandle, and then use that value to send a message to the window from C++ using SendMessage or PostMessage. If the messages being sent are already being caught by wxWindows and turned into events then you can just use the standard EVT_* event binders to catch it. Try EVT_COMMAND.

Otherwise you can hook a Python function into the WndProc chain of the frame and watch for the message there. I know I've seen code for doing this but I havn't been able to find it, nothing in the win32all docs is looking like the right thing, and google hasn't turned anything up. Does anybody remember?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Otherwise you can hook a Python function into the WndProc chain of the frame and watch for the message there. I know I've seen code for doing this but I havn't been able to find it, nothing in the win32all docs is looking like the right thing, and google hasn't turned anything up. Does anybody remember?

Ah... After a quick note to Mark he reminds me how to do it. Here is a simple sample:

import wx
import win32gui
import win32con

oldWndProc = None

def MyWndProc(hWnd, msg, wParam, lParam):
     print (msg, wParam, lParam)
     return win32gui.CallWindowProc(oldWndProc,
                                    hWnd, msg, wParam, lParam)

def OnButton(evt):
     print "OnButton"

app = wx.PySimpleApp()
f = wx.Frame(None, -1, "Hook Test", size = (200, 150))
p = wx.Panel(f, -1)
b = wx.Button(p, -1, "button", (10,10))
wx.EVT_BUTTON(f, b.GetId(), OnButton)

oldWndProc = win32gui.SetWindowLong(f.GetHandle(),
                                     win32con.GWL_WNDPROC,
                                     MyWndProc)

f.Show()
app.MainLoop()

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!