Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to create a browse file dialog which can select files longer then MAX_PATH

How to create a browse file dialog which can select files longer then MAX_PATH

Scheduled Pinned Locked Moved C / C++ / MFC
c++comhelptutorialquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Axter
    wrote on last edited by
    #1

    I need a browse dialog that can select files that have file paths longer then _MAX_PATH(260). I've tried using GetOpenFileName, but I get a "File name is invalid" error when I select a file with a name longer then 260 (_MAX_PATH) characters. Does anyone have a method or know of an existing library/project that can display a browse dialog which can select file names longer then 260 characters?

    Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

    K M D 4 Replies Last reply
    0
    • A Axter

      I need a browse dialog that can select files that have file paths longer then _MAX_PATH(260). I've tried using GetOpenFileName, but I get a "File name is invalid" error when I select a file with a name longer then 260 (_MAX_PATH) characters. Does anyone have a method or know of an existing library/project that can display a browse dialog which can select file names longer then 260 characters?

      Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

      K Offline
      K Offline
      KevinXLi
      wrote on last edited by
      #2

      Windows not support files longer than 259.

      TCHAR lpstrFilename[MAX_PATH]="";
      OPENFILENAME ofn;
      CString pathname;
      CString filename;
      int nTerm=0;
      char stemp[255]={0};
      TCHAR *p;
      memset(&ofn,0,sizeof(OPENFILENAME));
      ofn.lStructSize=sizeof(OPENFILENAME);
      ofn.lpstrFile=lpstrFilename;
      ofn.hwndOwner=m_hWnd;
      ofn.nMaxFile=MAX_PATH*2;
      ofn.lpstrFilter="All files(*.*)\0*.*\0";
      if(WINVER >= 0x0400)
      ofn.Flags=OFN_ALLOWMULTISELECT|OFN_EXPLORER|OFN_LONGNAMES|OFN_FILEMUSTEXIST;
      else ofn.Flags=OFN_FILEMUSTEXIST;
      ofn.lpstrTitle="GetOpenFileName";
      ofn.nFilterIndex=0;
      if(GetOpenFileName(&ofn))
      {
      pathname=lpstrFilename;
      if(pathname.Right(1)!="\\")pathname+="\\";
      for(int i=0;;i++)
      {
      if(lpstrFilename[i]==0)
      {
      nTerm++;
      if(lpstrFilename[i+1]==0)
      {
      if(nTerm==1)
      m_FileList.AddString(lpstrFilename);
      break;
      }
      else
      {
      p=&(lpstrFilename[i+1]);
      strcpy(stemp,p);
      m_FileList.AddString(pathname+stemp);
      }
      }
      }
      }

      A 1 Reply Last reply
      0
      • A Axter

        I need a browse dialog that can select files that have file paths longer then _MAX_PATH(260). I've tried using GetOpenFileName, but I get a "File name is invalid" error when I select a file with a name longer then 260 (_MAX_PATH) characters. Does anyone have a method or know of an existing library/project that can display a browse dialog which can select file names longer then 260 characters?

        Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        As KevinXli pointed out, make sure you pass large enough buffers and set the nMaxFilexxx members of the OPENFILENAME struct accordingly. Mark

        Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

        A 1 Reply Last reply
        0
        • K KevinXLi

          Windows not support files longer than 259.

          TCHAR lpstrFilename[MAX_PATH]="";
          OPENFILENAME ofn;
          CString pathname;
          CString filename;
          int nTerm=0;
          char stemp[255]={0};
          TCHAR *p;
          memset(&ofn,0,sizeof(OPENFILENAME));
          ofn.lStructSize=sizeof(OPENFILENAME);
          ofn.lpstrFile=lpstrFilename;
          ofn.hwndOwner=m_hWnd;
          ofn.nMaxFile=MAX_PATH*2;
          ofn.lpstrFilter="All files(*.*)\0*.*\0";
          if(WINVER >= 0x0400)
          ofn.Flags=OFN_ALLOWMULTISELECT|OFN_EXPLORER|OFN_LONGNAMES|OFN_FILEMUSTEXIST;
          else ofn.Flags=OFN_FILEMUSTEXIST;
          ofn.lpstrTitle="GetOpenFileName";
          ofn.nFilterIndex=0;
          if(GetOpenFileName(&ofn))
          {
          pathname=lpstrFilename;
          if(pathname.Right(1)!="\\")pathname+="\\";
          for(int i=0;;i++)
          {
          if(lpstrFilename[i]==0)
          {
          nTerm++;
          if(lpstrFilename[i+1]==0)
          {
          if(nTerm==1)
          m_FileList.AddString(lpstrFilename);
          break;
          }
          else
          {
          p=&(lpstrFilename[i+1]);
          strcpy(stemp,p);
          m_FileList.AddString(pathname+stemp);
          }
          }
          }
          }

          A Offline
          A Offline
          Axter
          wrote on last edited by
          #4

          Windows does support files longer then 259 characters, but you have to use a different syntax. You have to use prefix "\\?\" with the file name, and use CreateFile. For more information, look at CreateFile in MSDN. I've already tried using GetOpenFileName, and I gave it required buffer size which was greater then MAX_PATH, but it still failed.

          Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

          1 Reply Last reply
          0
          • M Mark Salsbery

            As KevinXli pointed out, make sure you pass large enough buffers and set the nMaxFilexxx members of the OPENFILENAME struct accordingly. Mark

            Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

            A Offline
            A Offline
            Axter
            wrote on last edited by
            #5

            I've already tried using GetOpenFileName, and I gave it required buffer size which was greater then MAX_PATH, but it still failed. Here's example code: void TestLongFileBrowse() { TCHAR acPath[2048] = _T("\0"); OPENFILENAME ofn; ZeroMemory(&ofn,sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.nMaxFile = 2048; ofn.lpstrFile = acPath; if (GetOpenFileName(&ofn)) { printf("Path length: %d\n", _tcslen(ofn.lpstrFile)); printf("Path: %S\n", ofn.lpstrFile); } else printf("CommDlgExtendedError(): %d\n"); }

            Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

            M 1 Reply Last reply
            0
            • A Axter

              I've already tried using GetOpenFileName, and I gave it required buffer size which was greater then MAX_PATH, but it still failed. Here's example code: void TestLongFileBrowse() { TCHAR acPath[2048] = _T("\0"); OPENFILENAME ofn; ZeroMemory(&ofn,sizeof(OPENFILENAME)); ofn.lStructSize = sizeof(OPENFILENAME); ofn.nMaxFile = 2048; ofn.lpstrFile = acPath; if (GetOpenFileName(&ofn)) { printf("Path length: %d\n", _tcslen(ofn.lpstrFile)); printf("Path: %S\n", ofn.lpstrFile); } else printf("CommDlgExtendedError(): %d\n"); }

              Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              Are you using the Unicode version of GetOpenFileName()? Only the Unicode APIs support full paths longer than MAX_PATH

              --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

              A 1 Reply Last reply
              0
              • M Michael Dunn

                Are you using the Unicode version of GetOpenFileName()? Only the Unicode APIs support full paths longer than MAX_PATH

                --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

                A Offline
                A Offline
                Axter
                wrote on last edited by
                #7

                I've tried it with both UNICODE and ANSI.

                Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

                1 Reply Last reply
                0
                • A Axter

                  I need a browse dialog that can select files that have file paths longer then _MAX_PATH(260). I've tried using GetOpenFileName, but I get a "File name is invalid" error when I select a file with a name longer then 260 (_MAX_PATH) characters. Does anyone have a method or know of an existing library/project that can display a browse dialog which can select file names longer then 260 characters?

                  Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  Axter wrote:

                  http://www.experts-exchange.com/Cplusplus

                  Non-existent page.


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  • A Axter

                    I need a browse dialog that can select files that have file paths longer then _MAX_PATH(260). I've tried using GetOpenFileName, but I get a "File name is invalid" error when I select a file with a name longer then 260 (_MAX_PATH) characters. Does anyone have a method or know of an existing library/project that can display a browse dialog which can select file names longer then 260 characters?

                    Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Axter wrote:

                    I've tried using GetOpenFileName, but I get a "File name is invalid" error...

                    Which is different than FNERR_BUFFERTOOSMALL.


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups