#include <windows.h>
#include <stdio.h>
#include <io.h>
#define FFH_FILE_MAPPING_INVAILD 1000
#define FFH_GET_FILENAME_FAIL 1001
#define FFH_FIND_VOLUME 1002
typedef DWORD (WINAPI *pGetMappingFileNameW)(HANDLE,LPVOID,LPWSTR,DWORD);
DWORD WINAPI GetMappedFileNameW( HANDLE hProcess,
LPVOID lpv,
LPWSTR lpFilename,
DWORD nSize)
{
HMODULE hMod = LoadLibraryA("PSAPI.DLL");
DWORD dwReturn;
dwReturn = ((pGetMappingFileNameW)GetProcAddress(hMod,"GetMappedFileNameW"))
(hProcess,lpv,lpFilename,nSize);
FreeLibrary(hMod);
return dwReturn;
}
DWORD WINAPI GetFileNameFromHandleW(HANDLE hFile,LPWSTR lpFileName,SIZE_T nSize){
HANDLE hMapFile;
LPVOID hMem;
LPWSTR lpString;
HANDLE hVolume;
WCHAR wcVolName[MAX_PATH+1];
WCHAR wcDrivers[MAX_PATH+1];
LPWSTR lpFullFileName;
DWORD dwReturn;
BOOL bResult;
DWORD i;
hMapFile = CreateFileMappingW(hFile,NULL,PAGE_READONLY,0,1,NULL);
if ( !hMapFile )
return FFH_FILE_MAPPING_INVAILD;
hMem = MapViewOfFileEx(hMapFile,FILE_MAP_READ,0,0,1,NULL);
lpString = (LPWSTR)HeapAlloc( GetProcessHeap(),
HEAP_ZERO_MEMORY,
(nSize+1)*sizeof(WCHAR) );
dwReturn = GetMappedFileNameW(GetCurrentProcess(),hMem,lpString,nSize);
UnmapViewOfFile(hMem);
CloseHandle(hMapFile);
if ( !dwReturn ){
HeapFree(GetProcessHeap(),0,lpString);
return FFH_GET_FILENAME_FAIL;
}
bResult = TRUE;
ZeroMemory(wcVolName,(MAX_PATH+1)*sizeof(WCHAR));
hVolume = FindFirstVolumeW(wcVolName,MAX_PATH);
if ( hVolume == INVALID_HANDLE_VALUE ){
HeapFree(GetProcessHeap(),0,lpString);
return FFH_FIND_VOLUME;
}
do{
ZeroMemory(wcDrivers,(MAX_PATH+1)*sizeof(WCHAR));
GetVolumePathNamesForVolumeNameW(wcVolName,wcDrivers,MAX_PATH,&dwReturn);
ZeroMemory(wcVolName,(MAX_PATH+1)*sizeof(WCHAR));
for(i=0;i<dwReturn;i++)
if ( wcDrivers[i] == L'\\' )
wcDrivers[i] = 0;
if ( QueryDosDeviceW(wcDrivers,wcVolName,MAX_PATH) ){
if ( !wcsncmp(lpString,wcVolName,wcslen(wcVolName)) ){
lpFullFileName = (LPWSTR)HeapAlloc( GetProcessHeap(),
HEAP_ZERO_MEMORY,
(wcslen(wcDrivers)*wcslen(lpString)+1)*sizeof(WCHAR));
wsprintfW(lpFullFileName,L"%s%s",wcDrivers,&lpString[wcslen(wcVolName)]);
wcsncpy(lpFileName,lpFullFileName,nSize);
HeapFree(GetProcessHeap(),0,lpFullFileName);
bResult = FALSE;
break;
}
}
ZeroMemory(wcVolName,(MAX_PATH+1)*sizeof(WCHAR));
}while(FindNextVolumeW(hVolume,wcVolName,MAX_PATH));
FindVolumeClose(hVolume);
HeapFree(GetProcessHeap(),0,lpString);
return (DWORD)bResult;
}
DWORD WINAPI GetFileNameFromFileStructW( FILE *fp,
LPWSTR lpFileName,
SIZE_T nSize)
{
// fp->_file == _fileno(fp)
return GetFileNameFromHandleW((HANDLE)_get_osfhandle(fp->_file),lpFileName,nSize);
}
LPWSTR WINAPI GetFileNameSplitW(LPWSTR lpString){
DWORD dwString = wcslen(lpString);
LPWSTR _lpString = lpString;
lpString = lpString + dwString;
while(dwString){
if ( *lpString == L'\\' )
break;
dwString--;
lpString--;
}
if ( *lpString == L'\\' ){
return (lpString+1);
}
return _lpString;
}
int main(void){
FILE *fp;
WCHAR Path[MAX_PATH+1];
fp = fopen("1.jpg","rb");
if ( !fp )
return 0;
GetFileNameFromFileStructW(fp,Path,MAX_PATH);
fclose(fp);
wprintf(L"%s\n%s\n",Path,GetFileNameSplitW(Path));
system("pause");
return 0;
}
|