Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
CloseHandle closes a handle and the object associated with that handle. After being closed, the handle is of course no longer valid. This function can close objects such as files. The function returns 1 if successful, or 0 if an error occured.
Example:
' Create a new file called C:\Test\myfile.txt --
' the function will fail if the file already exists.
' Only grant write-level access to this program and read-level sharing to other programs.
' This example under Windows 95 uses the alternate Declare
' (see CreateFile for more info about that API function -- CloseHandle closes the handle it makes)
hfile = CreateFileNS("C:\Test\myfile.txt", GENERAL_WRITE, FILE_SHARE_READ, 0, CREATE_NEW, FILE_ATTRIBUTE_ARCHIVE, 0)
If hfile = -1 Then ' function failed
Debug.Print "The file probably already exists."
End If
' (code to write data to the file would go here)
x = CloseHandle(hfile) ' close the file when done
Related Call: CreateFile
Category: Miscellaneous
Back to the index.