• Welcome to Valhalla Legends Archive.
 

FormatMessage returning garbage

Started by Eli_1, June 11, 2004, 10:41 PM

Previous topic - Next topic

Eli_1

Quote from: Moonshine on June 12, 2004, 05:49 PM
... and if you create multiple instances of your socket class, you'll be WSAStartup()'ing several times unnecessarily.

Oops, I never thought about that. Thanks, moon.

Quote...
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)errorbuff, 512, NULL)
...

Does anyone know why the function always fails when I pass errors returned by WSAGetLastError to it?

And why do you typecast to LPSTR and not LPTSTR? Are they the same, or is there some magical pointer thing that I'm still not grasping?

I don't understand all these different types!
*pulls out hair*

Moonshine

#16
I tried using FormatMessage on WSAGetLastError(), and it worked for me:

A test on the fly I did:


#include <winsock2.h>
#include <iostream>

using namespace std;

int main( void ) {
  WSADATA WSAData;
  WSAStartup(MAKEWORD(2,0), &WSAData);

  sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = inet_addr("1.1.1.a");
  addr.sin_port = htons(6112);

  char szBuffer[512];

  SOCKET mysock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (mysock != INVALID_SOCKET) {

     if (connect(mysock, (sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) {
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL,
                       SUBLANG_DEFAULT), szBuffer, (sizeof(szBuffer) / sizeof(TCHAR)) - 1, NULL);

        cout << "Error Msg: " << szBuffer << endl;
     }
  }

  WSACleanup();

  return (0);
}


Typecasting the buffer to LPTSTR or LPSTR when inputting it to FormatMessage() won't effect it I don't think; it'll treat the buffer as a TCHAR* regardless (unless you specify the local allocate flag of course)

Also again be careful about that maximum buffer size parameter, it takes in the number of TCHARs (2-bytes in size?) NOT bytes maximum.

PS: LPTSTR is a TCHAR* ; LPSTR is char*

Eli_1

I compiled your code in a different project and got this output:

Quote
Error Msg:

My computer hates me or something...  :-\

Moonshine

#18
Quote from: Eli_1 on June 12, 2004, 07:40 PM
I compiled your code in a different project and got this output:

Quote
Error Msg:

My computer hates me or something...  :-\

Realllly? What compiler are you using?  I'm using Microsoft Visual C++ .NET (2003), and I received an error message (too lazy to check what it was again), works for me..

Eli_1

Whatever the free Borland compiler is. But remember, I'm recieving an error message when I pass an error code of 5, but not 10049 (WSAGetLastError).

Moonshine

Quote from: Eli_1 on June 12, 2004, 08:52 PM
Whatever the free Borland compiler is. But remember, I'm recieving an error message when I pass an error code of 5, but not 10049 (WSAGetLastError).

Eww @ Borland :P