2012年3月29日 星期四

防止程式多開源碼

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 "輸入你要打的字", 0 + 64, " 輸入你要打的字 "
Unload Me
End If
End Sub