Obtaining a filename from a file handle (Windows 2000+)
Posted: Mon Apr 27, 2009 1:22 am
Tested with PB 4.30(x86) on XP Pro SP2.
Code: Select all
; Obtaining a filename from a file handle.
Structure FILE_NAME_INFORMATION
FileNameLength.l
FileName.s{1024}
EndStructure
Structure IO_STATUS_BLOCK
StructureUnion
Status.i
Pointer.i
EndStructureUnion
Information.i
EndStructure
Prototype GetMappedFileName(hProcess, lpv, *lpFilename, nSize.l)
Global GetMappedFileName.GetMappedFileName
If OSVersion() < #PB_OS_Windows_2000
End
EndIf
If OpenLibrary(0, "Psapi.dll")
CompilerIf #PB_Compiler_Unicode
GetMappedFileName = GetFunction(0, "GetMappedFileNameW")
CompilerElse
GetMappedFileName = GetFunction(0, "GetMappedFileNameA")
CompilerEndIf
EndIf
#FileNameInformation = 9
#ObjectNameInformation = 1
#STATUS_SUCCESS = 0
filename.s{#MAX_PATH}
If ReadFile(0, OpenFileRequester("","","",0))
;method 1
If ZwQueryInformationFile_(FileID(0), @io.IO_STATUS_BLOCK, @fi.FILE_NAME_INFORMATION, SizeOf(FILE_NAME_INFORMATION), #FileNameInformation) = #STATUS_SUCCESS
If fi\FileNameLength > 0
filename = PeekS(@fi\filename, fi\FileNameLength, #PB_Unicode)
drv = GetLogicalDrives_()
For i = 0 To 25
exist = (drv >> i) & 1
If exist
drv$ = Chr('A' + i) + ":"
If FileSize(drv$ + filename) >= 0
Debug "method 1"
Debug filename
Debug drv$ + filename
Break
EndIf
EndIf
Next
EndIf
EndIf
Debug ""
filename = ""
;method 2 - Problem: If the file size is 0, then it seems to fail.
; http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx
If GetMappedFileName
hFileMap = CreateFileMapping_(FileID(0), 0, #PAGE_READONLY, 0, 1, 0)
If hFileMap
pMem = MapViewOfFile_(hFileMap, #FILE_MAP_READ, 0, 0, 1)
If pMem
If GetMappedFileName(GetCurrentProcess_(), pMem, @filename, #MAX_PATH)
If filename
drv = GetLogicalDrives_()
devname.s{#MAX_PATH}
For i = 0 To 25
exist = (drv >> i) & 1
If exist
drv$ = Chr('A' + i) + ":"
If QueryDosDevice_(drv$, @devname, #MAX_PATH)
If FindString(filename, devname, 1)
Debug "method 2"
Debug filename
Debug drv$ + " = " + devname
Debug ReplaceString(filename, devname, drv$)
Break
EndIf
EndIf
EndIf
Next
EndIf
EndIf
UnmapViewOfFile_(pMem)
EndIf
CloseHandle_(hFileMap)
EndIf
EndIf
Debug ""
filename = ""
;method 3
Structure OBJECT_NAME_INFORMATION ;Name.UNICODE_STRING
Length.w
MaximumLength.w
Buffer.i
EndStructure
*name.OBJECT_NAME_INFORMATION = AllocateMemory(1024)
If ZwQueryObject_(FileID(0), #ObjectNameInformation, *name, 1024, @res) = #STATUS_SUCCESS
If *name\Length > 0
filename = PeekS(*name\Buffer, *name\Length, #PB_Unicode)
drv = GetLogicalDrives_()
devname.s{#MAX_PATH}
For i = 0 To 25
exist = (drv >> i) & 1
If exist
drv$ = Chr('A' + i) + ":"
If QueryDosDevice_(drv$, @devname, #MAX_PATH)
If FindString(filename, devname, 1)
Debug "method 3"
Debug filename
Debug drv$ + " = " + devname
Debug ReplaceString(filename, devname, drv$)
Break
EndIf
EndIf
EndIf
Next
EndIf
EndIf
CloseFile(0)
EndIf
If IsLibrary(0)
CloseLibrary(0)
EndIf