0
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;

    }

after the first writefile in running (In case of '8') the other writefile function doesn't work right, can someone understand why ? the function sendMessageToGraphics need to send move to chess board

ronbob
  • 13
  • 6

1 Answers1

1

There are 2 problems in your code:

First of all, there's a (minor) problem where you initialize a string in your conditional statement. You initialize it as so:

string answer = "0" + '\0';

This does not do what you think it does. It will invoke the operator+ using const char* and char as its argument types. This will perform pointer addition, adding the value of '\0' to where your constant is stored. Since '\0' will be converted to the integer value of 0, it will not add anything to the constant. But your string ends up not having a '\0' terminator. You could solve this by changing the statement to:

string answer = std::string("0") + '\0';

But the real problem lies in the way you use your size variables. You first initialize the size variable to the string length of your input variable (including the terminating '\0' character). Then in your conditional statement you create a new string which you pass to WriteFile, yet you still use the original size. This may cause a buffer overrun, which is undefined behavior. You also set your size variable to however many bytes you wrote to the file. Then later on you use this same value again in the next call. You never actually check this value, so this could cause problems.

The easiest way to change this, is to make sure your sizes are set up correctly. For example, instead of the first call, you could do this:

WriteFile(hPipe, request, answer.size(), &cbBytesWritten, NULL);

Then check the return value WriteFile and the value of cbBytesWritten before you make the next call to WriteFile, that way you know your first call succeeded too.

Also, do not forget to remove your sizeof(TCHAR) part in your size calculation. You are never using TCHAR in your code. Your input is a regular char* and so is the string you use in your conditional. I would also advice replacing WriteFile by WriteFileA to show you are using such characters.

Last of all, make sure your server is actually reading bytes from the handle you write to. If your server does not read from the handle, the WriteFile function will freeze until it can write to the handle again.

Shadowwolf
  • 973
  • 5
  • 13