我原来有想到运行程序时在安装目录下写标志文件,退出时再删掉,这样就可以识别程序有无在运行 了。但又担心程序非正常关闭时删不掉标志文件。
我只想用最简单的方法来实现。

解决方案 »

  1.   

    Private Sub Form_Load()
    If App.PrevInstance Then
      MsgBox App.EXEName & "程序处在运行状态"
      End
    End If
    End Sub
      

  2.   

    App.PrevInstance只能解决一般情况。比如将这个可执行文件复制到其他目录,或将其改名,同样能够运行。
      

  3.   

    'This code must be pasted into a module
    'Set the project's startup object to 'Sub Main' (-> Project -> Project Properties -> General Tab -> Startup Object)
    Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
    Public Type SECURITY_ATTRIBUTES
            nLength As Long
            lpSecurityDescriptor As Long
            bInheritHandle As Long
    End Type
    Public Const ERROR_ALREADY_EXISTS = 183&
    Private Sub Main()
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        ' -> code by Raist Lin
        Dim sa As SECURITY_ATTRIBUTES
        sa.bInheritHandle = 1
        sa.lpSecurityDescriptor = 0
        sa.nLength = Len(sa)
        'Try to create a new Mutex
        Debug.Print CreateMutex(sa, 1, App.Title)
        Debug.Print Err.LastDllError
        'Check if the function was succesfull
        If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
            'More than one instance detected
            MsgBox "More than one instance"
        Else
            'No other instance detected...
            'Your program-load code here
        End If
    End Sub