static wstring getLastError(DWORD error = GetLastError())

{

LPVOID buf;

DWORD buf_len = 0;


if (12000 <= error && error <= 12175) {

static HMODULE handle = GetModuleHandleW(L"winhttp.dll");

buf_len= FormatMessageW(

FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE,

handle, //NULL,

error,

MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

(wchar_t*)&buf,

0, NULL);

} else {

buf_len= FormatMessageW(

FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,

NULL,

error,

MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),

(wchar_t*)&buf,

0, NULL);

}


wstringstream ss;

if (buf_len) {

wstring msg((wchar_t*)buf, (wchar_t*)buf + buf_len);

LocalFree(buf);


//LFCR(\r\b) 제거

auto it = msg.begin();

while (it != msg.end()) it = (*it == 0x000A || *it == 0x000D) ? msg.erase(it) : it + 1;

ss << msg;

}

ss << L"[" << error << L"]";

return ss.str();

}