Sign in to follow this  
Followers 0
Squeeto

DllCall kernel32.dll CloseHandle doesn't close?

4 posts in this topic

Hi guys,

I have been troubled with this one a while; it is time to ask.

I would like to delete a file after a DllCall for CreateFile but the call holds up the file. Am I not releasing it somehow?

#include <GUIConstants.au3>

$main = GUICreate("", 115, 65)
$btn = GUICtrlCreateButton("", 20, 20, 75, 25, $BS_DEFPUSHBUTTON)
GUISetState(@SW_SHOW)

While 1
  $msg = GUIGetMsg(1)
  Select
    Case $msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $main
      ExitLoop
    Case $msg[0] = $btn And $msg[1] = $main
      $s = @ScriptDir & "\asdf.txt"
      a($s)
      FileDelete($s) ;<---why doesn't this work?
  EndSelect
WEnd

Func a($file)
  $hFile = DllCall("kernel32.dll", "ptr", "CreateFile", _
    "str", $file, _
    "dword", $GENERIC_READ, _
    "dword", $FILE_SHARE_READ, _
    "dword", "", _
    "dword", $OPEN_EXISTING, _
    "dword", $FILE_ATTRIBUTE_NORMAL, _
    "ptr", 0)
  If Not @error Then
   ;... I will do more here later
    DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hFile)
    If Not @error=0 Then MsgBox(0,"Error",@error)
  Else
    MsgBox(16, "Error", "Error retreaving file.")
  EndIf
EndFunc

TIA

Share this post


Link to post
Share on other sites



Both CreateFile and CloseHandle are available in WinAPI.au3 as _WinAPI_CreateFile and _WinAPI_CloseHandle, so you don't need to worry about the calling of the functions.


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Read again DllCall documentation and what kind of variable it returns ^^

Share this post


Link to post
Share on other sites

Read again DllCall documentation and what kind of variable it returns ^^

Crap. I have done this before!

Thanks guys.

DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hFile[0])

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0