Sei sulla pagina 1di 3

Obtaining a File Name From a File Handle (Windows)

Home

Library

Learn

Downloads

Support

Sign in | United States - English | Preferences

MSDN Library Windows Development System Services Memory Management Using the Memory Management Function Using File Mapping Obtaining a File Name From a File H Community Content
GetFinalPathNameByHandle Vista+ or Server 2008+ applicati...

Obtaining a File Name From a File Handle


The following example obtains a file name from a handle to a file object using a file mapping object. It uses the CreateFileMapping and MapViewOfFile functions to create the mapping. Next, it uses the GetMappedFileName function to obtain the file name. For remote files, it prints the device path received from this function. For local files, it converts the path to use a drive letter and prints this path. To test this code, create a main function that opens a file using CreateFile and passes the resulting handle to GetFileNameFromHandle. Copy
#include #include #include #include #include #include <windows.h> <stdio.h> <tchar.h> <string.h> <psapi.h> <strsafe.h>

More...

#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, MAX_PATH)) { UINT uNameLen = _tcslen(szName); if (uNameLen < MAX_PATH) { bFound = _tcsnicmp(pszFilename, szName, uNa meLen) == 0; if (bFound && *(pszFilename + uNameLen) ==

http://msdn.microsoft.com/en-us/library/aa366789%28VS.85%29.aspx[8/22/2010 1:25:18 AM]

Obtaining a File Name From a File Handle (Windows)


_T('\\')) { // Reconstruct pszFilename using szTempFi le // Replace device path with DOS path TCHAR szTempFile[MAX_PATH]; StringCchPrintf(szTempFile, MAX_PATH, TEXT("%s%s"), szDrive, pszFilename+uNameLen); StringCchCopyN(pszFilename, MAX_PATH+1, s zTempFile, _tcslen(szTempFile)); } } } // Go to the next NULL character. while (*p++); } while (!bFound && *p); // end of string } } bSuccess = TRUE; UnmapViewOfFile(pMem); } CloseHandle(hFileMap); } _tprintf(TEXT("File name is %s\n"), pszFilename); return(bSuccess); } int _tmain(int argc, TCHAR *argv[]) { HANDLE hFile; if( argc != 2 ) { printf("This sample takes a file name as a paramete r.\n"); return 0; } hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_REA D, NULL, OPEN_EXISTING, 0, NULL); if(hFile == INVALID_HANDLE_VALUE) { printf("CreateFile failed with %d\n", GetLastError() ); return 0; } GetFileNameFromHandle( hFile ); }

Send comments about this topic to Microsoft Build date: 7/1/2010

Community Content
GetFinalPathNameByHandle

Add

FAQ

Vista+ or Server 2008+ applications can call GetFinalPathNameByHandle() instead. History 4/7/2008 Josh Poley [MSFT]

http://msdn.microsoft.com/en-us/library/aa366789%28VS.85%29.aspx[8/22/2010 1:25:18 AM]

Obtaining a File Name From a File Handle (Windows)

2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Feedback

http://msdn.microsoft.com/en-us/library/aa366789%28VS.85%29.aspx[8/22/2010 1:25:18 AM]

Potrebbero piacerti anche