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.
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.
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
StructureThe OPENFILENAME
structure is crucial for configuring and receiving information from the File Open dialog. Key members include:
lStructSize
: The size of the structure in bytes.hwndOwner
: A handle to the owner window of the dialog box.hInstance
: Handle to the instance of the application that owns the dialog box.lpstrFilter
: A pointer to a string that specifies the file types that can be selected. The string consists of pairs of null-terminated strings. The last pair must be terminated by two null characters (\0\0
).lpstrFile
: A pointer to a buffer that contains the name of the file selected by the user. This buffer must be large enough to hold the null-terminated file name.nMaxFile
: The size, in characters, of the buffer pointed to by lpstrFile
.lpstrTitle
: A pointer to a null-terminated string that is placed in the title bar of the dialog box. If this member is NULL
, the title bar contains the default title.Flags
: A set of bit flags that specify various options for the dialog box.Flags
:OFN_EXPLORER
: Use the new Explorer-style dialog box.OFN_FILEMUSTEXIST
: The user must type a file name that exists.OFN_PATHMUSTEXIST
: The user must type a path that exists.OFN_HIDEREADONLY
: Hide the read-only check box.OFN_ALLOWMULTISELECT
: Allows the user to select multiple files.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.
#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;
}
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.