|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Apr 2005
Messaggi: 296
|
[WIN32] Ottenere un nome file da un handle
Ho provato l'esempio di MSDN,preso da qui
http://msdn.microsoft.com/library/en...ile_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 Codice:
#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; } |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: May 2005
Messaggi: 564
|
Compilato ed eseguito, non ho rilevato errori...
Uso il compilatore LCC Win32. Ciao. |
![]() |
![]() |
![]() |
#3 |
Member
Iscritto dal: Apr 2005
Messaggi: 296
|
Ok,anch'io l'ho eseguito,l'ho compilato con Visual C++,ma non è un errore di compilazione quello che dico io,è un errore a run time,è solo il valore di ritorno di GetMappedFileName() che è 0 per dire che la funzione è fallita
poi ho chiamato GetLastError() per vedere qual'è il problema e ho ottenuto una stringa di errore tramite FormatMessage() e infine dovrebbe venire visualizzata con MessageBox()...a te non è uscito un MessageBox con scritta una stringa di errore del tipo "Accesso a zona memoria non valida" ? e poi seguito da un MessageBox vuoto? |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: May 2005
Messaggi: 564
|
No, intendevo che il programma funziona correttamente, esce un messagebox con il nome dell'handle dell'eseguibile da testare, in questo caso explorer.exe.
Non conosco Visual C++, ma prova a controllare le impostazioni di compilazione. Ciao. |
![]() |
![]() |
![]() |
#5 | |
Member
Iscritto dal: Apr 2005
Messaggi: 296
|
Quote:
ma aspetta,tu forse hai usato #include <psapi.h> e la libreria psapi.lib,non hai caricato la dll psapi.dll a mano con LoadLibrary e ottenuto l'indirizzo della funzione GetMappedFilename con GetProcAddress beh proverò a cercare l'header e la libreria statica e poi usare quelli,ma non credo sia quella la causa,comunque dimmi se li hai usati o no |
|
![]() |
![]() |
![]() |
#6 | |
Member
Iscritto dal: Apr 2005
Messaggi: 296
|
Quote:
era semplicemente il mio psapi.dll che non funzionava,perchè ho semplicemente sostituito il mio con un altro che ho trovato in rete,e ho lasciato il codice sopra così com'è,e funzia...boh io ho Windows XP senza service pack,avreste un'ipotesi di quale fosse la causa,giusto per curiosità? ![]() |
|
![]() |
![]() |
![]() |
#7 | |
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
Quote:
|
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 04:29.