openFileDialog and string



  • std::string tool_system::System::openFileDialog(std::string filter)
    {
        char FileName[260] = { 0 };
        char FilePath[260] = { 0 };
    
        OPENFILENAME ofn = { 0 };
    
        ofn.lStructSize = sizeof(ofn);
        ofn.lpstrFile = FilePath;
        ofn.nMaxFile = sizeof(FilePath);
        ofn.lpstrFilter = filter.c_str();
        ofn.nFilterIndex = 1;
        ofn.lpstrFileTitle = FileName;
        ofn.nMaxFileTitle = MAX_PATH;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
    
        if (!GetOpenFileNameA(&ofn))
            return "";
    
        return ofn.lpstrFile;
    }
    

    Why can't you put "lpstrFilter" in a string? The result of a man like this would be a parameter, it's a waste, but I've tried to clean his memory. And it works so well:

    std::string tool_system::System::openFileDialog(const char* filter)
    

    {
    char FileName[260] = { 0 };
    char FilePath[260] = { 0 };

    OPENFILENAME ofn = { 0 };
    
    ofn.lStructSize = sizeof(ofn);
    ofn.lpstrFile = FilePath;
    ofn.nMaxFile = sizeof(FilePath);
    ofn.lpstrFilter = filter;
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = FileName;
    ofn.nMaxFileTitle = MAX_PATH;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
    
    if (!GetOpenFileNameA(&ofn))
        return "";
    
    return ofn.lpstrFile;
    

    }

    Why can't you use the line?



  • You probably don't initiate the line properly.
    If openFileDialog Acceptance std::stringYou can't call him.

     openFileDialog("All Files\0*.*\0\0")
    

    The filter shall contain symbols \0e.g. "All Files\0*.*\0\0"If you write

    std::string filter = "All Files\0*.*\0\0";
    

    That's it. filter Only symbols until the first \0I mean, "All Files"

    You can add a sablon overload. openFileDialogwhich would work with hard literature:

    template<int N>
    std::string openFileDialog(const char(&filter)[N]) {
      std::string filter_str(filter, filter + N);
      return openFileDialog(filter_str);
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2