Well, I'm running out of ideas :) but two things in your code caught my eye. Not that I think these will account for the problem, but it may be worth checking:
You've opened the port with FILE_SHARE_READ|FILE_SHARE_WRITE: Microsoft docs say dwShareMode must be zero for communications resources.
The piece of code
if(bResult == 0)
{
MessageBox(NULL, "WriteFile() failed", NULL, 0);
//dwLastError = GetLastError();
sprintf(str1, "Return value of GetLastError() = %d", GetLastError());
...
should be written
if(bResult == 0)
{
DWORD dwLastError = GetLastError();
MessageBox(NULL, "WriteFile() failed", NULL, 0);
//dwLastError = GetLastError();
sprintf(str1, "Return value of GetLastError() = %d", (int)GetLastError());
...
to avoid the possibility of MessageBox altering the error code.
Tell us what this leads to... Joaquín M López Muñoz Telefónica, Investigación y Desarrollo