프로그래밍 언어/C++
[API] System Error 메시지 사용하기 WSAGetLastError()
copynull
2010. 1. 20. 09:44
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 | // test.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다. // #include "stdafx.h" #include "winsock2.h" #pragma comment(lib, "ws2_32.lib") // 오류 출력 후 종료 void err_quit(char *msg) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); MessageBox(NULL, (LPCTSTR)lpMsgBuf, msg, MB_ICONERROR); LocalFree(lpMsgBuf); exit(-1); } // 오류 출력 MessageBox void err_display(char *msg) { LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); printf("[%s] %s", msg, (LPCTSTR)lpMsgBuf); LocalFree(lpMsgBuf); } int _tmain(int argc, _TCHAR* argv[]) { err_display("korea"); // show error comment err_quit("quit"); // show error MessageBox return 0; } |
WSAGetLastError() 함수를 이용하여 시스템 에러메시지를 유니코드별로 출력해준다.