26491e05edd6c3204372ea39c609448cc4681a8a
[libcds.git] / cds / os / win / syserror.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_OS_WIN_SYSERROR_H
4 #define __CDS_OS_WIN_SYSERROR_H
5
6 #include <windows.h>
7 #include <string>
8
9 namespace cds { namespace OS {
10     namespace Win32 {
11
12         /// OS-specific type of error code
13         typedef DWORD            error_code;
14
15         /// Get system error code
16         static inline error_code  getErrorCode()
17         {
18             return ::GetLastError();
19         }
20
21         /// Get system error text
22         static inline std::string getSystemErrorText( error_code nCode )
23         {
24             char *ptmp = 0;
25             if ( !FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
26                 nullptr,
27                 nCode,
28                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
29                 (LPTSTR) &ptmp, 0, 0  )
30                 )
31             {
32                 if ( ptmp )
33                     LocalFree( ptmp );
34                 return std::string();
35             }
36             std::string str( ptmp );
37             LocalFree( ptmp );
38             return str;
39         }
40     }    // namespace Win32
41
42     using Win32::error_code;
43     using Win32::getErrorCode;
44     using Win32::getSystemErrorText;
45 }} // namespace cds::OS
46
47 #endif // #ifndef __CDS_OS_WIN_SYSERROR_H
48