[RESOLVED] How to "FormatMessage()" my "WSAGetLastError()"
I'm trying to generate a message string from a WSAGetLastError()) error code (Winsock2).
My first attempt:
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM;
LPSTR lpBuffer = NULL;
DWORD size = FormatMessage(flags, NULL, WSAGetLastError(), 0, (LPSTR)&lpBuffer, 0, NULL);
FormatMessage fails with error 1813 (ERROR_RESOURCE_TYPE_NOT_FOUND).
My next attempt:
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE;
HMODULE hInst = AfxLoadLibrary(ws2_32.dll);
LPSTR lpBuffer = NULL;
DWORD size = FormatMessage(flags, hInst, WSAGetLastError(), 0, (LPSTR)&lpBuffer, 0, NULL);
FormatMessage fails with error 15100 (ERROR_MUI_FILE_NOT_FOUND).
What am I missing here?
/Ove
Re: How to "FormatMessage()" my "WSAGetLastError()"
Quote:
Originally Posted by
\Ove
I'm trying to generate a message string from a WSAGetLastError()) error code (Winsock2).
My first attempt:
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM;
LPSTR lpBuffer = NULL;
DWORD size = FormatMessage(flags, NULL, WSAGetLastError(), 0, (LPSTR)&lpBuffer, 0, NULL);
FormatMessage fails with error 1813 (ERROR_RESOURCE_TYPE_NOT_FOUND).
This code worked for me:
Code:
DWORD dwErr = GetLastError();
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwErr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
CString strMess = (LPTSTR) lpMsgBuf;
// Free the buffer.
LocalFree( lpMsgBuf );
// add message with dll name:
strMess += _T("\r\n\'") + strPath + _T('\'');
AfxMessageBox( strMess, MB_OK | MB_ICONSTOP );
Re: How to "FormatMessage()" my "WSAGetLastError()"
Quote:
Originally Posted by
\Ove
...
My next attempt:
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE;
HMODULE hInst = AfxLoadLibrary(ws2_32.dll);
LPSTR lpBuffer = NULL;
DWORD size = FormatMessage(flags, hInst, WSAGetLastError(), 0, (LPSTR)&lpBuffer, 0, NULL);
FormatMessage fails with error 15100 (ERROR_MUI_FILE_NOT_FOUND).
What am I missing here?
/Ove
Sorry, I never used this flag (FORMAT_MESSAGE_FROM_HMODULE).
Have a look at these threads:
https://forums.codeguru.com/showthre...ODULE-argument
https://forums.codeguru.com/showthre...or-description
Re: How to "FormatMessage()" my "WSAGetLastError()"
Quote:
Originally Posted by
\Ove
I'm trying to generate a message string from a WSAGetLastError()) error code (Winsock2).
My first attempt:
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM;
LPSTR lpBuffer = NULL;
DWORD size = FormatMessage(flags, NULL, WSAGetLastError(), 0, (LPSTR)&lpBuffer, 0, NULL);
FormatMessage fails with error 1813 (ERROR_RESOURCE_TYPE_NOT_FOUND).
My next attempt:
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE;
HMODULE hInst = AfxLoadLibrary(ws2_32.dll);
LPSTR lpBuffer = NULL;
DWORD size = FormatMessage(flags, hInst, WSAGetLastError(), 0, (LPSTR)&lpBuffer, 0, NULL);
FormatMessage fails with error 15100 (ERROR_MUI_FILE_NOT_FOUND).
What am I missing here?
/Ove
Maybe I should add that I'm using Windows 10 with VS 2019.
\Ove
Re: How to "FormatMessage()" my "WSAGetLastError()"
This is what I used when I did Windows programming:
Code:
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <string>
#include <iostream>
DWORD WinError(std::string& werror, DWORD p_dwError) {
LPSTR MessageBuffer;
werror.clear();
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, p_dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&MessageBuffer, 0, NULL) > 0) {
werror = MessageBuffer;
free(MessageBuffer);
return 0;
}
return GetLastError();
}
int main() {
std::string msg;
if (auto ret{ WinError(msg, 995) }; !ret)
std::cout << msg << '\n';
else
std::cout << "Error - " << ret << '\n';
}
Code:
The I/O operation has been aborted because of either a thread exit or an application request.
Re: How to "FormatMessage()" my "WSAGetLastError()"
Quote:
Originally Posted by
2kaud
This is what I used when I did Windows programming:
Code:
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <string>
#include <iostream>
DWORD WinError(std::string& werror, DWORD p_dwError) {
LPSTR MessageBuffer;
werror.clear();
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, p_dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&MessageBuffer, 0, NULL) > 0) {
werror = MessageBuffer;
free(MessageBuffer);
return 0;
}
return GetLastError();
}
int main() {
std::string msg;
if (auto ret{ WinError(msg, 995) }; !ret)
std::cout << msg << '\n';
else
std::cout << "Error - " << ret << '\n';
}
Code:
The I/O operation has been aborted because of either a thread exit or an application request.
Thanks for your answer 2kaud and VictorN, but your solution is for messages retrieved with '::GetLastError()'. Unfortunately it does not work for Messages retrieved with 'WSAGetLastError()' because these messages are stored in a different module than the deafult (here ws2_32.dll").
Best
\Ove
Re: How to "FormatMessage()" my "WSAGetLastError()"
Quote:
Originally Posted by
\Ove
Thanks for your answer 2kaud and VictorN, but your solution is for messages retrieved with '::GetLastError()'. Unfortunately it does not work for Messages retrieved with 'WSAGetLastError()' because these messages are stored in a different module than the deafult (here ws2_32.dll").
Best
\Ove
Well, did you try any of suggestion from these discussions in CG:
Quote:
Originally Posted by
Victor
Re: How to "FormatMessage()" my "WSAGetLastError()"
Quote:
it does not work for Messages retrieved with 'WSAGetLastError()'
Well it seems to! eg WSA error code WSAEDESTADDRREQ (10039) is "Destination required"
if you use 10039 in my code above, it shows:
A required address was omitted from an operation on a socket.
and 10040 (message too long) gives:
A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was
smaller than the datagram itself.
So it seems to work...
What issues are you getting? What wsa error code are you getting that isn't handled properly by FormatMessage() ??
Re: How to "FormatMessage()" my "WSAGetLastError()"
Quote:
Originally Posted by
2kaud
Well it seems to! eg WSA error code WSAEDESTADDRREQ (10039) is "Destination required"
if you use 10039 in my code above, it shows:
A required address was omitted from an operation on a socket.
and 10040 (message too long) gives:
A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was
smaller than the datagram itself.
So it seems to work...
What issues are you getting? What wsa error code are you getting that isn't handled properly by FormatMessage() ??
Hmm. I tried your code again, and now it works! I just restarted the workstation. Could it be VS itself who caused the problem?.
Anyway, the bottom line is: If you have *any* problem - reboot!
Thank you for helping me out.
\Ove