gpt4 book ai didi

c++ - 写入文件函数 "freezes"

转载 作者:可可西里 更新时间:2023-11-01 11:15:05 24 4
gpt4 key购买 nike

我正在尝试重写 xCmd,它可以在远程机器上启动进程。基本上,它将自身作为服务安装在目标计算机上,然后启动请求的进程。一切正常,但我注意到一个错误。 xCmd 通过管道进行通信,它使用 WriteFile()。我的问题是,如果我运行我的 API(或最初的 API,两者都会产生此错误),那么它将第一次启动请求的进程,但如果我再次启动它,那么它将“卡住”在这一行:

WriteFile( hCommandPipe, &msg, sizeof(msg), &dwTemp, NULL );

WriteFile 没有返回任何错误代码,程序就在这里停止。我什至无法关闭 cmd 窗口。当我关闭目标计算机上的服务时,我只能关闭它。谁能帮我解决这个问题?这真的很烦人,我不知道:(

这是无法正常工作的功能:

 BOOL ExecuteRemoteCommand()
{
DWORD dwTemp = 0;
xCmdMessage msg;
xCmdResponse response;

::ZeroMemory( &msg, sizeof(msg) );
::ZeroMemory( &response, sizeof(response) );

FillMessage( &msg );

// Send message to service
WriteFile( hCommandPipe, &msg, sizeof(msg), &dwTemp, NULL );

// Connects to remote pipes (stdout, stdin, stderr)
if ( ConnectToRemotePipes( 5, 1000 ) )
{
StdOutput( _T("Ok\n\n") );

// Waiting for response from service
ReadFile( hCommandPipe, &response, sizeof(response), &dwTemp, NULL );
}
else
StdOutput( _T("Failed\n\n") );

if ( response.dwErrorCode == 0 )
_tprintf( _T("\nRemote command returned %d(0x%X)\n"),
response.dwReturnCode,
response.dwReturnCode );
else
_tprintf( _T("\nRemote command failed to start. Returned error code is %d(0x%X)\n"),
response.dwErrorCode,
response.dwErrorCode );

return TRUE;
}

提前致谢!

坎比

最佳答案

我认为这意味着管道连接的另一端没有从管道中读取数据并且管道缓冲区已满。

它也可能表明另一端关闭了他们的管道句柄。对于在您尝试编写时会导致错误的匿名管道,但我不记得命名管道是否也是如此。

If the pipe buffer is full when an application uses the WriteFile function to write to a pipe, the write operation may not finish immediately. The write operation will be completed when a read operation (using the ReadFile function) makes more system buffer space available for the pipe.

MSDN

The write operation will block until the data is read from the pipe so that the additional buffer quota can be released.

MSDN

关于c++ - 写入文件函数 "freezes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1767479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com