0

I have an application that use a CreateProcess() function and create a new console window with FarManager run inside. The FarManager is installed on the computer and registered in the environment variable. The application itself is a server and accepts commands from the client. I have already implemented on the client the reading of pressed buttons and their transfer to the server. Pressing these buttons is simulated on the server and they are successfully executed in a window with an open FarManager. But now I need to get what is displayed in this window and somehow transfer it back to the client. At the same time, the manager is not installed on the client. I don't understand how to do it. I would be grateful for any ideas and advices.

CreateProcess function:

TCHAR szCmdline[] = TEXT("C:\\Windows\\System32\\cmd.exe");
TCHAR prog[] = TEXT("D:\\Program Files\\Far Manager x86\\Far.exe");

// Create the child process. 
bSuccess = CreateProcess(
    prog,           // far
    szCmdline,      // command line 
    NULL,           // process security attributes 
    NULL,           // primary thread security attributes 
    TRUE,           // handles are inherited
    CREATE_NEW_CONSOLE,   // creation flags
    NULL,           // use parent's environment 
    NULL,           // use parent's current directory 
    &si,            // STARTUPINFO pointer 
    &pi);           // receives PROCESS_INFORMATION

Execute command in FarManager function

//nVirtKey - code of virtual key to run in far
void ProcessInfo::RunFar(int nVirtKey){ 
    //Create structure with buttons and their state
    INPUT inputs[2] = {};
    ZeroMemory(inputs, sizeof(inputs));

    //Set key, that must be "pressed"
    inputs[0].type = INPUT_KEYBOARD;
    inputs[0].ki.wVk = static_cast<WORD>(nVirtKey);

    //Set state of key
    inputs[1].type = INPUT_KEYBOARD;
    inputs[1].ki.wVk = static_cast<WORD>(nVirtKey);
    inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;

    //Set position of window with FarManger to the foreground (by the handle of window)
    SetForegroundWindow(hwnd);

    //Send key structure to window
    UINT uSent = SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));   
}

Finction, that I used to find hwnd of window with FarManager by id of process (it's a little weird but works great)

//Find handle of far windows
HWND ProcessInfo::GetLastWindowsFromProcessID(){
    HWND vhWnds = 0;
    // find all hWnds (vhWnds) associated with a process id (dwProcessID)
    HWND hCurWnd = NULL;
    do
    {
        hCurWnd = FindWindowEx(NULL, hCurWnd, NULL, NULL);
        DWORD dwProcID = 0;
        GetWindowThreadProcessId(hCurWnd, &dwProcID);
        if (dwProcID == pi.dwProcessId)
        {
            vhWnds = hCurWnd;  // add the found hCurWnd
        }
    } while (hCurWnd != NULL);

    return vhWnds;
}

Thanks in advance for any help!

Zhe87
  • 11
  • 3
  • Have a look at `CreatePipe()` it's not really easy to setup standard input and output handles correctly to communicate in both directions. – πάντα ῥεῖ Jul 29 '22 at 16:26
  • @πάνταῥεῖ probably won't work - if you check the screenshots page of FarManager it's full of text-mode GUIs. Perhaps you could use ReadConsoleOutput to read the contents of the screen, instead. – user253751 Jul 29 '22 at 16:52

0 Answers0