返回

c++-如何在 Windows 控制台中运行的 FarManager 中执行命令

发布时间:2022-08-08 01:45:03 207
# golang

我有一个客户端服务器应用程序。使用该CreateProcess()功能,在服务器上创建一个新进程,它实际上就像一个远程控制台。客户端发送一个命令,它在服务器控制台中执行并将结果返回给服务器。为了与控制台通信,我使用WriteFile()and ReadFile()。这部分效果很好。

但我还需要能够在服务器控制台中运行FarManager并对其进行管理。

我可以使用环境变量在服务器上运行 FarManager,或者在创建进程时直接指定它。但运行程序后,我无法发出任何命令(例如,按下向下箭头)。看起来当前的管道实现不适合这项任务。

任何人都可以建议对此有用的东西吗?

CreateProcess函数:

我没有附上所有相关的结构,因为问题似乎不在进程本身

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 

WriteToPipe函数:

bool WriteToPipe(char* com)
{
    OVERLAPPED osWrite = { 0 };
    DWORD dwWritten;
    DWORD dwRes;
    BOOL fRes;

    // Create this write operation's OVERLAPPED structure's hEvent.
    osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (osWrite.hEvent == NULL) {
        // error creating overlapped event handle
        throw std::exception("Error creating overlapped event");
    }

    // Issue write.
    if (!WriteFile(console_stdIn_wr, com, strlen(com), &dwWritten, &osWrite)) {
        if (GetLastError() != ERROR_IO_PENDING) {
            // WriteFile failed, but isn't delayed. Report error and abort.
            std::cout << " WriteFile failed, but isn't delayed" << std::endl;
            fRes = FALSE;
        }
        else {
            // Write is pending.
            dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
            switch (dwRes)
            {
                // OVERLAPPED structure's event has been signaled. 
            case WAIT_OBJECT_0:
                if (!GetOverlappedResult(console_stdIn_wr, &osWrite, &dwWritten, FALSE))
                    fRes = FALSE;
                else
                    // Write operation completed successfully.
                    fRes = TRUE;
                break;
            default:
                // An error has occurred in WaitForSingleObject.
                // This usually indicates a problem with the
                // OVERLAPPED structure's event handle.
                fRes = FALSE;
                break;
            }
        }
    }
    else {
        // WriteFile completed immediately.
        fRes = TRUE;
    }


    CloseHandle(osWrite.hEvent);
    return fRes;
}

我不明白如何向打开的 FarManager 发出命令?哪个功能值得看,目前管道的实现是否适合这个?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像