Sanmveg saini
Sanmveg saini

Reputation: 750

GetOpenFileName function is not opening the dialog box

so i have this simple code, because i am new to win32 so don't expect me to write very difficult code, however, here is my winProc

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY: PostQuitMessage (0); break;
        case WM_CREATE : make_controls(hwnd); break;
        case WM_COMMAND: handle_commands(hwnd, wParam, lParam); break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
} 

and this is the handle_commands function

void handle_commands(HWND hwnd, WPARAM wp, LPARAM lp){
    if( HIWORD(wp) == BN_CLICKED && LOWORD(wp) == openBtn ){
// openBtn is the only button in the whole application 
        OPENFILENAME ofn;       // common dialog box structure
        char szFile[260];       // buffer for file name
        HWND hwnd;              // owner window
        HANDLE hf;              // file handle

// Initialize OPENFILENAME
        ZeroMemory(&ofn, sizeof(ofn));
        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner = hwnd;
        ofn.lpstrFile = szFile;
// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.
        ofn.lpstrFile[0] = '\0';
        ofn.nMaxFile = sizeof(szFile);
        ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
        ofn.nFilterIndex = 1;
        ofn.lpstrFileTitle = NULL;
        ofn.nMaxFileTitle = 0;
        ofn.lpstrInitialDir = NULL;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box.

        if (GetOpenFileName(&ofn)==TRUE)
            hf = CreateFile(ofn.lpstrFile,
                            GENERIC_READ,
                            0,
                            (LPSECURITY_ATTRIBUTES) NULL,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            (HANDLE) NULL);
    }
}// this is the end of the handle_commands functions

but the problem is that it is not opening any dialog box

as far as i can go, people over internet are successful in opening with the same code.

and yes! i have included commdlg.h and corresponding library

Thanks in advance!

Upvotes: 1

Views: 1825

Answers (1)

Sanmveg saini
Sanmveg saini

Reputation: 750

So the problem is that in the handle_commands, hwnd has been changed. what that mean is that OPENFILENAME structure don't know its correct owner therefore, although clicking the button is firing the correct code it is still not opening the dialog box.

so just comment out the HWND hwnd line in the handle_commands function

Upvotes: 5

Related Questions