|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import scrolledtext, messagebox |
| 3 | +import os |
| 4 | +import time |
| 5 | +import threading |
| 6 | +import win32file, win32pipe |
| 7 | + |
| 8 | +PIPE_NAME = r"\\.\pipe\MTTVirtualDisplayPipe" |
| 9 | + |
| 10 | +class PipeClientGUI: |
| 11 | + def __init__(self, root): |
| 12 | + self.root = root |
| 13 | + self.root.title("Virtual Display Driver Control") |
| 14 | + |
| 15 | + self.frame = tk.Frame(root) |
| 16 | + self.frame.pack(padx=10, pady=10) |
| 17 | + |
| 18 | + self.label = tk.Label(self.frame, text="Command:") |
| 19 | + self.label.grid(row=0, column=0, sticky="w") |
| 20 | + |
| 21 | + self.command_entry = tk.Entry(self.frame, width=40) |
| 22 | + self.command_entry.grid(row=0, column=1, padx=5, pady=5) |
| 23 | + |
| 24 | + self.send_button = tk.Button(self.frame, text="Send Command", command=self.send_command) |
| 25 | + self.send_button.grid(row=0, column=2, padx=5, pady=5) |
| 26 | + |
| 27 | + self.reconnect_button = tk.Button(self.frame, text="Reconnect", command=self.reconnect_pipe) |
| 28 | + self.reconnect_button.grid(row=0, column=3, padx=5, pady=5) |
| 29 | + |
| 30 | + self.log = scrolledtext.ScrolledText(self.frame, width=60, height=15) |
| 31 | + self.log.grid(row=1, column=0, columnspan=4, pady=10) |
| 32 | + |
| 33 | + self.pipe = None |
| 34 | + self.connect_pipe() |
| 35 | + |
| 36 | + def connect_pipe(self): |
| 37 | + """Attempts to connect to the named pipe.""" |
| 38 | + try: |
| 39 | + self.pipe = win32file.CreateFile( |
| 40 | + PIPE_NAME, |
| 41 | + win32file.GENERIC_READ | win32file.GENERIC_WRITE, |
| 42 | + 0, |
| 43 | + None, |
| 44 | + win32file.OPEN_EXISTING, |
| 45 | + 0, |
| 46 | + None |
| 47 | + ) |
| 48 | + self.log.insert(tk.END, "Connected to the Virtual Display Driver pipe.\n") |
| 49 | + except Exception as e: |
| 50 | + self.log.insert(tk.END, f"Failed to connect to pipe: {e}\n") |
| 51 | + self.pipe = None |
| 52 | + |
| 53 | + def reconnect_pipe(self): |
| 54 | + """Reconnects to the named pipe.""" |
| 55 | + self.log.insert(tk.END, "Attempting to reconnect...\n") |
| 56 | + self.connect_pipe() |
| 57 | + |
| 58 | + def send_command(self): |
| 59 | + """Sends a command to the driver.""" |
| 60 | + if not self.pipe: |
| 61 | + messagebox.showerror("Error", "Not connected to pipe.") |
| 62 | + return |
| 63 | + |
| 64 | + command = self.command_entry.get().strip() |
| 65 | + if not command: |
| 66 | + messagebox.showwarning("Warning", "Command cannot be empty.") |
| 67 | + return |
| 68 | + |
| 69 | + try: |
| 70 | + win32file.WriteFile(self.pipe, command.encode('utf-16le')) |
| 71 | + self.log.insert(tk.END, f"Sent: {command}\n") |
| 72 | + self.receive_response() |
| 73 | + except Exception as e: |
| 74 | + self.log.insert(tk.END, f"Error sending command: {e}\n") |
| 75 | + |
| 76 | + def receive_response(self): |
| 77 | + """Reads and displays response from the driver.""" |
| 78 | + try: |
| 79 | + _, data = win32file.ReadFile(self.pipe, 512) |
| 80 | + response = data.decode('utf-16le').strip() |
| 81 | + self.log.insert(tk.END, f"Response: {response}\n") |
| 82 | + except Exception as e: |
| 83 | + self.log.insert(tk.END, f"Error reading response: {e}\n") |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + root = tk.Tk() |
| 87 | + app = PipeClientGUI(root) |
| 88 | + root.mainloop() |
0 commit comments