That's a good idea. I've actually been working on the code for that:
Declarations: -
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
-
Public Declare Function CreateFileA Lib "kernel32" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
-
Public Declare Function DeleteFileA Lib "kernel32" (ByVal lpFileName As String) As Long
-
-
Public Const FILE_ATTRIBUTE_NORMAL As Long = 128
-
Public Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000
-
Public Const GENERIC_READ As Long = &H80000000
-
Public Const CREATE_NEW As Long = 1
-
-
Public hFile As Long, I As Long
-
General Code: (Module)
-
Sub Main()
-
-
hFile = CreateFileA(App.Path & "\" & "MYINST.INI", GENERIC_READ, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_DELETE_ON_CLOSE, 0)
-
If hFile <> -1 Then
-
Form1.Show
-
Else
-
End
-
End If
-
-
End Sub
-
General Code: (Form)
-
Sub Form_Unload()
-
-
CloseHandle hFile
-
-
End Sub
-
When the program runs the first time, it will create a configuration file in the same directory. The file is created with the FILE_FLAG_DELETE_ON_CLOSE value, which causes the file to be deleted when it is closed.
If another instance of the same program is created, it will see if that configuration file exists; if it does, it immediately closes. If not, it will re-create the file, and continue to run.