55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
"""Simulate dropping a file onto a window via WM_DROPFILES."""
|
|
import ctypes
|
|
import sys
|
|
import time
|
|
from ctypes import wintypes
|
|
|
|
import win32gui
|
|
|
|
user32 = ctypes.WinDLL("user32", use_last_error=True)
|
|
user32.PostMessageW.argtypes = [wintypes.HWND, wintypes.UINT, ctypes.c_size_t, ctypes.c_ssize_t]
|
|
user32.PostMessageW.restype = wintypes.BOOL
|
|
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
|
|
|
|
|
|
class DROPFILES(ctypes.Structure):
|
|
_fields_ = [
|
|
("pFiles", wintypes.DWORD),
|
|
("pt", wintypes.POINT),
|
|
("fNC", wintypes.BOOL),
|
|
("fWide", wintypes.BOOL),
|
|
]
|
|
|
|
|
|
def drop(hwnd, path, x, y):
|
|
files = path.encode("utf-16-le") + b"\x00\x00\x00\x00"
|
|
header = DROPFILES()
|
|
header.pFiles = ctypes.sizeof(DROPFILES)
|
|
header.pt = wintypes.POINT(x, y)
|
|
header.fNC = False
|
|
header.fWide = True
|
|
payload = bytes(header) + files
|
|
size = len(payload)
|
|
kernel32.GlobalAlloc.argtypes = [wintypes.UINT, ctypes.c_size_t]
|
|
kernel32.GlobalAlloc.restype = wintypes.HGLOBAL
|
|
kernel32.GlobalLock.argtypes = [wintypes.HGLOBAL]
|
|
kernel32.GlobalLock.restype = ctypes.c_void_p
|
|
kernel32.GlobalUnlock.argtypes = [wintypes.HGLOBAL]
|
|
kernel32.GlobalUnlock.restype = wintypes.BOOL
|
|
hglobal = kernel32.GlobalAlloc(0x0042, size) # GMEM_MOVEABLE | GMEM_ZEROINIT
|
|
ptr = kernel32.GlobalLock(hglobal)
|
|
ctypes.memmove(ptr, payload, size)
|
|
kernel32.GlobalUnlock(hglobal)
|
|
user32.PostMessageW(hwnd, 0x0233, hglobal, 0) # WM_DROPFILES
|
|
return hglobal
|
|
|
|
|
|
if __name__ == "__main__":
|
|
hwnd = int(sys.argv[1])
|
|
path = sys.argv[2]
|
|
x, y = (int(sys.argv[3]), int(sys.argv[4])) if len(sys.argv) > 4 else (400, 300)
|
|
win32gui.SetForegroundWindow(hwnd)
|
|
h = drop(hwnd, path, x, y)
|
|
time.sleep(2.0)
|
|
print("dropped", path, "hglobal", h)
|