怎样防止用户二次运行系统???

解决方案 »

  1.   

    If App.PrevInstance Then
       MsgBox "程序已经运行"
       End
    End If
      

  2.   

    If App.PrevInstance Then
            MsgBox ("程序已经运行,不能再次装载。"), vbExclamation
            End
        End If
      

  3.   

    这是VB提供的办法,但是还是有一个问题,你如果复制一个程序然后改名,它还是可以运行第二个实例的,所以也可以采取类似Lock和Unlock的做法,系统启动设一个标志,启动时查看,如果有说明已经运行,没有则没有运行
      

  4.   

    If App.PrevInstance Then End
      

  5.   

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As LongPrivate Const SW_RESTORE = 9  ' If there has been an instance of the application then activate the previous
      ' instance and terminate the current instance
      If App.PrevInstance Then
        ' Try to find main window
        Dim strTitle As String    strTitle = "Your main form's caption"
        ' Rename the current copy
        App.Title = "NewCopy"      hwnd = FindWindow(vbNullString, strTitle)
          If hwnd <> 0 Then
            lngRet = IsWindowVisible(hwnd)
            If lngRet <> 0 Then
              ' Activate the previous instance
              lngRet = ShowWindow(hwnd, SW_RESTORE)
              lngRet = SetForegroundWindow(hwnd)
              Exit For
            End If
          End If
        
        ' Terminate the current instance
        End
      End If
      

  6.   

    If App.PrevInstance Then
       MsgBox "程序已经运行"
       End
    End If
      

  7.   

    App.PrevInstance
    是通常使用的方法,
    不过, holydiablo(鱼头) 的方法也不错!
      

  8.   

    Option Explicit
    Private Declare Function ShowWindow Lib "user32" _
        (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPrivate Declare Function FindWindow Lib "user32" _
       Alias "FindWindowA" _
       (ByVal lpClassName As String, ByVal lpWindowName _
       As String) As LongPrivate Declare Function IsIconic Lib "user32" _
        (ByVal hwnd As Long) As LongPrivate Declare Function SetForegroundWindow Lib "user32" _
        (ByVal hwnd As Long) As LongConst SW_RESTORE = 9'Conatants used for the return of the MultiInst function
    Private Const OPEN_APPLICATION = 0
    Private Const SINGLE_INSTANCE_OPEN = 1Sub Main()
    Dim MultiInstResult As Integer'Call procedure to determine if an instance of
    'the application is already loaded
    MultiInstResult = MultiInst'Handle the result from the MultiInst function
    If MultiInstResult = OPEN_APPLICATION Then
         Form1.Show  
         'No instance of the application is already open,
         'continue to load the login form
    ElseIf MultiInstResult = SINGLE_INSTANCE_OPEN Then
       'An instance already exists cancel the 
       'current application load
        End
    End If
    End SubPrivate Function MultiInst() As Integer
    'This function determines if a single instance of the 
    'application is already running.Dim hwndFound As Long   'The window handle
    Dim strWindowName       'The Caption on the window'Set the caption of the application form
    strWindowName = App.Title
    App.Title = "temp title"  'set application title as temporary string'Get the handle of the application if it is open
    hwndFound = FindWindow(vbNullString, strWindowName)If hwndFound Then
         'Set the function return
         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 application minimized, restore, show it on top
         If IsIconic(hwndFound) Then
              ShowWindow hwndFound, SW_RESTORE
              'Show the window infront of all other windows
              SetForegroundWindow hwndFound
         Else
              'Bring the application top most on the screen
              SetForegroundWindow hwndFound
        End If
    ElseIf hwndFound = 0 Then
        'Set the function return so it will continue loading
        App.Title = strWindowName    'restore application title
        MultiInst = OPEN_APPLICATION
    End If
    End Function
      

  9.   

    Private Sub Form_Load()
        Dim SaveTitle$
        If App.PrevInstance Then
            SaveTitle$ = App.Title
            App.Title = "... duplicate instance."
            Form1.Caption = "... duplicate instance."
            AppActivate SaveTitle$
            SendKeys "% R", True
            End
        End If
    End Sub