i'm trying to do (what should be) some minor work with win32. I basically just need to send a message and wait for a response.
- I need the HWND or window handle of my python script/program to actually send the message.
- I need to receive a WM_COPYDATA message from the end program.
I'm able to find the HWND of my python program with a script provided by ColonelFai1ure#0706 on the python discord server:
#gets the HWND of the active window
def getActiveWinID():
print(ctypes.windll.user32.GetForegroundWindow())
return ctypes.windll.user32.GetForegroundWindow()
Although I have to keep my PyCharm instance as the active window, it works fine, i'm able to send a message with the HWND now!
So I send a message with:
import ctypes
import subprocess # can be async
from ctypes import wintypes
from ctypes import *
with subprocess.Popen(r"C:\Users\lafft\Downloads\2021-04-11\3.35\ProxyTool\ProxyAPI.exe "
r"-changeproxy/US/NY/'New York' -citynolimit -proxyport=5001 -hwnd=330320",
stdin=subprocess.PIPE, stdout=subprocess.PIPE) as process:
for line in process.stdout:
print(line)
It works! The end program receives my message and performs the work I need it to do, this is the output:
b'Api executed successfully.\r\n'
Now I need to receive WM_COPYDATA from the end program to my hwnd, I modified some py2 code I found here:
class ACOPYDATASTRUCT(Structure):
_fields_ = [
('dwData', c_ulong),
('cbData', c_ulong),
('lpData', c_void_p)
]
PCOPYDATASTRUCT = POINTER(ACOPYDATASTRUCT)
class Listener:
def __init__(self):
message_map = {
win32con.WM_COPYDATA: self.OnCopyData
}
wc = win32gui.WNDCLASS()
wc.lpfnWndProc = message_map
wc.lpszClassName = 'MyWindowClass'
hinst = wc.hInstance = win32api.GetModuleHandle(None)
classAtom = win32gui.RegisterClass(wc)
self.hwnd = 330320
print(self.hwnd)
def OnCopyData(self, hwnd, msg, wparam, lparam):
print(hwnd)
print(msg)
print(wparam)
print(lparam)
pCDS = cast(lparam, PCOPYDATASTRUCT)
print(pCDS.contents.dwData)
print(pCDS.contents.cbData)
status = wstring_at(pCDS.contents.lpData)
print(status)
win32gui.PostQuitMessage(0)
return 1
l = Listener()
win32gui.PumpMessages()
Unfortunately, the message never comes in. I'm using PyCharm to run this, if it makes a difference.
I have 0 idea what to do now, and tutorials on the internet for this is sparse or written in python 2. Any help is appreciated, a link to a tutorial, some code, anything.