CommDlg32: FileOpen Function

The CommDlg32.dll library provides a set of common dialog box functions that allow applications to interact with the user for tasks such as opening files, saving files, and choosing colors. The FileOpen function (often accessed via GetOpenFileName or GetSaveFileName) is a cornerstone for file selection.

Function Signature

BOOL GetOpenFileName(
  LPOPENFILENAME lpofn
);

This function displays the File Open common dialog box. This dialog box allows the user to select files and paths. The function returns TRUE if the user selects a file and the function successfully closes the dialog box, or FALSE if the user cancels the dialog box or an error occurs.

Parameters

Parameter Description
lpofn A pointer to an OPENFILENAME structure that contains information used to initialize the dialog box and to record the user's input.

OPENFILENAME Structure

The OPENFILENAME structure is crucial for configuring and receiving information from the File Open dialog. Key members include:

Common Flags:

Return Value

Nonzero (TRUE): The user selected a file and clicked the OK button. The lpstrFile buffer contains the full path and file name of the selected file.

Zero (FALSE): The user clicked the Cancel button, closed the dialog box, or an error occurred. If CommDlgExtendedError returns a value other than zero, an error occurred.

Example Usage


#include <windows.h>
#include <commdlg.h>
#include <stdio.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    OPENFILENAME ofn;
    char szFileName[MAX_PATH] = "";

    // Initialize OPENFILENAME structure
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = NULL; // Or get the handle of your main window
    ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = sizeof(szFileName);
    ofn.lpstrTitle = "Select a File";
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;

    // Display the File Open dialog box
    if (GetOpenFileName(&ofn)) {
        // User selected a file
        MessageBox(NULL, szFileName, "Selected File", MB_OK);
    } else {
        // User cancelled or an error occurred
        DWORD dwError = CommDlgExtendedError();
        if (dwError) {
            char errorMsg[100];
            sprintf(errorMsg, "Dialog Error: %lu", dwError);
            MessageBox(NULL, errorMsg, "Error", MB_ICONERROR);
        } else {
            MessageBox(NULL, "File selection cancelled.", "Cancelled", MB_OK);
        }
    }

    return 0;
}
            

Remarks

When OFN_ALLOWMULTISELECT is used, the lpstrFile buffer will contain the selected directory followed by the file names. If multiple files are selected, the first file name will be preceded by two null characters, and subsequent file names will be preceded by one null character.

Always check the return value of GetOpenFileName. If it returns FALSE, call CommDlgExtendedError to determine if an error occurred.

See Also