
vb禁止程序多开
我的思路是:当窗体Load入时,判断C盘有没有一个txt文件,如果没有则建立,可以运行程序;如果有则提示程序已经打开,不能运行同一个程序。请问如何写代码?我是要哪个程序无...
我的思路是:当窗体Load入时,判断C盘有没有一个txt文件,如果没有则建立,可以运行程序;如果有则提示程序已经打开,不能运行同一个程序。请问如何写代码?
我是要哪个程序无论改名、移动到哪个路径也不能多开,只能够运行一个程序。 展开
我是要哪个程序无论改名、移动到哪个路径也不能多开,只能够运行一个程序。 展开
3个回答
展开全部
'下面的就是你要的效果,你的那个和我这个是一样的原理,不过是内外存检测的区别,标准的就是这个创建互斥体,不多说了,复制代码试了看看:
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 "程序不能多开!"
Unload Me
End If
End Sub
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 "程序不能多开!"
Unload Me
End If
End Sub
展开全部
不用那么麻烦的,把这段加入form_load里就可以了。
Private Sub Form_Load()
If App.PrevInstance Then
MsgBox "程序已在运行。", vbInformation, "系统"
End
End If
End Sub
Private Sub Form_Load()
If App.PrevInstance Then
MsgBox "程序已在运行。", vbInformation, "系统"
End
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Const SW_RESTORE = 9
Private Const OPEN_APPLICATION = 0
Private Const SINGLE_INSTANCE_OPEN = 1
Private Function MultiInst() As Integer
Dim hwndFound As Long
Dim strWindowName
strWindowName = App.title
App.title = "temp title"
hwndFound = FindWindow(vbNullString, strWindowName)
If hwndFound Then
MultiInst = SINGLE_INSTANCE_OPEN
'MsgBox "A instance of the application is already open." & vbCrLf & vbCrLf & "Only one open instance allowed.", vbOKOnly + vbExclamation, "App Name"
If IsIconic(hwndFound) Then
ShowWindow hwndFound, SW_RESTORE
SetForegroundWindow hwndFound
Else
SetForegroundWindow hwndFound
End If
ElseIf hwndFound = 0 Then
App.title = strWindowName
MultiInst = OPEN_APPLICATION
End If
End Function
Sub Main()
Dim MultiInstResult As Integer
MultiInstResult = MultiInst
If MultiInstResult = OPEN_APPLICATION Then
msgbox "运行程序"
ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then
'退出
End
End If
End Sub
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Const SW_RESTORE = 9
Private Const OPEN_APPLICATION = 0
Private Const SINGLE_INSTANCE_OPEN = 1
Private Function MultiInst() As Integer
Dim hwndFound As Long
Dim strWindowName
strWindowName = App.title
App.title = "temp title"
hwndFound = FindWindow(vbNullString, strWindowName)
If hwndFound Then
MultiInst = SINGLE_INSTANCE_OPEN
'MsgBox "A instance of the application is already open." & vbCrLf & vbCrLf & "Only one open instance allowed.", vbOKOnly + vbExclamation, "App Name"
If IsIconic(hwndFound) Then
ShowWindow hwndFound, SW_RESTORE
SetForegroundWindow hwndFound
Else
SetForegroundWindow hwndFound
End If
ElseIf hwndFound = 0 Then
App.title = strWindowName
MultiInst = OPEN_APPLICATION
End If
End Function
Sub Main()
Dim MultiInstResult As Integer
MultiInstResult = MultiInst
If MultiInstResult = OPEN_APPLICATION Then
msgbox "运行程序"
ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then
'退出
End
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |