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. Getting the Display Name of a file Extension

Getting the Display Name of a file Extension

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
4 Posts 3 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.
  • D Offline
    D Offline
    Diarrhio
    wrote on last edited by
    #1

    Does anyone know how to get the Display Name (e.g. "Microsoft Word Document") from a given extension (e.g. "doc"). I can't use ShGetFileInfo, because that requires a file you want information for. I won't have a file, just an extention. Thanks!

    S D 2 Replies Last reply
    0
    • D Diarrhio

      Does anyone know how to get the Display Name (e.g. "Microsoft Word Document") from a given extension (e.g. "doc"). I can't use ShGetFileInfo, because that requires a file you want information for. I won't have a file, just an extention. Thanks!

      S Offline
      S Offline
      Shog9 0
      wrote on last edited by
      #2

      Diarrhio wrote: I can't use ShGetFileInfo Sure you can - just use the SHGFI_USEFILEATTRIBUTES flag & the filename can be bogus.

      - Shog9 -

      I'd show a smile but I'm too weak I'd share with you, could I only speak

      D 1 Reply Last reply
      0
      • S Shog9 0

        Diarrhio wrote: I can't use ShGetFileInfo Sure you can - just use the SHGFI_USEFILEATTRIBUTES flag & the filename can be bogus.

        - Shog9 -

        I'd show a smile but I'm too weak I'd share with you, could I only speak

        D Offline
        D Offline
        Diarrhio
        wrote on last edited by
        #3

        Thanks, Shog.

        1 Reply Last reply
        0
        • D Diarrhio

          Does anyone know how to get the Display Name (e.g. "Microsoft Word Document") from a given extension (e.g. "doc"). I can't use ShGetFileInfo, because that requires a file you want information for. I won't have a file, just an extention. Thanks!

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

          Did you not read all of the documentation? It states: If the uFlags parameter includes the SHGFI_USEFILEATTRIBUTES flag, this parameter does not have to be a valid file name. The function proceeds as if the file exists with the specified name and with the file attributes passed in the dwFileAttributes parameter. This enables you to obtain information about a file type by passing just the extension for pszPath and passing FILE_ATTRIBUTE_NORMAL in dwFileAttributes. That implies that this will work:

          SHFILEINFO  fi;
          CString     strExtension;
          
          
          m\_ebExtension.GetWindowText(strExtension);
          
          if (SHGetFileInfo(strExtension, FILE\_ATTRIBUTE\_NORMAL, &fi, sizeof(fi), SHGFI\_USEFILEATTRIBUTES | SHGFI\_TYPENAME) != 0)
          {      
              m\_staticAssocApp.SetWindowText(fi.szTypeName);
          }
          

          If you still do not want to use SHGetFileInfo(), try this. It's from the top of my head so you may have to tweak it:

          HKEY        hKey;
          CString     strExtension;
          TCHAR       szValue\[128\];
          LONG        lSize;
          
          
          m\_ebExtension.GetWindowText(strExtension);
          
          if (RegOpenKey(HKEY\_CLASSES\_ROOT, strExtension, &hKey) == ERROR\_SUCCESS)
          {
              lSize = sizeof(szValue);
          
              RegQueryValue(hKey, \_T(""), szValue, &lSize);
              
              RegCloseKey(hKey);
          
              if (RegOpenKey(HKEY\_CLASSES\_ROOT, szValue, &hKey) == ERROR\_SUCCESS)
              {
                  lSize = sizeof(szValue);
          
                  RegQueryValue(hKey, \_T(""), szValue, &lSize);
          
                  RegCloseKey(hKey);
              
                  m\_staticAssocApp.SetWindowText(szValue);
              }
          }
          
          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