设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 11994|回复: 31
打印 上一主题 下一主题

禁止程序多开

[复制链接]
跳转到指定楼层
1#
发表于 2008-9-8 15:05:43 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
ACCESS本身也不允许程序打开多次,  但如果把程序复制到其他目录,  就可以程序多开了
有什么方法禁止上述的行为呢  ,  方法有好几种,  这里介绍CreateMutex 建立互斥体

模块代码

Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (ByVal lpMutexAttributes As Long, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  
'variable constant to match if the mutex exists
Private Const ERROR_ALREADY_EXISTS = 183&
  
'Application Variable Declarations
Public Const AppVer = "MyApp v1.1"
Public Function Test()

Dim Mutexvalue As Long
  
'*the following code would go in the starting function of your application
'either the main() or form_load() depending on how your application works
  
'Create an individual mutex value for the application
Mutexvalue = CreateMutex(ByVal 0&, 1, AppVer)
  Debug.Print Mutexvalue
'If an error occured creating the mutex, that means it
'must have already existed, therefore your application
'is already running
If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
  
'Inform the user of running the same app twice
MsgBox "The application is already running."
  
'Terminate the application via the reference to it, its hObject value
CloseHandle Mutexvalue
Application.Quit acQuitSaveNone
End If
End Function


测试例子

游客,如果您要查看本帖隐藏内容请回复

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享分享 分享淘帖 订阅订阅
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-6-2 02:59 , Processed in 0.091185 second(s), 26 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表