Private
Sub
Form_Load()
If App.PrevInstance Then
MsgBox " 程序已在运行。 " , vbInformation, " 系统 "
End
End If
End Sub
If App.PrevInstance Then
MsgBox " 程序已在运行。 " , vbInformation, " 系统 "
End
End If
End Sub
以上这种方法,是只针对于同目录同名的程序,下面还有一种方法是通过API函数实现的,不论程序怎么改名都不可以多开的。
Private
Declare
Function
CreateMutex
Lib
"
kernel32
"
Alias
"
CreateMutexA
"
(lpMutexAttributes
As
SECURITY_ATTRIBUTES,
ByVal
bInitialOwner
As
Long
,
ByVal
lpName
As
String
)
As
Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const ERROR_ALREADY_EXISTS = 183 &
Private Sub Form_Load()
Dim sa As SECURITY_ATTRIBUTES
sa.bInheritHandle = 1
sa.lpSecurityDescriptor = 0
sa.nLength = Len (sa)
Call CreateMutex(sa, 1 , App.Title)
If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
MsgBox "程序已在运行。", vbInformation, "系统"
End
End If
End Sub
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Const ERROR_ALREADY_EXISTS = 183 &
Private Sub Form_Load()
Dim sa As SECURITY_ATTRIBUTES
sa.bInheritHandle = 1
sa.lpSecurityDescriptor = 0
sa.nLength = Len (sa)
Call CreateMutex(sa, 1 , App.Title)
If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
MsgBox "程序已在运行。", vbInformation, "系统"
End
End If
End Sub