Thread: Allocating extra memory for a dialog

  1. #1
    julie lexx... btq's Avatar
    Join Date
    Jun 2002

    Allocating extra memory for a dialog

    I'm tryin to figure out how to allocate extra memory for a dialog-window,
    such as setting the cbWndExtra-field of the WNDCLASSEX structure.
    I'm creating the dialog using CreateDialog() from a dialog template and it would be nice to be able to set this in the template.
    ..or somewhere else
    Maybe I just should create a structure containing the
    bytes I need and use SetWindowLong() to point to an instance
    of that structure? Any suggestions?

    thanks a lot
    ...viewlexx - julie lexx

  2. #2
    Registered User
    Join Date
    Feb 2003
    In my opinion, allocating a structure and referencing it using SetWindowLong is the more appropriate way of storing data. Be sure to free it when processing the WM_DESTROY or WM_NCDESTROY message.

  3. #3
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    ok, I've been trying to get a hold of the WNDCLASSEX structure of the dialog-box for a day now but it's kinda poorly documented on the web. So, I put together some kind of an article about it.
    I'm posting it here if someone is having the same problem =)

    ======================================the start
    Ok, this is how to get a hold of the WNDCLASSEX structure when
    createing a dialogbox.
    This assumes visual studio 7 but may work on others too (haven't tested).
    First off, create a dialogbox-template in the resource editor. Then
    view the properties of the first node in the directory-tree:

    +myProject.rc <-- this one

    and set the 'MFC mode' to false.
    This allows us to specify a 'Class Name' in the dialog editor properties. If
    'MFC mode' is true this field is dissabled.
    For the sake of the example code listed below we set this field to 'myDialogClass' (without the ' ' )
    NOTE: you can also do this by editing the .rc file as text and typing ' CLASS "myDialogClass" ' at the right place .

    Now do whatever you want with the dialogbox(add buttons etc) and
    now we're set to code this baby.
    Normally when creating a dialog we just use CreateDialog (or one of the other 'Create dialog'-functions)
    like this:

    int err;
    HWND hDlg = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DiALOG1),hWnd,ptfnDialogProc);
    err = GetLastError();
    with the changes we've made this GetLastError() will now return the #1407 which means "Can't find window class",
    or something similar (we here assume that our dialog id is IDD_DIALOG1) .
    This is,off course, because we added a 'Class Name' in the dialog-template, which is supposed
    to be a 'window class'-name. As you may know, CreateDialog(..) (and the other functions) calls CreateWindowEx
    to create the dialog, and now the CreateWindowEx is trying to create a window built on a window-class not
    yet registered. What we need to do is to register this window-class using RegisterClassEx.
    We do this by first calling the function GetClassInfoEx(..) to fill up a WNDCLASSEX structure with the standard dialog info.
    Then we alter this structure as we want and set the szClassName-field to the class-name specified in the resource template.
    So the code would look a little something like this:
    	HWND hDlg=NULL;
    	WNDCLASSEX wnd;
    	ATOM aTom;	
    	wnd.hInstance=hInstance; //the handle to the instance
    	wnd.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ID_ICON));
    	wnd.lpszClassName="myDialogClass";  //we set the Class Name in the template to this, remember ?
                   set other members if you wish
    	aTom = RegisterClassEx(&wnd); //aTom equals 0 if function fails
    	if(hDlg==NULL && aTom!=0)
    		hDlg = CreateDialog(hInstExe,MAKEINTRESOURCE(IDD_DIALOG1),hWnd,MainDialogProc);
    		//some error code here..
    Now, CreateDialog(..) calls CreateWindowEx(..) and since we've registered our window-class it finds it
    and creates the dialog based on our WNDCLASSEX structure.
    A little thing about the "#32770" argument in the GetClassInfoEx call: this is the window-class that
    CreateDialog(..) (and again the other dialog-functions) would use if the class name wan't specified.
    I'll bet there are excepions but I don't know of any =)
    Well, that was that..

    =================== the end

    this "article" probably has lotsa misses and errors in it but it's something anyway
    Hope this will help someone.


    oh, and if you find any errors or something that I've not understood please tell me. I'd really appriciate it .
    ...viewlexx - julie lexx

  4. #4
    Registered User
    Join Date
    Feb 2003
    Windows reserves 4 bytes of space for every window (not just dialog boxes) that allows you to use them in any manner. The offset is GWL_USERDATA.

    You can put a pointer to a structure and retrieve it easily within the dialog procedure:

    // example 
    // example 
    or, if you're programming for 64-bit Windows:

    // example 
    // example 

  5. #5
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    actually..if you put pointers or handles in there you should call Get/SetWindowLongPtr(..) all the time..
    but what did this have to do with that ?

    ...viewlexx - julie lexx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help -- allocating memory in a multidimensional array
    By jonathan.plumb in forum C Programming
    Replies: 10
    Last Post: 05-20-2009, 11:04 PM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. make Child Dialog not Popup?
    By Zeusbwr in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2005, 02:42 PM
  4. Getting the position of a dialog without parent on the screen
    By stormbringer in forum Windows Programming
    Replies: 1
    Last Post: 08-27-2003, 02:59 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM