I am trying to understand python's capability to use Windows Messages with pywin32 library. For this I have created 2 scripts that are supposed to run parralel to each other. One is a client that is supposed to send messages the other is a server that is supposed to print all incoming messages. To use window messeages I have given them gui windows with tkinter library.
But when I run them paralel server doesn't recieve any messages.
Here is the client I use.
def main():
root = tk.Tk()
root.title("WM_client")
root.geometry("400x300")
root.update()
root.deiconify()
#python_hwnd = root.winfo_id()
python_hwnd = win32gui.FindWindow(None, "WM_client")
print(f"Server handle is {hex(python_hwnd)}, starting listening")
target = win32gui.FindWindow(None, "WM_server")
while True:
print(target, type(target))
print(WM_STATEREQUEST, type(WM_STATEREQUEST))
print(python_hwnd, type(python_hwnd))
print(f"message to be sent {hex(int(target))}, {WM_STATEREQUEST} , {hex(python_hwnd)}, 0")
response = win32gui.SendMessage(target, WM_STATEREQUEST, python_hwnd, 0)
print("server sent message response code", response)
print("last error given", win32api.GetLastError())
time.sleep(1)
if __name__ == "__main__":
main()
And here is the server I use.
def main():
root = tk.Tk()
root.title("WM_server")
root.geometry("400x300")
root.update()
root.deiconify()
python_hwnd = win32gui.FindWindow(None, "WM_server")
print(f"Server handle is {hex(python_hwnd)}, starting listening")
while True:
msg = win32gui.PeekMessage(python_hwnd, 0, 100, win32con.PM_REMOVE)
if msg[0] != 0:
print(msg)
window_handle, message_type, w_param, l_param, time_posted, (cursor_x, cursor_y) = msg[1]
if message_type != 96 and message_type != 0:
print("\nWindow Handle:", window_handle, hex(window_handle))
print("Message Type:", hex(message_type), message_type)
print("wParam:", w_param)
print("lParam:", l_param, "\n")
client_hwnd = win32gui.FindWindow(None, "WM_client")
msg = win32gui.PeekMessage(client_hwnd, 0, 100, win32con.PM_REMOVE)
if msg[0] != 0:
print(msg)
window_handle, message_type, w_param, l_param, time_posted, (cursor_x, cursor_y) = msg[1]
if message_type != 96 and message_type != 0:
print("\nWindow Handle:", window_handle, hex(window_handle))
print("Message Type:", hex(message_type), message_type)
print("wParam:", w_param)
print("lParam:", l_param, "\n")
What I have tried:
I have checked this using spy++ library and there are no messages falling through to the server side. So I deduced that there is something wrong on client side.
I have also tried to send messages to other applications before with send message and successfuly captured messages with spy++. Strange part is they are the same code.
<pre lang="Python">response = win32api.SendMessage(window_handle, WM_STATEREQUEST, python_hwnd, python_hwnd)