Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7628871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:41:10+00:00 2026-05-31T05:41:10+00:00

In my Windows API wrapper, I can choose to have a message box come

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:41:12+00:00Added an answer on May 31, 2026 at 5:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there Windows API functions that allows reading what the current password policy is?
Can some one specify the windows API, one need to use in order to
I am writing a GUI wrapper for windows api right now ( i can't
I am writing a Windows Api wrapper and I have run into a problem.
Using the Windows API, how can I get a list of domains on my
Is there in Windows API or in MFC any analog to atoh() function? atoh()
Is there a Windows API or any way to determine on which physical processor/core
I am trying to use native windows API with Qt using mingw toolset. There
I'm working on a object-oriented Windows API wrapper library, written in C++, and I
I am working on Windows. I have to see certain set of API's for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.