# Reproduce: attach ch5 -> PLAY -> LOAD EZkeys ch5 (editor open) -> STOP -> WM_CLOSE -> watch import ctypes, ctypes.wintypes as w, struct, subprocess, sys, os, time, re sys.stdout.reconfigure(encoding='utf-8', errors='replace') APP_PID = int(sys.argv[1]) SHM_NAME = f"SonicForge_DAW_IPC_{APP_PID}".encode() APP_LOG = os.path.join(os.environ.get('APPDATA', '.'), 'SonicForgeDAW', 'logs', 'bridge.log') NEXUS = r"C:\Program Files\Common Files\VST3\Nexus.vst3" EZKEYS = r"C:\Program Files\Common Files\VST3\Toontrack\EZkeys 2.vst3" kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) kernel32.OpenFileMappingA.argtypes = [ctypes.wintypes.DWORD, w.BOOL, ctypes.c_char_p] kernel32.OpenFileMappingA.restype = ctypes.c_void_p kernel32.MapViewOfFile.argtypes = [ctypes.c_void_p, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.c_size_t] kernel32.MapViewOfFile.restype = ctypes.c_void_p hMap = kernel32.OpenFileMappingA(0xF001F, False, SHM_NAME) ptr = kernel32.MapViewOfFile(hMap, 0xF001F, 0, 0, 0) user32 = ctypes.windll.user32 def w32(off, fmt, *args): ctypes.memmove(ptr + off, struct.pack(fmt, *args), struct.calcsize(fmt)) def rd32(off): return ctypes.c_uint32.from_address(ptr + off).value def write_control(ctype, arg0, arg1, ch, path): for i in range(8): off = 2836 + i * 1040 if rd32(off) == 0: w32(off, 'IIII', ctype, arg0, arg1, ch) if path: b = path.encode('utf-8')[:1023] + b'\x00' ctypes.memmove(ptr + off + 16, b, len(b)) return i def push_controls(events): for i in range(8): w32(2836 + i * 1040, 'I', 0) for (ctype, arg0, arg1, ch, path) in events: write_control(ctype, arg0, arg1, ch, path) w32(11156, 'I', len(events)) def log_lines(): try: with open(APP_LOG, 'r', encoding='utf-8', errors='replace') as f: return f.readlines() except FileNotFoundError: return [] def wait_log(pattern, timeout, since, verbose=True): end = time.time() + timeout while time.time() < end: lines = log_lines() for i in range(since, len(lines)): if pattern in lines[i]: if verbose: print(f" LOG: {lines[i].strip()}") return lines[i].strip() time.sleep(0.2) return None def find_hwnd(ch): for l in log_lines(): if f"ch={ch}" in l and "GUI attached" in l: m = re.search(r'hwnd=([0-9A-Fa-f]+)', l) if m: return int(m.group(1), 16) return None since = len(log_lines()) print("== step 1: attach ch5 ==") push_controls([(4, 0, 0, 5, "Nexus")]) r = wait_log("GUI attached", 30, since) print("attach:", "OK" if r else "TIMEOUT") hwnd5 = None if r: m = re.search(r'hwnd=([0-9A-Fa-f]+)', r) if m: hwnd5 = int(m.group(1), 16) print("hwnd5:", hex(hwnd5) if hwnd5 else None) print("IsWindow:", user32.IsWindow(hwnd5), "tid:", user32.GetWindowThreadProcessId(hwnd5, None), "visible:", user32.IsWindowVisible(hwnd5)) since = len(log_lines()) print("== step 2: PLAY ==") push_controls([(3, 1, 0, 0, "")]) wait_log("transport PLAY", 10, since) since = len(log_lines()) print("== step 3: LOAD EZkeys ch5 while editor open ==") push_controls([(2, 0, 0, 5, EZKEYS)]) r = wait_log("instrument loaded ch=5", 60, since) print("load:", "OK" if r else "TIMEOUT") print("IsWindow:", user32.IsWindow(hwnd5), "visible:", user32.IsWindowVisible(hwnd5), "child:", user32.FindWindowExA(hwnd5, None, None, None)) since = len(log_lines()) print("== step 4: STOP ==") push_controls([(3, 0, 0, 0, "")]) wait_log("transport STOP", 10, since) since = len(log_lines()) print("== step 5: WM_CLOSE to", hex(hwnd5) if hwnd5 else "None", "watch 45s ==") if hwnd5: user32.PostMessageW(hwnd5, 0x0010, 0, 0) end = time.time() + 45 seen = [] while time.time() < end: ls = log_lines() for l in ls[since:]: if any(k in l for k in ("state captured", "state restored", "closeGUI", "EXCEPTION", "FAILED")): if l.strip() not in seen: print(" LOG:", l.strip()) seen.append(l.strip()) if any("state restored ch=5" in s for s in seen): break time.sleep(0.3) if not any("state restored ch=5" in s for s in seen): print(" NO 'state restored ch=5' in 45s; last 10 log lines:") for l in log_lines()[-10:]: print(" ", l.strip()) print("IsWindow after:", user32.IsWindow(hwnd5), "visible:", user32.IsWindowVisible(hwnd5))