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. why can not I get a long filename through GetModuleFileName( )?

why can not I get a long filename through GetModuleFileName( )?

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingc++questionlearning
5 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.
  • E Offline
    E Offline
    ewighell
    wrote on last edited by
    #1

    I only get a 8.3 format filename through following code, and I want to get a long filename, how?

    HINSTANCE hInst = AfxGetInstanceHandle();
    if(FAILED(!hInst)) return E_FAIL;
    CString strDllFileName;
    DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
    TRACE(strDllFileName);

    by debugging, I find that the value of strDllFileName is F:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL, and the corresponding longname is F:\hu\DD\DetailDesign\Debug\DetailDesign.dll Note: I am programming in VC++ 6.0, my Operating System is Windows XP, the Disk F: ,where the .dll file is located, is NTFS format. Thank you very much!!! ------------------- I am learning C++ and English

    D T N J 4 Replies Last reply
    0
    • E ewighell

      I only get a 8.3 format filename through following code, and I want to get a long filename, how?

      HINSTANCE hInst = AfxGetInstanceHandle();
      if(FAILED(!hInst)) return E_FAIL;
      CString strDllFileName;
      DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
      TRACE(strDllFileName);

      by debugging, I find that the value of strDllFileName is F:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL, and the corresponding longname is F:\hu\DD\DetailDesign\Debug\DetailDesign.dll Note: I am programming in VC++ 6.0, my Operating System is Windows XP, the Disk F: ,where the .dll file is located, is NTFS format. Thank you very much!!! ------------------- I am learning C++ and English

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

      ewighell wrote:

      ...I want to get a long filename, how?

      Have you considered GetLongPathName()?


      "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

      "There is no death, only a change of worlds." - Native American Proverb

      1 Reply Last reply
      0
      • E ewighell

        I only get a 8.3 format filename through following code, and I want to get a long filename, how?

        HINSTANCE hInst = AfxGetInstanceHandle();
        if(FAILED(!hInst)) return E_FAIL;
        CString strDllFileName;
        DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
        TRACE(strDllFileName);

        by debugging, I find that the value of strDllFileName is F:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL, and the corresponding longname is F:\hu\DD\DetailDesign\Debug\DetailDesign.dll Note: I am programming in VC++ 6.0, my Operating System is Windows XP, the Disk F: ,where the .dll file is located, is NTFS format. Thank you very much!!! ------------------- I am learning C++ and English

        T Offline
        T Offline
        Thierry Maurel
        wrote on last edited by
        #3

        Hello you can try this function which transform the path you get from GetModuleFileName... typedef DWORD (__stdcall *GETLONGPATHNAME)(LPCTSTR, LPTSTR, DWORD); ///////////////////////////////////////////////////////////////////////////// // DWORD MyGetLongPathNameEx (CString& sPath) { // Result DWORD dwRet = 0; // Load the KERNEL32 library HINSTANCE hdl = ::LoadLibrary ("KERNEL32.DLL"); if (hdl == NULL) return dwRet; // Get the function we will use (Not Unicode version) GETLONGPATHNAME pfnGetLongPathName = (GETLONGPATHNAME) GetProcAddress (hdl, "GetLongPathNameA"); if (pfnGetLongPathName == NULL) { ::FreeLibrary (hdl); return dwRet; } // Call the GetLongPathName function to converts the specified path to its long form. dwRet = (pfnGetLongPathName) (sPath, sPath.GetBuffer (_MAX_PATH), _MAX_PATH); sPath.ReleaseBuffer (); // Unload library ::FreeLibrary (hdl); return dwRet; } CString strDllFileName; DWORD Status = ::GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName.GetBuffer (_MAX_PATH), _MAX_PATH); strDllFileName.ReleaseBuffer (); DWORD dwRet = ::MyGetLongPathNameEx (strDllFileName); TRACE(strDllFileName);

        1 Reply Last reply
        0
        • E ewighell

          I only get a 8.3 format filename through following code, and I want to get a long filename, how?

          HINSTANCE hInst = AfxGetInstanceHandle();
          if(FAILED(!hInst)) return E_FAIL;
          CString strDllFileName;
          DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
          TRACE(strDllFileName);

          by debugging, I find that the value of strDllFileName is F:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL, and the corresponding longname is F:\hu\DD\DetailDesign\Debug\DetailDesign.dll Note: I am programming in VC++ 6.0, my Operating System is Windows XP, the Disk F: ,where the .dll file is located, is NTFS format. Thank you very much!!! ------------------- I am learning C++ and English

          N Offline
          N Offline
          Naveen
          wrote on last edited by
          #4

          use GetLongPathName after u have got the short file name. I would like to comment that the statement DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH); is wrong. u should never cast CString like (LPTSTR)(LPCTSTR)strDllFileName. U may have unexpected results because of this. Use as follows DWORD Status = GetModuleFileName (hInst, strDllFileName.GetBuffer( MAX_PATH ), MAX_PATH); strDllFileName.ReleaseBuffer( -1 ); nave

          1 Reply Last reply
          0
          • E ewighell

            I only get a 8.3 format filename through following code, and I want to get a long filename, how?

            HINSTANCE hInst = AfxGetInstanceHandle();
            if(FAILED(!hInst)) return E_FAIL;
            CString strDllFileName;
            DWORD Status = GetModuleFileName (hInst, (LPTSTR)(LPCTSTR)strDllFileName, MAX_PATH);
            TRACE(strDllFileName);

            by debugging, I find that the value of strDllFileName is F:\hu\DD\DETAIL~1\Debug\DETAIL~1.DLL, and the corresponding longname is F:\hu\DD\DetailDesign\Debug\DetailDesign.dll Note: I am programming in VC++ 6.0, my Operating System is Windows XP, the Disk F: ,where the .dll file is located, is NTFS format. Thank you very much!!! ------------------- I am learning C++ and English

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            Let me try to actually answer the subject of ou post...    That function dates back to 16-bit Windows, and my guess is that since only FAT-style 8.3 filenames were supported, Win32 inherited that behavior.    Also, note that the GetModuleFileName(...) function is one of the few that is capable of returning filepaths longer than MAX_PATH, so do not assume that MAX_PATH is always going to be long enough...    Peace! -=- James


            If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            DeleteFXPFiles & CheckFavorites (Please rate this post!)

            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