Soren, I somehow missed this message when you sent it and found it only today. Indeed I do have my own equivalents to these. Please forgive me if I note that there are at least two bugs in your code. I will point them out if you wish.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Soren, I somehow missed this message when you sent it and found it only
today. Indeed I do have my own equivalents to these. Please forgive me if I
note that there are at least two bugs in your code. I will point them out
if you wish.
If PrintError is called with a NULL second argument then the Message Box it displays will be empty. And I was wrong about the other error I thought I saw before; I read one statement incorrectly. Sorry.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If PrintError is called with a NULL second argument then the Message Box
it displays will be empty. And I was wrong about the other error I thought
I saw before; I read one statement incorrectly. Sorry.
If PrintError is called with a NULL second argument then the Message Box
it displays will be empty. And I was wrong about the other error I thought
I saw before; I read one statement incorrectly. Sorry.
I have these two declaration in stafx.h:
void PrintError(LPTSTR lpszFunction);
void PrintError(HRESULT hr, LPTSTR lpszFunction = NULL);
I haven't really debugged no 2. But no 1 is invaluable. I'm sure you have
your versions,Pete.
stdafx.cpp:
void PrintError(HRESULT hr, LPTSTR lpszFunction = NULL)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
if (lpszFunction)
{
lpDisplayBuf = (LPVOID)LocalAlloc(
LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) *
sizeof(TCHAR)
);
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, hr, lpMsgBuf);
}
else
{
lpDisplayBuf = (LPVOID)LocalAlloc(
LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) * sizeof(TCHAR)));
}
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK |
MB_ICONERROR);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
void PrintError(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(
LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) *
sizeof(TCHAR)
);
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK |
MB_ICONERROR);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
// ExitProcess(dw);
}
Soren, I somehow missed this message when you sent it and found it only today. Indeed I do have my own equivalents to these. Please forgive me if I note that there are at least two bugs in your code. I will point them out if you wish.
Please do. I've been lazy though. I've more or less copied them from the
net.
Regards
On Monday, November 12, 2018, Pete Maclean petemaclean@users.sourceforge.net wrote:
--
Søren Bro Thygesen
If PrintError is called with a NULL second argument then the Message Box it displays will be empty. And I was wrong about the other error I thought I saw before; I read one statement incorrectly. Sorry.
That's a little funny., 'cause that argument defaults to NULL and I never
provide it in the code. My error is still displayed.
Regards.
On Tue, Nov 13, 2018 at 4:35 PM Pete Maclean petemaclean@users.sourceforge.net wrote:
In reality I only use no 1.
So, I might as well remove the second. Or give it some more attention...
Regards.
On Tue, Nov 13, 2018 at 5:17 PM Soren Bro sbrothy@users.sourceforge.net
wrote: