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. FileName from File Handle

FileName from File Handle

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 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.
  • J Offline
    J Offline
    Jijo Raj
    wrote on last edited by
    #1

    Dear all, I had a file handle. How can i get the File name using the file handle. Please help... Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present.

    T 2 2 Replies Last reply
    0
    • J Jijo Raj

      Dear all, I had a file handle. How can i get the File name using the file handle. Please help... Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present.

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      From where are you getting the File Handle (i.e. which api or function). anyway you can use this function to get that

      WINAPI DWORD GetModuleFileName(
      HMODULE hModule,
      LPWSTR lpFilename,
      DWORD nSize);


      "I Think this Will Help"

      visit me at http://www.thisisalok.tk
      2 1 Reply Last reply
      0
      • T ThatsAlok

        From where are you getting the File Handle (i.e. which api or function). anyway you can use this function to get that

        WINAPI DWORD GetModuleFileName(
        HMODULE hModule,
        LPWSTR lpFilename,
        DWORD nSize);


        "I Think this Will Help"

        visit me at http://www.thisisalok.tk
        2 Offline
        2 Offline
        224917
        wrote on last edited by
        #3

        >Alok wrote "From where are you getting the File Handle (i.e. which api or function). ?" Difficult to know, his function may have only one parameter ie, file handle. this is called from all over the program ;)


        suhredayan
        There is no spoon.

        T 1 Reply Last reply
        0
        • J Jijo Raj

          Dear all, I had a file handle. How can i get the File name using the file handle. Please help... Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present.

          2 Offline
          2 Offline
          224917
          wrote on last edited by
          #4

          #include #include #include #include #include #define BUFSIZE 512 BOOL GetFileNameFromHandle(HANDLE hFile) { BOOL bSuccess = FALSE; TCHAR pszFilename[MAX_PATH+1]; HANDLE hFileMap; // Get the file size. DWORD dwFileSizeHi = 0; DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi); if( dwFileSizeLo == 0 && dwFileSizeHi == 0 ) { printf("Cannot map a file with a length of zero.\n"); return FALSE; } // Create a file mapping object. hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 1, NULL); if (hFileMap) { // Create a file mapping to get the file name. void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1); if (pMem) { if (GetMappedFileName (GetCurrentProcess(), pMem, pszFilename, MAX_PATH)) { // Translate path with device name to drive letters. TCHAR szTemp[BUFSIZE]; szTemp[0] = '\0'; if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) { TCHAR szName[MAX_PATH]; TCHAR szDrive[3] = TEXT(" :"); BOOL bFound = FALSE; TCHAR* p = szTemp; do { // Copy the drive letter to the template string *szDrive = *p; // Look up each device name if (QueryDosDevice(szDrive, szName, BUFSIZE)) { UINT uNameLen = _tcslen(szName); if (uNameLen < MAX_PATH) { bFound = _tcsnicmp(pszFilename, szName, uNameLen) == 0; if (bFound) { // Reconstruct pszFilename using szTemp // Replace device path with DOS path TCHAR szTempFile[MAX_PATH]; _stprintf(szTempFile, TEXT("%s%s"), szDrive, pszFilename+uNameLen); _tcsncpy(pszFilename, szTempFile, MAX_PATH); } } } // Go to the next NULL character. while (*p++); } while (!bFound && *p); // end of string } } bSuccess = TRUE; UnmapViewOfFile(pMem); }

          J 1 Reply Last reply
          0
          • 2 224917

            >Alok wrote "From where are you getting the File Handle (i.e. which api or function). ?" Difficult to know, his function may have only one parameter ie, file handle. this is called from all over the program ;)


            suhredayan
            There is no spoon.

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            :doh: ............... Got it that's Great :-D


            "I Think this Will Help"

            visit me at http://www.thisisalok.tk
            1 Reply Last reply
            0
            • 2 224917

              #include #include #include #include #include #define BUFSIZE 512 BOOL GetFileNameFromHandle(HANDLE hFile) { BOOL bSuccess = FALSE; TCHAR pszFilename[MAX_PATH+1]; HANDLE hFileMap; // Get the file size. DWORD dwFileSizeHi = 0; DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi); if( dwFileSizeLo == 0 && dwFileSizeHi == 0 ) { printf("Cannot map a file with a length of zero.\n"); return FALSE; } // Create a file mapping object. hFileMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 1, NULL); if (hFileMap) { // Create a file mapping to get the file name. void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1); if (pMem) { if (GetMappedFileName (GetCurrentProcess(), pMem, pszFilename, MAX_PATH)) { // Translate path with device name to drive letters. TCHAR szTemp[BUFSIZE]; szTemp[0] = '\0'; if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) { TCHAR szName[MAX_PATH]; TCHAR szDrive[3] = TEXT(" :"); BOOL bFound = FALSE; TCHAR* p = szTemp; do { // Copy the drive letter to the template string *szDrive = *p; // Look up each device name if (QueryDosDevice(szDrive, szName, BUFSIZE)) { UINT uNameLen = _tcslen(szName); if (uNameLen < MAX_PATH) { bFound = _tcsnicmp(pszFilename, szName, uNameLen) == 0; if (bFound) { // Reconstruct pszFilename using szTemp // Replace device path with DOS path TCHAR szTempFile[MAX_PATH]; _stprintf(szTempFile, TEXT("%s%s"), szDrive, pszFilename+uNameLen); _tcsncpy(pszFilename, szTempFile, MAX_PATH); } } } // Go to the next NULL character. while (*p++); } while (!bFound && *p); // end of string } } bSuccess = TRUE; UnmapViewOfFile(pMem); }

              J Offline
              J Offline
              Jijo Raj
              wrote on last edited by
              #6

              Dear Suhredayan, This is what i actually need. Thanks for your great help. Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present.

              2 1 Reply Last reply
              0
              • J Jijo Raj

                Dear Suhredayan, This is what i actually need. Thanks for your great help. Regards, Jijo. ________________________________ Yesterday is history, Tomorrow is a mystery, But today is a present.

                2 Offline
                2 Offline
                224917
                wrote on last edited by
                #7

                ne time ;)


                suhredayan
                There is no spoon.

                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