Search code examples
javawindowsprocessbuilderpostmessage

Is possible to send VM_CHAR message to a console program launch by ProcessBuilder? (windows platform)


I want to simulate VK_ENTER to a console program.

I can post VK_ENTER message successfully when I launch the program by cmd.exe manually.

It doesn't to find the hwnd of the console program that launch by ProcessBuilder.

@Test
public void testGetPid(){
    ProcessBuilder pb = new ProcessBuilder("test.exe"); // not work
    // ProcessBuilder pb = new ProcessBuilder("notepad.exe"); // work
    Process p = null;
    try {
        p = pb.start();

        //"handle"

        System.out.println(p);

         Field f = p.getClass().getDeclaredField( "handle");
         f.setAccessible( true);
         long procHandle = f.getLong( p);

        System.out.println("Handle: " + procHandle);

        //Kernel32.INSTANCE.

        HANDLE handle = new HANDLE();
        handle.setPointer(Pointer.createConstant(procHandle));

        final int pid = Kernel32.INSTANCE.GetProcessId(handle);
        System.out.println("Pid: " + pid);

        ThreadUtil.sleep(5*1000);

        sendKeyToProcess(pid);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        ThreadUtil.sleep(60*1000);
        if( p != null )
            p.destroy();
    }

}

        private void sendKeyToProcess(final int pid) {
    final User32 user32Lib = User32.INSTANCE;

    final int WM_KEYDOWN = 0x0100;
    final int WM_KEYUP = 0x0101;
    final int WM_CHAR = 0x0102;


    user32Lib.EnumWindows(new WinUser.WNDENUMPROC() {
        @Override
        public boolean callback(HWND paramHWND, Pointer paramPointer) {

            IntByReference intRef = new IntByReference();
            user32Lib.GetWindowThreadProcessId(paramHWND, intRef);

            if( pid == intRef.getValue() ){
                // final WPARAM wPARAM = new WPARAM(13);  // enter
                final WPARAM wPARAM = new WPARAM(48);  // '0'
                final LPARAM lPARAM = new LPARAM(0);

                System.out.println("Match hwnd: " + paramHWND.toString());
                user32Lib.PostMessage(paramHWND,
                        WM_KEYDOWN, wPARAM, lPARAM);
                User32.INSTANCE.EnumChildWindows(paramHWND,
                        new WinUser.WNDENUMPROC() {
                            @Override
                            public boolean callback(HWND childParamHWND,
                                    Pointer paramPointer) {
                                System.out.println("send");
                                user32Lib.PostMessage(childParamHWND,
                                        WM_KEYDOWN, wPARAM, lPARAM);
                                return true;
                            }
                        }, null);

                //return false;
            }

            return true;
        }
    }, null);
}

Solution

  • It's work for me: (All JNA solution)

    1. CreateProcess with STARTF_USESHOWWINDOW and CREATE_NEW_CONSOLE flag
    2. EnumWindows with PROCESS_INFORMATION dwProcessId to find the process window id (GetWindowThreadProcessId)
    3. PostMessage with the process window id to send enter