#ifndef CWINERR_H
#define CWINERR_H

#include "GdCPP_Exports.h"
#include <stdint.h>
#include <xstring>

GDCPP_API wchar_t* getWinErrString(DWORD winErrCode);
GDCPP_API DWORD getWinErrString(DWORD winErrCode, wchar_t* lpMsgBuf, DWORD   nSize);

class GDCPP_API CWinErr
{
public:
    CWinErr(uint32_t bufsize=1024)
	    : bufSize(bufsize)
    {
        pMsg = new wchar_t[bufSize];
    }

    ~CWinErr()
    {
        delete [] pMsg;
    }

    /// 记录Windows API执行出错后用GetLastError()获取的错误码。
    uint32_t errCode=0;

    wchar_t* pMsg = nullptr;
    uint32_t bufSize = 0;

    DWORD GetLastError()
    {
        errCode = ::GetLastError();
        return getWinErrString(errCode, pMsg, bufSize);
    }

    DWORD WSAGetLastError()
    {
        errCode = ::WSAGetLastError();
        return getWinErrString(errCode, pMsg, bufSize);
    }
};

#endif // CWINERR_H
