win32 problem with WM_COPYDATA

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()
 
N

Neil Hodgson

Chris:
wc.lpszClassName = "QServer"
...
"QuoteServer App",
...
wnd = win32gui.FindWindow("QuoteServer", None)

Using the same name helps.
...
buff = struct.pack('11s', "XXXXX: YYYY")
print win32gui.SendMessage(wnd, win32con.WM_COPYDATA, 0, buff)

Copydata does not take an arbitrary pointer, it requires a correctly
initialized COPYDATASTRUCT. From a Mark Hammond post:

import struct, array
int_buffer = array.array("L", [0])
char_buffer = array.array("c", "the string data")
int_buffer_address = int_buffer.buffer_info()[0]
char_buffer_address, char_buffer_size = char_buffer.buffer_info
copy_struct = struct.pack("pLp", # dword *, dword, char *
int_buffer_address,
char_buffer_size, char_buffer_address)
# find target_hwnd somehow.
win32gui.SendMessage(w, WM_COPYDATA, target_hwnd, copy_struct)

Neil
 
C

Chris

Yea... I changed it to Qserver while posting, but forgot to do both
spots. But the array and struct thing worked, so its all going now.
Thanks a bunch. Any ideas about the Unregistering error?

Neil Hodgson said:
Chris:
wc.lpszClassName = "QServer"
...
"QuoteServer App",
...
wnd = win32gui.FindWindow("QuoteServer", None)

Using the same name helps.
...
buff = struct.pack('11s', "XXXXX: YYYY")
print win32gui.SendMessage(wnd, win32con.WM_COPYDATA, 0, buff)

Copydata does not take an arbitrary pointer, it requires a correctly
initialized COPYDATASTRUCT. From a Mark Hammond post:

import struct, array
int_buffer = array.array("L", [0])
char_buffer = array.array("c", "the string data")
int_buffer_address = int_buffer.buffer_info()[0]
char_buffer_address, char_buffer_size = char_buffer.buffer_info
copy_struct = struct.pack("pLp", # dword *, dword, char *
int_buffer_address,
char_buffer_size, char_buffer_address)
# find target_hwnd somehow.
win32gui.SendMessage(w, WM_COPYDATA, target_hwnd, copy_struct)

Neil
 
N

Neil Hodgson

Chris:
Thanks a bunch. Any ideas about the Unregistering error?

Put a trace in the WM_DESTROY handler. You were reentering by calling
DestroyWindow within the WM_DESTROY handler.

Neil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top