Click to See Complete Forum and Search --> : error text for winsock


AltayKarakus
April 16th, 2003, 02:02 PM
Hi, all.
how can I get error message texts for winsock errors to inform users when they use my program.
For example
WSAGetLastError() gives 10065 then I want to display a message
that says the meaning of this message.

thanx

Mick
April 16th, 2003, 02:55 PM
Originally posted by AltayKarakus
Hi, all.
how can I get error message texts for winsock errors to inform users when they use my program.
For example
WSAGetLastError() gives 10065 then I want to display a message
that says the meaning of this message.

thanx

You can use FormatMessage() for this. Here is a sample:


#include <windows.h>
#include <stdio.h>

#include <lmerr.h>

void
DisplayErrorText(
DWORD dwLastError
);

#define RTN_OK 0
#define RTN_USAGE 1
#define RTN_ERROR 13

int
__cdecl
main(
int argc,
char *argv[]
)
{
if(argc != 2) {
fprintf(stderr,"Usage: %s <error number>\n", argv[0]);
return RTN_USAGE;
}

DisplayErrorText( atoi(argv[1]) );

return RTN_OK;
}

void
DisplayErrorText(
DWORD dwLastError
)
{
HMODULE hModule = NULL; // default to system source
LPSTR MessageBuffer;
DWORD dwBufferLength;

DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM ;

//
// If dwLastError is in the network range,
// load the message source.
//

if(dwLastError >= NERR_BASE && dwLastError <= MAX_NERR) {
hModule = LoadLibraryEx(
TEXT("netmsg.dll"),
NULL,
LOAD_LIBRARY_AS_DATAFILE
);

if(hModule != NULL)
dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;
}

//
// Call FormatMessage() to allow for message
// text to be acquired from the system
// or from the supplied module handle.
//

if(dwBufferLength = FormatMessageA(
dwFormatFlags,
hModule, // module to get message from (NULL == system)
dwLastError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
(LPSTR) &MessageBuffer,
0,
NULL
))
{
DWORD dwBytesWritten;

//
// Output message string on stderr.
//
WriteFile(
GetStdHandle(STD_ERROR_HANDLE),
MessageBuffer,
dwBufferLength,
&dwBytesWritten,
NULL
);

//
// Free the buffer allocated by the system.
//
LocalFree(MessageBuffer);
}

//
// If we loaded a message source, unload it.
//
if(hModule != NULL)
FreeLibrary(hModule);
}

AltayKarakus
April 17th, 2003, 04:44 AM
thanks for your reply but I think there could be simple app function for this like
GetMessageText(#) or some what else I will try your code

mahanare
April 17th, 2003, 07:28 AM
I Too need this.
I think I did something to display error description.

but any GURU can help us
I feel the following is enough

DWORD FormatMessage(
DWORD dwFlags,
LPCVOID lpSource,
DWORD dwMessageId,
DWORD dwLanguageId,
LPTSTR lpBuffer,
DWORD nSize,
va_list* Arguments
);

cheers
mahanare

mahanare
April 17th, 2003, 07:54 AM
from MSDN

LPVOID lpMsgBuf;
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
// Handle the error.
return;
}

// Process any inserts in lpMsgBuf.
// ...

// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );

// Free the buffer.
LocalFree( lpMsgBuf );

mahanare
April 17th, 2003, 07:55 AM
from MSDN, how to convert the error number to description.
may be of interest

LPVOID lpMsgBuf;
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
// Handle the error.
return;
}

// Process any inserts in lpMsgBuf.
// ...

// Display the string.
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );

// Free the buffer.
LocalFree( lpMsgBuf );


if not satisified.. then visit
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/looking_up_text_for_error_code_numbers.asp