Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2009 11:31
Show Gist options
  • Save anonymous/142753 to your computer and use it in GitHub Desktop.
Save anonymous/142753 to your computer and use it in GitHub Desktop.
#include "OpenFile.h"
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <stdio.h>
#endif
// Function to select the file using dialog.
bool GetFileName(char* pszFilename, bool bIsRead, wchar_t* pszFilter, int iFilterSize)
{
#ifdef WIN32
OPENFILENAME ofn; // The common open filename dialog by Win32
wchar_t szFilename_wchar[MAX_PATH];
wchar_t szFilter_wchar[128];
size_t iConvertedChars;
mbstowcs_s(&iConvertedChars, szFilename_wchar, sizeof(pszFilename)+1, pszFilename, _TRUNCATE);
// Initialization
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = (LPWSTR)szFilename_wchar;
ofn.nMaxFile = 256;
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST;
if(pszFilter)
{
// mbstowcs_s(&iConvertedChars, szFilter_wchar, iFilterSize+1, pszFilter, _TRUNCATE);
ofn.lpstrFilter = (LPCWSTR)pszFilter;
}
else
ofn.lpstrFilter = (LPCWSTR)TEXT("Wavefront OBJ\0*.obj\0PLY\0*.ply\0M\0*.m\0*.smf\0All\0*.*\0");
if(bIsRead)
ofn.lpstrTitle = (LPCWSTR)TEXT("Open File");
else
ofn.lpstrTitle = (LPCWSTR)TEXT("Save File");
// Display the Open dialog box.
if(GetOpenFileName(&ofn)==TRUE)
{
wcstombs_s(&iConvertedChars, pszFilename, wcslen(szFilename_wchar)+1, szFilename_wchar, _TRUNCATE);
return true;
}
else
{
DWORD error_no = CommDlgExtendedError();
error_no = CommDlgExtendedError();
switch(error_no) {
case CDERR_DIALOGFAILURE :
printf("Open Dialog Failed (CDERR_DIALOGFAILURE) :\n The dialog box could not be created. The common dialog box function's call to the DialogBox function failed. For example, this error occurs if the common dialog box call specifies an invalid window handle.");
break;
case CDERR_FINDRESFAILURE :
printf("Open Dialog Failed (CDERR_FINDRESFAILURE) :\n The common dialog box function failed to find a specified resource.");
break;
case CDERR_NOHINSTANCE :
printf("Open Dialog Failed (CDERR_NOHINSTANCE) :\n The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding instance handle.");
break;
case CDERR_INITIALIZATION :
printf("Open Dialog Failed (CDERR_INITIALIZATION) :\n The common dialog box function failed during initialization. This error often occurs when sufficient memory is not available.");
break;
case CDERR_NOHOOK :
printf("Open Dialog Failed (CDERR_NOHOOK) :\n The ENABLEHOOK flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a pointer to a corresponding hook procedure.");
break;
case CDERR_LOCKRESFAILURE :
printf("Open Dialog Failed (CDERR_LOCKRESFAILURE) :\n The common dialog box function failed to lock a specified resource.");
break;
case CDERR_NOTEMPLATE :
printf("Open Dialog Failed (CDERR_NOTEMPLATE) :\n The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding template.");
break;
case CDERR_LOADRESFAILURE :
printf("Open Dialog Failed (CDERR_LOADRESFAILURE) :\n The common dialog box function failed to load a specified resource.");
break;
case CDERR_STRUCTSIZE :
printf("Open Dialog Failed (CDERR_STRUCTSIZE) :\n The lStructSize member of the initialization structure for the corresponding common dialog box is invalid.");
break;
case CDERR_LOADSTRFAILURE :
printf("Open Dialog Failed (CDERR_LOADSTRFAILURE) :\n The common dialog box function failed to load a specified string.");
break;
case FNERR_BUFFERTOOSMALL :
printf("Open Dialog Failed (FNERR_BUFFERTOOSMALL) :\n The buffer pointed to by the lpstrFile member of the OPENFILENAME structure is too small for the file name specified by the user. The first two bytes of the lpstrFile buffer contain an integer value specifying the size, in TCHARs, required to receive the full name.");
break;
case CDERR_MEMALLOCFAILURE :
printf("Open Dialog Failed (CDERR_MEMALLOCFAILURE) :\n The common dialog box function was unable to allocate memory for internal structures.");
break;
case FNERR_INVALIDFILENAME :
printf("Open Dialog Failed (FNERR_INVALIDFILENAME) :\n A file name is invalid.");
break;
case CDERR_MEMLOCKFAILURE :
printf("Open Dialog Failed (CDERR_MEMLOCKFAILURE) :\n The common dialog box function was unable to lock the memory associated with a handle.");
break;
case FNERR_SUBCLASSFAILURE :
printf("Open Dialog Failed (FNERR_SUBCLASSFAILURE) :\n An attempt to subclass a list box failed because sufficient memory was not available.");
break;
}
return false;
}
#else
gets(pszFilename);
return false;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment