GordonFreeman
09-09-2005, 21:05
Ho provato l'esempio di MSDN,preso da qui
http://msdn.microsoft.com/library/en-us/fileio/fs/obtaining_a_file_name_from_a_file_handle.asp
ma GetMappedFileName() fallisce,
GetLastError() ritorna 998 che corrisponde a "Accesso a posizione di memoria non valido"
perchè? eppure il puntatore di memoria tornato da MapViewOfFile() != NULL
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <tchar.h>
#define BUFSIZE 512
const char szFile[] = "c:\\windows\\explorer.exe";
BOOL GetFileNameFromHandle(HANDLE hFile,LPSTR pszFilename)
{
typedef DWORD (WINAPI * GetMappedFileNameFunc)(HANDLE,LPVOID,LPSTR,DWORD);
GetMappedFileNameFunc GetMappedFileName = (GetMappedFileNameFunc)GetProcAddress(LoadLibrary("psapi.dll"),"GetMappedFileNameA");
BOOL bSuccess = FALSE;
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
}
}
else // GetMappedFileName() == 0
{
LPSTR lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the string.
MessageBox( NULL, lpMsgBuf, "GetLastError() per GetMappedFileName()", MB_OK|MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
bSuccess = TRUE;
UnmapViewOfFile(pMem);
}
CloseHandle(hFileMap);
}
printf("File name is %s\n", pszFilename);
return(bSuccess);
}
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
HANDLE hFile = CreateFile(szFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
char szBuf[MAX_PATH + 1];
if(hFile != INVALID_HANDLE_VALUE && GetFileNameFromHandle(hFile,szBuf))
MessageBox(NULL,szBuf,"",MB_OK);
CloseHandle(hFile);
return 0;
}
http://msdn.microsoft.com/library/en-us/fileio/fs/obtaining_a_file_name_from_a_file_handle.asp
ma GetMappedFileName() fallisce,
GetLastError() ritorna 998 che corrisponde a "Accesso a posizione di memoria non valido"
perchè? eppure il puntatore di memoria tornato da MapViewOfFile() != NULL
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <tchar.h>
#define BUFSIZE 512
const char szFile[] = "c:\\windows\\explorer.exe";
BOOL GetFileNameFromHandle(HANDLE hFile,LPSTR pszFilename)
{
typedef DWORD (WINAPI * GetMappedFileNameFunc)(HANDLE,LPVOID,LPSTR,DWORD);
GetMappedFileNameFunc GetMappedFileName = (GetMappedFileNameFunc)GetProcAddress(LoadLibrary("psapi.dll"),"GetMappedFileNameA");
BOOL bSuccess = FALSE;
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
}
}
else // GetMappedFileName() == 0
{
LPSTR lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the string.
MessageBox( NULL, lpMsgBuf, "GetLastError() per GetMappedFileName()", MB_OK|MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
bSuccess = TRUE;
UnmapViewOfFile(pMem);
}
CloseHandle(hFileMap);
}
printf("File name is %s\n", pszFilename);
return(bSuccess);
}
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
HANDLE hFile = CreateFile(szFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
char szBuf[MAX_PATH + 1];
if(hFile != INVALID_HANDLE_VALUE && GetFileNameFromHandle(hFile,szBuf))
MessageBox(NULL,szBuf,"",MB_OK);
CloseHandle(hFile);
return 0;
}