WinAPI Wrapper for GetOpenFileName

Shell32.dll provides an undocumented API that wraps around a call to GetOpenFileName called GetFileNameFromBrowse we’ll have to get the call pointer using GetProcAddress, quite easy to use! 😉 Well I can see the documentation in MSDN but can’t find the prototype in any of the header file in my SDK folder, so till then GetProcAddress is our refuge. As per MSDN prototype can be found in shlobj.h…

[sourcecode language=’cpp’]int _tmain()
{
    // Creates a typedef
    typedef BOOL ( WINAPI *GetFileNameFromBrowse )( HWND hwnd,
                                                    LPWSTR pszFilePath,
                                                    UINT cchFilePath,
                                                    LPCWSTR pszWorkingDir,
                                                    LPCWSTR pszDefExt,
                                                    LPCWSTR pszFilters,
                                                    LPCWSTR szTitle );

    // Path buffer, specifies starting directory and on successful return
    // from open dialog also holds the selected file name
    wchar_t wszPath[MAX_PATH] = L”D:\”;

    // Load shell32 dll
    HMODULE hModule = LoadLibrary( “Shell32.dll” );
if( !hModule )
{
return 1;
}

    // Get procedure address
    GetFileNameFromBrowse GetFileNameFromBrowsePtr = ( GetFileNameFromBrowse )GetProcAddress( hModule, “GetFileNameFromBrowse” );

    // Show browse dialog
    if( GetFileNameFromBrowsePtr && GetFileNameFromBrowsePtr( 0, wszPath, MAX_PATH, 0, 0, L”*.*”, L”Nibu Open” ))
    {
       MessageBoxW( 0, wszPath, L”You selected”, MB_OK | MB_ICONINFORMATION );
    }

    // Free loaded library
    FreeLibrary( hModule );

return 0;

}// End _tmain[/sourcecode]

Appreciate your comments...