Jump to content



Photo

_FileNameByHandle() - Find a filepath using the handle returned by FileOpen.


  • Please log in to reply
3 replies to this topic

#1 guinness

guinness

    guinness

  • MVPs
  • 10,340 posts

Posted 07 November 2011 - 02:47 PM

This is a proof of concept for the Trac Ticket #1631. It was requesting a native AutoIt function that could find the filepath of a handle returned by FileOpen. The suggestion of the API GetFinalPathNameByHandle was bounced around, but this can only work when using _WinAPI_CreateFile/Ex.

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
AutoIt         
#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.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013






#2 guinness

guinness

    guinness

  • MVPs
  • 10,340 posts

Posted 08 October 2012 - 09:51 PM

Updated the UDF syntax and overall structure. Any suggestions then please post below. Thanks.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013


#3 JScript

JScript

    Goodbye everybody, I got tired of this system adopted here!

  • Active Members
  • PipPipPipPipPipPip
  • 1,062 posts

Posted 08 October 2012 - 10:30 PM

Excellent and very useful, 5 * from me!

JS
http://notebook.forumais.com (Forum Maintenance Notebooks and Desktop)http://autoitbrasil.com/ (AutoIt v3 Brazil!!!)
Spoiler

Posted Image Download Dropbox - Simplify your life!Your virtual HD wherever you go, anywhere!       


#4 guinness

guinness

    guinness

  • MVPs
  • 10,340 posts

Posted 09 October 2012 - 09:42 AM

Thanks JScript for the vote.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users