mxc*_*xcl 36
wchar_t *s = NULL;
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, WSAGetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&s, 0, NULL);
fprintf(stderr, "%S\n", s);
LocalFree(s);
Run Code Online (Sandbox Code Playgroud)
CB *_*ley 16
正如文档所述,WSAGetLastError
您可以使用它FormatMessage
来获取错误消息的文本版本.
您需要FORMAT_MESSAGE_FROM_SYSTEM
在dwFlags
参数中设置并将错误代码作为dwMessage
参数传递.
mxcl答案的稍简单版本,它消除了对malloc / free的需求以及其中所隐含的风险,并且可以处理没有可用消息文本的情况(因为Microsoft并未记录随后发生的情况):
int
err;
char
msgbuf [256]; // for a message up to 255 bytes.
msgbuf [0] = '\0'; // Microsoft doesn't guarantee this on man page.
err = WSAGetLastError ();
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags
NULL, // lpsource
err, // message id
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // languageid
msgbuf, // output buffer
sizeof (msgbuf), // size of msgbuf, bytes
NULL); // va_list of arguments
if (! *msgbuf)
sprintf (msgbuf, "%d", err); // provide error # if no string available
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20659 次 |
最近记录: |