用系统原子:  GlobalAddAtom
  GlobalFindAtom  char* a="XXX只执行一次";
  ATOM atom;
  atom=GlobalFindAtom(a);
  if(atom==0)
    GlobalAddAtom(a);
  else
   MessageBox "程序运行中!"  
  在程序结束删除原子:
   
  atom=GlobalFindAtom(a);
  GlobalDeleteAtom(atom);
   
  

解决方案 »

  1.   

    对不起!你们说的什么互斥、系统原子小弟根本就没有听说过,能不能尽量的详细!如:c:\a.exe、d:\b.exe,如何判断?万分感谢!
      

  2.   

        Dim Title As String
        If App.PrevInstance Then
            Title = App.Title
            Call MsgBox("这程式已执行", vbOKOnly, vbCritical)
            App.Title = "" '如此才不会Avtivate到自己
            FM.Caption = ""
            AppActivate Title 'activate先前就已行的程式
            End
        End If
      

  3.   

    对,可以用系统原子,查查msdn
      

  4.   

    to yqh(毛毛虫):我试了你的方法,可是你的方法好像只能是控制一个程序文件不被打开两次!我要问的是不同路径下的不同文件名的文件(但是是同一个程序,只是名字不同)!
    to slay78(天灯石):
    我应该怎么查msdn?我应该打入什么查询关键字?
    让大家费心了!
      

  5.   

    Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
    Private Declare Function GlobalDeleteAtom Lib "kernel32" Alias "GlobalDeleteAtom" (ByVal nAtom As Integer) As Integer
    Private Declare Function GlobalFindAtom Lib "kernel32" Alias "GlobalFindAtomA" (ByVal lpString As String) As Integer
    原子其实就是系统的一个字符串
      

  6.   

    所以这样不好,我觉得还是应该用FindWindow
      

  7.   

    各位,你们写的太能不能举出具体的例子:如:c:\a.exe、d:\b.exe,如何判断?谢谢!
      

  8.   

    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate mlMainForm As Long
    Private msMainForm As StringPrivate Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const WM_SYSCOMMAND = &H112
    Private Const SC_RESTORE = &HF120&
    '激活窗口
    Private Sub ActivateWin(ByVal lHwnd As Long)
        Call SetForegroundWindow(lHwnd)
        Call SendMessage(lHwnd, WM_SYSCOMMAND, SC_RESTORE, 0)
    End Sub'枚举窗口回调函数
    Private Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
        On Error Resume Next
        Dim sBuffer As String * 255, sText As String
        Call GetWindowText(hwnd, sBuffer, 256)
        sText = StrZToStr(sBuffer)
        If msMainForm <> "" Then
            sText = Mid(sText, 1, Len(msMainForm))
            If sText = msMainForm Then mlMainForm = hwnd
        End If
        EnumChildProc = True
    End FunctionPrivate Function StrZToStr(s As String) As String
        If InStr(1, s, Chr$(0)) > 0 Then
            StrZToStr = Left$(s, InStr(1, s, Chr$(0)) - 1)
        Else
            StrZToStr = s
        End If
    End FunctionSub Main()
        On Error Resume Next
        Dim lDHwnd As Long
        If App.PrevInstance Then
            lDHwnd = GetDesktopWindow
            '打开窗口
            msMainForm = "Form1"
            If lDHwnd <> 0 Then Call EnumChildWindows(lDHwnd, AddressOf EnumChildProc, 0)
            If mlMainForm <> 0 Then
                '如果找到已经定义的窗口,激活该窗口,然后终止运行
                Call ActivateWin(mlMainForm)
                End
            Else
                frmMain.Show
            End If
        Else
            frmMain.Show
        End If
    End Sub
      

  9.   

    我觉得还是有问题!
    1、你用了App.PrevInstance,这样只能是控制一个程序文件不被打开两次!我要问的是不同路径下的不同文件名的文件(但是是同一个程序,只是名字不同)!
    2、用你的方法,如果我有两个不同的程序,但是窗体的名字都是FORM1,那岂不是会有问题!还是用系统原子好一些吧?怎么做?谢!
      

  10.   

      dim atom as integer,a as string
      a="XXX只执行一次"
      atom=GlobalFindAtom(a)
      if atom=0 then
        GlobalAddAtom(a)
      else
         MsgBox "程序运行中!"
      end if
      
      在程序结束删除原子:  
      dim atom as integer,a as string
      a="XXX只执行一次"
      atom=GlobalFindAtom(a)
      GlobalDeleteAtom(atom)