GetOpenFileName Function

Creates an Open dialog box that lets the user specify the drive, directory, and the name of a file or set of files to open.

Syntax

BOOL GetOpenFileName(      
    LPOPENFILENAME lpofn );

Parameters

lpofn
[in, out] Pointer to an OPENFILENAME structure that contains information used to initialize the dialog box. When GetOpenFileName returns, this structure contains information about the user's file selection.

Return Value

If the user specifies a file name and clicks the OK button, the return value is nonzero. The buffer pointed to by the lpstrFile member of the OPENFILENAME structure contains the full path and file name specified by the user.

If the user cancels or closes the Open dialog box or an error occurs, the return value is zero. To get extended error information, call the CommDlgExtendedError function, which can return one of the following values.

CDERR_DIALOGFAILURE
CDERR_FINDRESFAILURE
CDERR_NOHINSTANCE
CDERR_INITIALIZATION
CDERR_NOHOOK
CDERR_LOCKRESFAILURE
CDERR_NOTEMPLATE
CDERR_LOADRESFAILURE
CDERR_STRUCTSIZE
CDERR_LOADSTRFAILURE
FNERR_BUFFERTOOSMALL
CDERR_MEMALLOCFAILURE
FNERR_INVALIDFILENAME
CDERR_MEMLOCKFAILURE
FNERR_SUBCLASSFAILURE

Remarks

Note  Starting with Windows Vista, the Common File Dialog has been superseded by the Common Item Dialog when used to open a file. We recommended that you use the Common Item Dialog API instead of the Common File Dialog API in this case. For more information, please see Common Item Dialog.

The Explorer-style Open dialog box provides user-interface features that are similar to the Windows Explorer. You can provide an OFNHookProc hook procedure for an Explorer-style Open dialog box. To enable the hook procedure, set the OFN_EXPLORER and OFN_ENABLEHOOK flags in the Flags member of the OPENFILENAME structure and specify the address of the hook procedure in the lpfnHook member.

Windows continues to support the old-style Open dialog box for applications that want to maintain a user-interface consistent with the old-style user-interface. To display the old-style Open dialog box, enable an OFNHookProcOldStyle hook procedure and ensure that the OFN_EXPLORER flag is not set.

To display a dialog box that allows the user to select a directory instead of a file, call the SHBrowseForFolder function.

Note, when selecting multiple files, the total character limit for the file names depends on the operating system and the version of the function.

  • Windows 95/98/Me: (only ANSI is supported) no restriction
  • Microsoft Windows NT4 and earlier: 32k limit
  • Windows 2000: (ANSI) 32k limit, (Unicode) no restriction

Windows NT 4.0: The OPENFILENAME structure includes additional members on more recent versions of Windows. However, this causes problems for applications on previous versions of Windows. To use the current header files for applications that must run on Windows NT 4.0, either use the #define "/D_WIN32_WINNT=0x0400" or use OPENFILENAME_SIZE_VERSION_400 for the lStructSize member of OPENFILENAME.

Windows 95/98/Me: GetOpenFileNameW is supported by the Microsoft Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Example

For an example, see Opening a File.

Function Information

Minimum DLL Versioncomdlg32.dll
HeaderDeclared in Commdlg.h, include Windows.h
Import libraryComdlg32.lib
Minimum operating systems Windows 95, Windows NT 3.1
UnicodeImplemented as ANSI and Unicode versions.

See Also

Tags :


Community Content

peepeeprogrammer
Windows 2000/XP: (ANSI) 32k limit, (Unicode) no restriction
The restriction of 32k appears to exist also in Unicode XP, despite what the documentation says.
Tags : contentbug

William Sproule - MSFT
Current Working Directory is altered when a file is opened
When the user selects at least one file and clicks the OK button, the process' current working directory is changed to the directory contain the file(s) being opened.

Page view tracker