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

  • Home
  • SEARCH
  • 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 6177059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:12:26+00:00 2026-05-24T00:12:26+00:00

I’m wondering how to provide context for dialog procedures? Up to now, I declare

  • 0

I’m wondering how to provide context for dialog procedures?

Up to now, I declare static or global variables to provide whatever context I need knowing that the dialog procedure can only be active one time. However, I find this pretty ugly and wonder if I can pass the context to the dialog procedure through the DialogBoxParam/WM_INITDIALOG/LPARAM mechanism saving it in the INITDIALOG handler using SetWindowLog/GWL_USERDATA?

A few experiments show this might work but I notice that the dialog procedure gets at least one message (namely 30h) before it gets the INITDIALOG message.

So. I’m left with a dilemma. One of the following occurs to me…

  • return FALSE to any message received while GetWindowLong/GWL_USERDATA
    is zero. But do I know its initial value will be zero?

  • return FALSE to message 30h. But are there any other message sent b4
    WM_INITDIALOG?

  • Start in a procedure which returns FALSE until it gets a WM_INITDIALOG, then change the dialog procedure using SetWindowLong/GWL_WNDPROC.

I’m also concerned about what other system calls or other uSoft software (stuff I find in the MSDN) might do with GWL_USERDATA.

  • 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-24T00:12:26+00:00Added an answer on May 24, 2026 at 12:12 am

    There are actually quite a few ways to store context for your window procedures, and dialogs are simply a special case of windows, so these techniques apply.

    One way, as you mentioned, is to use GWLP_USERDATA/GetWindowLongPtr()/SetWindowLongPtr() to store a pointer to the dialog context. Note that the functions are prefixed with Ptr(). These functions will work with 32-bit and 64-bit code. The “non-Ptr()” functions only work with 32-bit code, so they should not be used.

    According to the documentation for GetWindowLongPtr(), the GWLP_USERDATA value is initially set to zero. So you can always rely on checking if GetWindowLongPtr() returns zero and returning FALSE from the dialog procedure in those situations.

    A dialog procedure using these functions might look something like this:

    // MyDialogProc is a static function in MyDialogClass. Can also be a global function.
    INT_PTR CALLBACK MyDialogClass::MyDialogProc(HWND hwndDlg, UINT uMsg,
        WPARAM wParam, LPARAM lParam)
    {
        MyDialogClass* target = 0;
        if(uMsg == WM_INITDIALOG)
        {
            target = reinterpret_cast<MyDialogClass*>(lParam);
            ::SetWindowLongPtr(hwndDlg, GWLP_USERDATA,
                reinterpret_cast<LONG_PTR>(target));
        }
        target = reinterpret_cast<MyDialogClass*>(
            ::GetWindowLongPtr(hwndDlg, GWLP_USERDATA));
        if(target != 0)
        {
            // Something like this
            return target->ProcessMessage(hwndDlg, uMsg, wParam, lParam);
        }
        return FALSE;
    }
    

    And your dialog creation code passes the pointer to an instance of MyDialogClass using a member function, possibly like this:

    void MyDialogClass::Create()
    {
        // ....
        // Use CreateDialogParam() or friends to create a dialog and pass
        // the context pointer.
        HWND h = ::CreateDialogParam(hInstance, lpTemplateName, hWndParent,
            &MyDialogClass::MyDialogProc, reinterpret_cast<LPARAM>(this));
        // ....
    }
    

    If you prefer a different method (because you’re worried that someone else will write over GWLP_USERDATA), you can use GetProp()/SetProp()/RemoveProp() and come up with a unique name to identify the pointer. GetProp() returns zero if it can’t find the property, so again you can rely on checking if it returns zero. This is the method that I use in my library/framework.

    A dialog procedure that uses the property functions may look something like this:

    const wchar_t* myDialogClassContextPtrName = L"MyDlgClsCxtPtr"; // Unicode
    // const char* myDialogClassContextPtrName = "MyDlgClsCxtPtr";  // ANSI
    
    // MyDialogProc is a static function in MyDialogClass. Can also be a global function.
    INT_PTR CALLBACK MyDialogClass::MyDialogProc(HWND hwndDlg, UINT uMsg,
        WPARAM wParam, LPARAM lParam)
    {
        MyDialogClass* target = 0;
        if(uMsg == WM_INITDIALOG)
        {
            target = reinterpret_cast<MyDialogClass*>(lParam);
            ::SetProp(hwndDlg, myDialogClassContextPtrName,
                reinterpret_cast<HANDLE>(target));
        }
        target = reinterpret_cast<MyDialogClass*>(
            ::GetProp(hwndDlg, myDialogClassContextPtrName));
        if(target != 0)
        {
            // Something like this
            INT_PTR returnValue = target->ProcessMessage(hwndDlg, uMsg, wParam, lParam);
            if(uMsg == WM_NCDESTROY)
            {
                ::RemoveProp(hwndDlg, myDialogClassContextPtrName);
            }
            return returnValue;
        }
        return FALSE;
    }
    

    As you have experienced, there are messages that are sent before the WM_INITDIALOG message (or the WM_NCCREATE message for non-dialog windows). In my experience these messages are inconsequential and will not affect the functionality of your procedure in any way. You can return FALSE for the messages that you receive before WM_INITDIALOG.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

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.