This example is not implying that the request is either good nor bad and since I'm not a Dev it's not my responsibility to decide otherwise. What I can do is show that with a bit of lateral thinking the concept can be put into practice by using just native code.
Is there a huge requirement to change alot of existing code?
>> No, FileOpen becomes _FileOpen.
So how does it work?
>> Simple. Please see below.
The syntax for _FileOpen & _FileClose are exactly the same as their counterparts, the only difference is the data that is passed to _FileOpen is stored in an Array which is then used as reference when finding the filepath of a handle.
As I've mentioned this is a proof of concept UDF, so any suggestions or improvements are welcomed, but probably won't be implemented. Thanks.
UDF: Save as _FileNameByHandle.au3
#include-once ; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 ; #INDEX# ======================================================================================================================= ; Title .........: _FileNameByHandle ; AutoIt Version : v3.3.2.0 or higher ; Language ......: English ; Description ...: Find a filepath using the handle returned by _FileOpen. This can only be achieved by using the internal functions _FileOpen & _FileClose. ; Note ..........: This won't work with the handle returned by FileOpen & GetFinalPathNameByHandle won't work with FileOpen either. ; Author(s) .....: guinness ; Remarks .......: This is a workaround for the Trac Ticket: http://www.autoitscript.com/trac/autoit/ticket/1631 ; =============================================================================================================================== ; #INCLUDES# ========================================================================================================= ; None ; #GLOBAL VARIABLES# ================================================================================================= Global Enum $__hFileHandleHWnd, $__sFileHandlePath, $__iFileHandleMode, $__iFileHandleMax Global $__vFileHandleAPI[2][$__iFileHandleMax] = [[1, $__iFileHandleMax, 1],[-1, -1, -1]] ; #CURRENT# ===================================================================================================================== ; _FileClose: Opens a file for reading or writing. ; _FileNameByHandle: Retrieves the final path of the specified file. ; _FileOpen: Closes a previously opened file. ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ; __FileGetFreeIndex ......; Retrieve the index number of the array to write the new data to. ; =============================================================================================================================== Func _FileClose($hHandle) _FileNameByHandle($hHandle) Local $iIndex = @extended If $iIndex = -1 Then Return SetError(1, 0, 0) EndIf Local $iReturn = FileClose($hHandle) For $i = 0 To $__vFileHandleAPI[0][1] - 1 $__vFileHandleAPI[$iIndex][$i] = -1 Next Return $iReturn EndFunc ;==>_FileClose Func _FileNameByHandle($hHandle) For $i = 1 To $__vFileHandleAPI[0][0] If $__vFileHandleAPI[$i][$__hFileHandleHWnd] = $hHandle Then Return SetError(0, $i, $__vFileHandleAPI[$i][$__sFileHandlePath]) EndIf Next Return SetError(1, -1, '') EndFunc ;==>_FileNameByHandle Func _FileOpen($sFilePath, $iFlag = 0) Local $iIndex = 0 For $i = 1 To $__vFileHandleAPI[0][0] If $__vFileHandleAPI[$i][$__sFileHandlePath] = $sFilePath Then Switch Number((BitAND($iFlag, 1) = 1 Or BitAND($iFlag, 2) = 2)) Case 0 If $__vFileHandleAPI[$iIndex][$__iFileHandleMode] = 0 Then ExitLoop EndIf Case 1 If $__vFileHandleAPI[$iIndex][$__iFileHandleMode] = 1 Then ExitLoop EndIf EndSwitch Return $__vFileHandleAPI[$i][$__hFileHandleHWnd] EndIf Next $iIndex = __FileGetFreeIndex() $__vFileHandleAPI[$iIndex][$__hFileHandleHWnd] = FileOpen($sFilePath, $iFlag) If $__vFileHandleAPI[$iIndex][$__hFileHandleHWnd] = -1 Then Return SetError(1, 0, -1) EndIf $__vFileHandleAPI[$iIndex][$__sFileHandlePath] = $sFilePath $__vFileHandleAPI[$iIndex][$__iFileHandleMode] = Number(BitAND($iFlag, 1) = 1 Or BitAND($iFlag, 2) = 2) ; 1 = Write mode or 0 = Read mode. Return $__vFileHandleAPI[$iIndex][$__hFileHandleHWnd] EndFunc ;==>_FileOpen ; #INTERNAL_USE_ONLY#============================================================================================================ Func __FileGetFreeIndex() For $i = 1 To $__vFileHandleAPI[0][0] If $__vFileHandleAPI[$i][$__hFileHandleHWnd] = -1 Then Return $i EndIf Next If ($__vFileHandleAPI[0][0] + 1) >= $__vFileHandleAPI[0][2] Then $__vFileHandleAPI[0][2] = Ceiling(($__vFileHandleAPI[0][0] + 1) * 1.3) ReDim $__vFileHandleAPI[$__vFileHandleAPI[0][2]][$__vFileHandleAPI[0][1]] EndIf $__vFileHandleAPI[0][0] += 1 For $i = 0 To $__vFileHandleAPI[0][1] - 1 $__vFileHandleAPI[$__vFileHandleAPI[0][0]][$i] = -1 Next Return $__vFileHandleAPI[0][0] EndFunc ;==>__FileGetFreeIndex
Example use of UDF:
#include '_FileNameByHandle.au3' Local $hFileOpen = _FileOpen(@ScriptFullPath, 0) ; Open the file in reading mode. ConsoleWrite('The return value of the native function FileOpen was: ' & $hFileOpen & @CRLF) ; Debug line. MsgBox(4096, '_FileNameByHandle()', 'The filepath of the filehandle is:' & @CRLF & @CRLF & _FileNameByHandle($hFileOpen) & '.') ; Find the filepath by passing the filehandle to the function _FileNameByHandle. Local $iReturn = _FileClose($hFileOpen) ; Close the filehandle to release the file. ConsoleWrite('The return value of the native function FileClose was: ' & $iReturn & @CRLF) ; Debug line.
Edited by guinness, 10 October 2012 - 10:42 PM.







