Reputation: 61910
In my Windows API wrapper, I can choose to have a message box come up when there's an error. I have one that I can't really pin down though.
Here's my main function:
int main()
{
Window win; //create default window with default class (name changes each new instance)
return messageLoop(); //the familiar GetMessage() while loop, returns msg.wParam
}
This all works fine, but when I close my window (just tested via X button), I get the following message (this is what I get when I copy the message box):
---------------------------
Error
---------------------------
File: "G:\programming\v2\wwbasewindow.h"
Function: _fakeWndProc
Line: 61
Error Code: 1410
Error: Class already exists.
---------------------------
OK
---------------------------
Now it's crystal clear where this error is coming from, but not exactly why. Here's the _fakeWndProc
function. The whole wrap (function, args)
syntax checks GetLastError()
after that function is called. This is why you don't see error checking.
LRESULT CALLBACK BaseWindow::_fakeWndProc (msgfillparams) //trick procedure (taken from someone's gui wrapper guide)
{
BaseWindow * destinationWindowPtr = 0; //for which window message goes to
//PROBLEM IN THE FOLLOWING LINE (gets a pointer to the window, set when it's created)
destinationWindowPtr = (BaseWindow *)wrap (GetWindowLongPtr, hwnd, GWLP_USERDATA);
if (msg == WM_COMMAND && lParam != 0) //if control message, set destination to that window
destinationWindowPtr = (BaseWindow *)wrap (GetWindowLongPtr, (hwin)lParam, GWLP_USERDATA);
if (destinationWindowPtr) //check if pointer is valid
return destinationWindowPtr->_WndProc (hwnd, msg, wParam, lParam); //call window's procedure
else
return wrap (DefWindowProc, hwnd, msg, wParam, lParam); //call default procedure
}
I'm just wondering why this call is (trying to create a class?) That aside, I tried checking the error codes from the time a WM_CLOSE
message comes along. I output the code before the line, and after. This is what comes up:
Before: 0
After: 0
--->Before: 0
--->Before: 1410
After: 1410
Before: 1410
After: 1410
...
This puts the topping on my confusion, as this implies that the function calls SendMessage
somewhere inside. But why wouldn't it do the same for any others?
The error itself doesn't make much of a difference, as the program ends right after, but I don't want it hanging around. How can I deal with it?
Note:
I just tried not calling PostQuitMessage (0);
when WM_DESTROY
came up, and created 2 windows. Both of them gave the same error when closing, so it's not necessarily the end of the program anyways.
Also, each one gave an error 1400 (Invalid window handle) too, but only when I didn't call PostQuitMessage
. This error originated from the call to DefWindowProc
in those windows' respective window procedures. Any ideas on that one either?
Edit:
Due to request, here is the code for wrap
:
// pass along useful error information (useless constants within error function)
#define wrap(...) Wrap (__FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
// cstr == char *
// con == const
// sdword == int (signed dword)
// version if return value of API function is not void
template<typename TRet, typename... TArgs>
typename std::enable_if<!std::is_void<TRet>::value, TRet>::type
Wrap(con cstr file, const char * const func, con sdword line, TRet(*WINAPI api)(TArgs...), TArgs... args)
{
TRet result = api(std::forward<TArgs>(args)...); //call API function
if (GetLastError()) __wwError.set (GetLastError(), file, func, line); //set variables and create message box
return result; // pass back return value
}
// version if return value is void
template<typename... TArgs>
void Wrap(con cstr file, const char * const func, con sdword line, void(*WINAPI api)(TArgs...), TArgs... args)
{
api(std::forward<TArgs>(args)...);
if (GetLastError()) __wwError.set (GetLastError(), file, func, line);
}
I'm 100% sure this and __wwError.set()
work though. All other functions wrapped with this give appropriate message boxes.
Upvotes: 1
Views: 563
Reputation: 612914
Your calls to GetLastError are simply incorrect. You cannot indiscriminately call GetLastError like that. You should only call it when the API call documentation says that it is valid to do so. Usually this will be if the API call reports failure.
The calls to DefWindowProc are a fine illustration of how this can go wrong. The documentation for DefWindowProc does not make any mention of a way for the function to report failure. And it makes no mention of calling GetLastError. Thus your calls to GetLastError should not be made and are returning undefined, meaningless values.
Since there is no single common mechanism for a Win32 function to report failure, your attempt to wrap all Win32 API calls with a single common error handling routine is doomed to failure.
What you need to do is to treat each API call on its own merits, and write error checking appropriate for that API call. Since you are using C++ I would recommend you make use of exceptions here. Write a function, ThrowLastWin32Error say, that you call whenever an API function reports failure. The implementation of ThrowLastWin32Error would call GetLastError and then call FormatMessage to obtain a textual description before throwing a suitably descriptive exception. You would use it like this:
if (!CallSomeWin32Function())
ThrowLastWin32Error();
But the main point is that you do need case-by-case checking of function success since different Win32 functions report failure in different ways.
Upvotes: 7