View Single Post
Old Mar 10th, 2006, 8:11 AM   #1
Chesso
Newbie
 
Join Date: Mar 2006
Posts: 7
Rep Power: 0 Chesso is on a distinguished road
Problem with GetOpenFileName buffer too small.

I have written a DLL for a Delphi application I am making and decided to have the Open/Save dialog
functionality within an external DLL instead of inside the application.

The problem is I have to hard code the buffer which makes the DLL much larger then it should be.

I have read on MSDN about this problem but it really does not help as I am not too proficient with C++ in
general as I only use it when I need it. I have found plenty of article about this issue but they only explain
how to solve it using MFC with VS. I am however using Dev-CPP enviroment.

Here is the function source:

std::string OpenSaveDialog(int Open, char* InitDir)
{
 OPENFILENAME ofn;
 char szFileName[21000] = "";
 string FData("");
    
 ZeroMemory(&ofn, sizeof(OPENFILENAME));
 ofn.lStructSize = sizeof(OPENFILENAME);
 ofn.hwndOwner = NULL;
 ofn.lpstrFile = szFileName;
 ofn.nMaxFile = 21000;
 
 switch(Open)
 {
  case 0: break;
  case 1:
    ofn.lpstrFilter = "All Supported Files\0*.mp3;*.mp2;*.wav;*.wma;*.mid;*.ogg\0";
    ofn.lpstrInitialDir = InitDir;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT;
    
    if (GetOpenFileName(&ofn))
    {
     int len = strlen(ofn.lpstrFile);
     
     if( ofn.lpstrFile[len + 1] == 0) { return ofn.lpstrFile; } // If single file was selected.
     else {
     // This is the directory.
      FData = ofn.lpstrFile;
     
      ofn.lpstrFile += len + 1;
      while(ofn.lpstrFile[0]) {
     
     // This will contain the current file name from multiple selection.
      FData = FData + "|" + ofn.lpstrFile;      
      
      // Find next name
      len = strlen(ofn.lpstrFile);
      ofn.lpstrFile += len + 1;
      } 
       return FData;                        
     }
    }
    else { return "CANCEL"; }
  break;
  case 2: 
    ofn.lpstrFilter = "All Supported Files\0*.sm3l;*.sm3;*.m3u;*.pls\0";
    ofn.lpstrInitialDir = InitDir;
    ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = "sm3l";
    
    if (GetOpenFileName(&ofn)) { return ofn.lpstrFile; }
    else { return "CANCEL"; }
  break;  
  case 3:
    ofn.lpstrFilter = "SM3 format\0*.sm3\0M3U Format\0*.m3u\0PLS Format\0*.pls\0";
    ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
    ofn.lpstrDefExt = "sm3l";
    
    if (GetSaveFileName(&ofn)) { return ofn.lpstrFile; }
    else { return "CANCEL"; }                                   
 }
}

Does anyone know how to solve this issue? From what I read on MSDN I need some sort of callback function to resize the buffer if necessary and read something about calling it twice.....
Chesso is offline   Reply With Quote