c++ - GetWindowLongPtr sets Error to "Class already Exists" -
in windows api wrapper, can choose have message box come when there's error. have 1 can't pin down though.
here's main function:
int main() { window win; //create default window default class (name changes each new instance) return messageloop(); //the familiar getmessage() while loop, returns msg.wparam }
this works fine, when close window (just tested via x button), following message (this when copy message box):
--------------------------- error --------------------------- file: "g:\programming\v2\wwbasewindow.h" function: _fakewndproc line: 61 error code: 1410 error: class exists. --------------------------- ok ---------------------------
now it's crystal clear error coming from, not why. here's _fakewndproc
function. whole wrap (function, args)
syntax checks getlasterror()
after function called. why don't see error checking.
lresult callback basewindow::_fakewndproc (msgfillparams) //trick procedure (taken someone's gui wrapper guide) { basewindow * destinationwindowptr = 0; //for window message goes //problem in following line (gets pointer window, set when it's created) destinationwindowptr = (basewindow *)wrap (getwindowlongptr, hwnd, gwlp_userdata); if (msg == wm_command && lparam != 0) //if control message, set destination window destinationwindowptr = (basewindow *)wrap (getwindowlongptr, (hwin)lparam, gwlp_userdata); if (destinationwindowptr) //check if pointer 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 wondering why call (trying create class?) aside, tried checking error codes time wm_close
message comes along. output code before line, , after. comes up:
before: 0 after: 0 --->before: 0 --->before: 1410 after: 1410 before: 1410 after: 1410 ...
this puts topping on confusion, implies function calls sendmessage
somewhere inside. why wouldn't same others?
the error doesn't make of difference, program ends right after, don't want hanging around. how can deal it?
note: tried not calling postquitmessage (0);
when wm_destroy
came up, , created 2 windows. both of them gave same error when closing, it's not end of program anyways.
also, each 1 gave error 1400 (invalid window handle) too, when didn't call postquitmessage
. error originated call defwindowproc
in windows' respective window procedures. ideas on 1 either?
edit:
due request, here code 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 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 , create message box return result; // pass return value } // version if return value 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 , __wwerror.set()
work though. other functions wrapped give appropriate message boxes.
your calls getlasterror incorrect. cannot indiscriminately call getlasterror that. should call when api call documentation says valid so. if api call reports failure.
the calls defwindowproc fine illustration of how can go wrong. documentation defwindowproc not make mention of way function report failure. , makes no mention of calling getlasterror. calls getlasterror should not made , returning undefined, meaningless values.
since there no single common mechanism win32 function report failure, attempt wrap win32 api calls single common error handling routine doomed failure.
what need treat each api call on own merits, , write error checking appropriate api call. since using c++ recommend make use of exceptions here. write function, throwlastwin32error say, call whenever api function reports failure. implementation of throwlastwin32error call getlasterror , call formatmessage obtain textual description before throwing suitably descriptive exception. use this:
if (!callsomewin32function()) throwlastwin32error();
but main point need case-by-case checking of function success since different win32 functions report failure in different ways.
Comments
Post a Comment