【发布时间】:2026-02-06 07:00:02
【问题描述】:
bool sendMessageToGraphics(char* msg)
{
//char ea[] = "SSS";
char* chRequest = msg; // Client -> Server
DWORD cbBytesWritten, cbRequestBytes;
// Send one message to the pipe.
cbRequestBytes = sizeof(TCHAR) * (lstrlen(chRequest) + 1);
if (*msg - '8' == 0)
{
char new_msg[1024] = { 0 };
string answer = "0" + '\0';
copy(answer.begin(), answer.end(), new_msg);
char *request = new_msg;
WriteFile(hPipe, request, cbRequestBytes, &cbRequestBytes, NULL);
}
BOOL bResult = WriteFile( // Write to the pipe.
hPipe, // Handle of the pipe
chRequest, // Message to be written
cbRequestBytes, // Number of bytes to writ
&cbBytesWritten, // Number of bytes written
NULL); // Not overlapped
if (!bResult/*Failed*/ || cbRequestBytes != cbBytesWritten/*Failed*/)
{
_tprintf(_T("WriteFile failed w/err 0x%08lx\n"), GetLastError());
return false;
}
_tprintf(_T("Sends %ld bytes; Message: \"%s\"\n"),
cbBytesWritten, chRequest);
return true;
}
在运行第一个 writefile 之后(如果是 '8'),另一个 writefile 函数不能正常工作,有人能理解为什么吗? 函数 sendMessageToGraphics 需要发送移动到棋盘
【问题讨论】:
-
如何它“工作正常”?当您第二次调用该函数时会发生什么?你怎么称呼你的
sendMessageToGraphics函数?为什么不检查第一次WriteFile调用的结果? -
第二次没有到达终点(_tprintf 部分)它只是返回到调用 sendMessageToGraphics 的函数
-
哦,而不是做例如
*msg - '8' == 0如果你这样做了,会不会更容易(而且更“可读”)msg[0] == '8'? -
可能是,但我想这不是问题
-
所以你的意思是函数只是返回,甚至没有打印错误信息?您是否尝试过在调试器中单步执行该函数?
标签: c++ windows chess writefile