[python-win32] win32api & win32gui help

Tim Roberts timr at probo.com
Mon Mar 3 20:37:43 CET 2014


Joe Bennett wrote:
> Looking for a good simple remedial course on getting my python script
> to talk to a Windows program. The author of the windows program has
> documented the api here:
>
> http://www.logger32.net/help/Logger32/Using%20the%20Logger32%20External%20Interface.htm

To use this API, you have to BE a Windows application, with at least one
window and a message loop.  You cannot call this from a console application.


> I get a response for 'hwnd' but have not been able to get any
> confirmation that RegisterMessage, PostMessage and GetMessage are
> working, configured properly or...what...
>
> Running this all in WIN7 with ActivePython 2.6.6...
>
> import win32api
> import win32gui
>
> hwnd = win32gui.FindWindowEx(0, 0, 0, "Logger32")
> print "hwnd= ", hwnd
>
> message = win32api.RegisterWindowMessage("Logger32 3")
> win32api.PostMessage(hwnd,message,1,99999)

That much is probably working just fine.  You're registering a window
message and getting a handle.  But, as Roger pointed out, you need to
tell it YOUR window handle to the logging application, so it can send
information to you.  You're passing a made-up number, which won't work.

> test = win32gui.GetMessage(hwnd,0,0)
> print "GetMessage: ", test

This is not right.  GetMessage is used to get messages that have been
send to your OWN windows.  In this case, you're giving it the handle of
a window that belongs to another process.

You can do this with a hidden window -- it doesn't have to be visible. 
Basically, you call RegisterClass and CreateWindow to create the window,
then run a message loop with PumpWaitingMessages.  In the PyWin32
distribution, go look for win32ui_devicenotify.py.  That is a sample
application that creates a hidden window in order to catch PnP
notifications.  You can yank out the most of the code and use the shell.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list