Private Sub Form_Load() If App.PrevInstance = True Then End End If End Sub
/************************************* 第1种 Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long Public Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type Public Const ERROR_ALREADY_EXISTS = 183& Private Sub Main() 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: [email protected] ' -> code by Raist Lin Dim sa As SECURITY_ATTRIBUTES sa.bInheritHandle = 1 sa.lpSecurityDescriptor = 0 sa.nLength = Len(sa) 'Try to create a new Mutex Debug.Print CreateMutex(sa, 1, App.Title) Debug.Print Err.LastDllError 'Check if the function was succesfull If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then 'More than one instance detected MsgBox "More than one instance" Else Form1.Show 'No other instance detected... 'Your program-load code here End If End Sub/************************************* 第2种Private Sub Form_Load() If App.PrevInstance Then MsgBox App.EXEName & 已经启动了" End End If
Private Sub Form_Load() If App.PrevInstance Then End '如果已经有运行的实例则退出 End Sub
If App.PrevInstance = True Then
End
End If
End Sub
第1种
Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Const ERROR_ALREADY_EXISTS = 183&
Private Sub Main()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
' -> code by Raist Lin
Dim sa As SECURITY_ATTRIBUTES
sa.bInheritHandle = 1
sa.lpSecurityDescriptor = 0
sa.nLength = Len(sa)
'Try to create a new Mutex
Debug.Print CreateMutex(sa, 1, App.Title)
Debug.Print Err.LastDllError
'Check if the function was succesfull
If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
'More than one instance detected
MsgBox "More than one instance"
Else
Form1.Show
'No other instance detected...
'Your program-load code here
End If
End Sub/*************************************
第2种Private Sub Form_Load()
If App.PrevInstance Then
MsgBox App.EXEName & 已经启动了"
End
End If
If App.PrevInstance Then End '如果已经有运行的实例则退出
End Sub