C
Chris
Hi. I'm trying to make a python program that can recieve WM_COPYDATA
messages. Not ideal, but the application I'll be receiving them from
will only send it that way. So I wrote a small program to try to
receive them, and a smaller one to send test messages. It't not
happening for some reason. I'll post both below. The program that
sends the test message finds the receiver ok, and SendMessage returns
0, but the receving program never calls its copydata handler. Any
thoughts? Discalimer: I haven't done windows in years, so am kind of
clueless about it now, but based on what I've seen around google, this
seems ok. Never actually saw an example of someone recieveing
messages though... only sending.
BTW - the try/except thing around UnregisterClass is catching an
invalid handle error for hinst. Not sure where that's coming from,
but it still seems to unregister the class... so I'm happy for now...
at least until I figure out my other issue. Any thougts on this error
are welcome too.
**code**
# Recieving prog
import sys
import win32gui
import win32api
import win32con
class gsWindow:
def __init__(self):
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
win32con.WM_COPYDATA: self.OnCopyData,
}
wc = win32gui.WNDCLASS()
wc.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
wc.hbrBackground = win32con.COLOR_WINDOW
wc.lpszClassName = "QServer"
wc.lpfnWndProc = message_map
self.hinst = wc.hInstance = win32api.GetModuleHandle(None)
self.cAtom = win32gui.RegisterClass(wc)
print 'hinst: %s' % self.hinst
self.hwnd = win32gui.CreateWindowEx(
win32con.WS_EX_APPWINDOW,
self.cAtom,
"QuoteServer App",
win32con.WS_OVERLAPPED | win32con.WS_SYSMENU,
win32con.CW_USEDEFAULT, 0,
win32con.CW_USEDEFAULT, 0,
0, 0, self.hinst, None)
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWDEFAULT)
win32gui.UpdateWindow(self.hwnd)
def OnCopyData(self, hwnd, msg, wparam, lparam):
print 'copydata(%s, %s, %s, %s)' % (hwnd, msg, wparam, lparam)
def OnDestroy(self, hwnd, msg, wparam, lparam):
win32gui.DestroyWindow(self.hwnd)
try: win32gui.UnregisterClass(self.cAtom, self.hinst)
except: pass
win32gui.PostQuitMessage(0)
def main():
w = gsWindow()
win32gui.PumpMessages()
main()
******
# Sending prog
import struct
import win32con
import win32gui
def main():
wnd = win32gui.FindWindow("QuoteServer", None)
print wnd
result = []
buff = struct.pack('11s', "XXXXX: YYYY")
print win32gui.SendMessage(wnd, win32con.WM_COPYDATA, 0, buff)
return
main()
messages. Not ideal, but the application I'll be receiving them from
will only send it that way. So I wrote a small program to try to
receive them, and a smaller one to send test messages. It't not
happening for some reason. I'll post both below. The program that
sends the test message finds the receiver ok, and SendMessage returns
0, but the receving program never calls its copydata handler. Any
thoughts? Discalimer: I haven't done windows in years, so am kind of
clueless about it now, but based on what I've seen around google, this
seems ok. Never actually saw an example of someone recieveing
messages though... only sending.
BTW - the try/except thing around UnregisterClass is catching an
invalid handle error for hinst. Not sure where that's coming from,
but it still seems to unregister the class... so I'm happy for now...
at least until I figure out my other issue. Any thougts on this error
are welcome too.
**code**
# Recieving prog
import sys
import win32gui
import win32api
import win32con
class gsWindow:
def __init__(self):
message_map = {
win32con.WM_DESTROY: self.OnDestroy,
win32con.WM_COPYDATA: self.OnCopyData,
}
wc = win32gui.WNDCLASS()
wc.hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
wc.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW)
wc.hbrBackground = win32con.COLOR_WINDOW
wc.lpszClassName = "QServer"
wc.lpfnWndProc = message_map
self.hinst = wc.hInstance = win32api.GetModuleHandle(None)
self.cAtom = win32gui.RegisterClass(wc)
print 'hinst: %s' % self.hinst
self.hwnd = win32gui.CreateWindowEx(
win32con.WS_EX_APPWINDOW,
self.cAtom,
"QuoteServer App",
win32con.WS_OVERLAPPED | win32con.WS_SYSMENU,
win32con.CW_USEDEFAULT, 0,
win32con.CW_USEDEFAULT, 0,
0, 0, self.hinst, None)
win32gui.ShowWindow(self.hwnd, win32con.SW_SHOWDEFAULT)
win32gui.UpdateWindow(self.hwnd)
def OnCopyData(self, hwnd, msg, wparam, lparam):
print 'copydata(%s, %s, %s, %s)' % (hwnd, msg, wparam, lparam)
def OnDestroy(self, hwnd, msg, wparam, lparam):
win32gui.DestroyWindow(self.hwnd)
try: win32gui.UnregisterClass(self.cAtom, self.hinst)
except: pass
win32gui.PostQuitMessage(0)
def main():
w = gsWindow()
win32gui.PumpMessages()
main()
******
# Sending prog
import struct
import win32con
import win32gui
def main():
wnd = win32gui.FindWindow("QuoteServer", None)
print wnd
result = []
buff = struct.pack('11s', "XXXXX: YYYY")
print win32gui.SendMessage(wnd, win32con.WM_COPYDATA, 0, buff)
return
main()