summaryrefslogtreecommitdiff
path: root/w32/subproc/w32err.c
blob: 712fccdfcc80576308ca9d67eabd370811df6250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <windows.h>
#include "w32err.h"

/*
 * Description: the windows32 version of perror()
 *
 * Returns:  a pointer to a static error
 *
 * Notes/Dependencies:  I got this from
 *      comp.os.ms-windows.programmer.win32
 */
char *
map_windows32_error_to_string (DWORD ercode) {
/* __declspec (thread) necessary if you will use multiple threads on MSVC */
#ifdef _MSC_VER
__declspec (thread) static char szMessageBuffer[128];
#else
static char szMessageBuffer[128];
#endif
	/* Fill message buffer with a default message in
	 * case FormatMessage fails
	 */
    wsprintf (szMessageBuffer, "Error %ld\n", ercode);

	/*
	 *  Special code for winsock error handling.
	 */
	if (ercode > WSABASEERR) {
		HMODULE hModule = GetModuleHandle("wsock32");
		if (hModule != NULL) {
			FormatMessage(FORMAT_MESSAGE_FROM_HMODULE,
				hModule,
				ercode,
				LANG_NEUTRAL,
				szMessageBuffer,
				sizeof(szMessageBuffer),
				NULL);
			FreeLibrary(hModule);
		}
	} else {
		/*
		 *  Default system message handling
		 */
    	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
                  NULL,
                  ercode,
                  LANG_NEUTRAL,
                  szMessageBuffer,
                  sizeof(szMessageBuffer),
                  NULL);
	}
    return szMessageBuffer;
}