VB 避免程序二次运行

Private   Sub  Form_Load()
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值