Parent of a BrowseForFolder dialog box

This is a discussion on Parent of a BrowseForFolder dialog box within the Windows Programming forums, part of the Platform Specific Boards category; I am having problems obtaining the owner (parent) of my BrowseForFolder dialog box from within my dialog box callback function. ...

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Parent of a BrowseForFolder dialog box

    I am having problems obtaining the owner (parent) of my BrowseForFolder dialog box from within my dialog box callback function. The callback function's hwnd parameter is the handle of the BrowseForFolder dialog box, so I tried using GetParent(hwnd) to obtain the parent of the dialog box but it does not seem to be working.
    I made sure to set the hwndOwner member of the BROWSEINFO struct:
    Code:
    BROWSEINFO bi;
    ZeroMemory(&bi, sizeof(bi));
    bi.hwndOwner = hwnd;
    bi.pidlRoot = NULL;
    bi.pszDisplayName = displayName;
    ...
    setting its owner to the current window.

    Here is my simple callback function:
    Code:
    int CALLBACK
    BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
    {
        switch(uMsg)
        {
            case BFFM_INITIALIZED:
            {
                HWND hGamePathEdit = GetDlgItem(GetParent(hwnd), IDC_GAMEPATHEDIT);
                char path[SendMessage(hGamePathEdit, WM_GETTEXTLENGTH, 0, 0) + 1];
    
                SendMessage(hGamePathEdit, WM_GETTEXT, (WPARAM)sizeof(path), (LPARAM)path);
                SendMessage(hwnd, BFFM_SETSELECTION, (WPARAM)TRUE, (LPARAM)path);
    
                break;
            }
        }
        return 0;
    }
    I believed that calling GetParent on hwnd would give me the handle for the parent window in which I had called SHBrowseForFolder (the window containing the IDC_GAMEPATHEDIT control I am trying to send a message to); however it did not work at all. To rule out any other conflicts, I passed the parent window handle to the callback function through the lpData param instead and used (HWND)lpData in place GetParent(hwnd), and that worked perfectly. Also, I have used GetParent() successfully in many other dialog boxes.
    So I guess my question is, why does calling GetParent from within the BrowseForFolder dialog box callback function not return me the handle to the window that initiated the dialog box? What am I doing wrong here??

  2. #2
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    Code:
    BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
    Did you check if hwnd has a valid handle?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Have you checked GetLastError to see if it really returns NULL and doesn't fail?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I know the handle to the dialog box (hwnd) is valid, because this part of the code:
    Code:
    SendMessage(hwnd, BFFM_SETSELECTION, (WPARAM)TRUE, (LPARAM)path);
    worked as expected. So I'm pretty sure the GetParent function is failing somewhere when passed a BFF dialog box handle.

    Here's my test for the error code:
    Code:
    char t[100];
    if(GetParent(hwnd) == NULL){
    sprintf(t, "%d", GetLastError());
    MessageBox(hwnd, t,"",0); }
    This displays "0", meaning GetParent successfully returned NULL!?... meaning the window is a top-level unowned window... which makes no sense at all given there is a BROWSEINFO member "hwndOwner".
    What is going on here!?... Is it that a BFF dialog box cannot be owned and is a top-level window (in which case, why bother with an hwndOwner member?) or is GetParent failing silently?

    [edit]
    Well, I found a temporary (maybe permament) workaround for this, using GetWindow(), with the flag GW_OWNER.
    This is completely unintuitive (and I tried it out of sheer desparity) because it clearly states on MSDN that "... the GetParent function to retrieve the owner window handle to a dialog box. The function returns the owner window handle to dialog boxes, and the parent window handle to child windows."
    [rant]
    So any sane person would assume that GetParent is the function to use (and nowhere in GetParent, does it make any mention of using GetWindow instead). And between their constant moving of material to msdn2, this is just driving me insane. One documentation will say to use function X, another one will say function X is depricated and use function Y, and then function Y will have example code in which it uses function X... etc etc. Many documentations on msdn2 are lacking in information (such as a complete listing of input flags etc) compared to their msdn counterparts (which are now harder to find thanks to redirecting to msdn2). I've said it before, and I'll say it again... using MSDN is like shopping for specialty items at a department store: the stuff you're looking for is always in the LEAST likely place you'd expect it be. grrr.
    [/rant]

    Thanks for you're help guys, if anyone finds out why GetParent isnt working how MSDN says it should, please let me know, otherwise, spread the word about using GetWindow instead.
    Last edited by @nthony; 01-07-2007 at 09:06 PM.

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    Yeah, I try with GetParent is null...just because MSDN says:
    Remarks

    Note that, despite its name, this function can return an owner window instead of a parent window. To obtain the parent window and not the owner, use GetAncestor with the GA_PARENT flag.
    Seems that GetWindow() is much much better
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 01:59 PM
  2. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM
  3. 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
  4. Dialog Box & Property Sheet :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 01:33 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21