FileName from File Handle
-
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.
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
-
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
>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. -
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.
#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); }
-
>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.:doh: ............... Got it that's Great :-D
"I Think this Will Help"
visit me at http://www.thisisalok.tk
-
#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); }
-
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.