IPC in C#, sending text from one exe to another exe

前端 未结 2 1449
生来不讨喜
生来不讨喜 2021-02-11 04:32

I would like to send a message from a WPF application\'s textbox to an open notepad. After I click the button next to the the textbox, I would like the content is written into t

2条回答
  •  时光说笑
    2021-02-11 05:03

    [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    
    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
    
    private static void DoSendMessage(string message)
    {
        Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
        notepad.WaitForInputIdle();
    
        if (notepad != null)
        {
            IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
            SendMessage(child, 0x000C, 0, message);
        }
    }
    

提交回复
热议问题