用api函数 findwindow
Public Const GW_HWNDNEXT = 2Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
  ByVal wCmd As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
  (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" _
  (ByVal hwnd As Long, lpdwprocessid As Long) As LongFunction ProcIDFromWnd(ByVal hwnd As Long) As Long
   Dim idProc As Long
   
   ' Get PID for this HWnd
   GetWindowThreadProcessId hwnd, idProc
   
   ' Return PID
   ProcIDFromWnd = idProc
End Function
      
Function GetWinHandle(hInstance As Long) As Long
   Dim tempHwnd As Long
   
   ' Grab the first window handle that Windows finds:
   tempHwnd = FindWindow(vbNullString, vbNullString)
   
   ' Loop until you find a match or there are no more window handles:
   Do Until tempHwnd = 0
      ' Check if no parent for this window
      If GetParent(tempHwnd) = 0 Then
         ' Check for PID match
         If hInstance = ProcIDFromWnd(tempHwnd) Then
            ' Return found handle
            GetWinHandle = tempHwnd
            ' Exit search loop
            Exit Do
         End If
      End If
   
      ' Get the next window handle
      tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
   Loop
End Function 
Add the following code to the form:Sub Command1_Click()
   Dim hInst As Long             ' Instance handle from Shell function.
   Dim hWndApp As Long           ' Window handle from GetWinHandle.
   Dim buffer As String          ' Holds caption of Window.
   Dim numChars As Integer       ' Count of bytes returned.
   
   ' Shell to an application
   hInst = Shell("calc.exe")
   
   ' Begin search for handle
   hWndApp = GetWinHandle(hInst)
   
   If hWndApp <> 0 Then
      ' Init buffer
      buffer = Space$(128)
      
      ' Get caption of window
      numChars = GetWindowText(hWndApp, buffer, Len(buffer))
   
      ' Display window's caption
      MsgBox "You shelled to the Application: " & Left$(buffer, numChars)
   End If
End Sub 

解决方案 »

  1.   

    Some Microsoft Windows application programming interface (API) function calls, for example, FindWindow(), require the window class name (the main window). MORE INFORMATION
    The window class names used by each Microsoft Office 97 program are listed in the following table. 
       Program name           Window class name
       ----------------------------------------
       Microsoft Access       OMain
       Microsoft Excel        XLMAIN
       Microsoft Outlook      rctrl_renwnd32
       Microsoft PowerPoint   PP97FrameClass
       Microsoft Word         OpusApp 
      

  2.   

    这个例子更好
    Option Explicit      Private Declare Function WaitForSingleObject Lib "kernel32" _
             (ByVal hHandle As Long, _
             ByVal dwMilliseconds As Long) As Long      Private Declare Function FindWindow Lib "user32" _
             Alias "FindWindowA" _
             (ByVal lpClassName As String, _
             ByVal lpWindowName As String) As Long      Private Declare Function PostMessage Lib "user32" _
             Alias "PostMessageA" _
             (ByVal hwnd As Long, _
             ByVal wMsg As Long, _
             ByVal wParam As Long, _
             ByVal lParam As Long) As Long      Private Declare Function IsWindow Lib "user32" _
             (ByVal hwnd As Long) As Long      'Constants used by the API functions
          Const WM_CLOSE = &H10
          Const INFINITE = &HFFFFFFFF      Private Sub Form_Load()
             Command1.Caption = "Start the Calculator"
             Command2.Caption = "Close the Calculator"
          End Sub      Private Sub Command1_Click()
          'Starts the Windows Calculator
             Shell "calc.exe", vbNormalNoFocus
          End Sub      Private Sub Command2_Click()
          'Closes the Windows Calculator
             Dim hWindow As Long
             Dim lngResult As Long
             Dim lngReturnValue As Long         hWindow = FindWindow(vbNullString, "Calculator")
             lngReturnValue = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
             lngResult = WaitForSingleObject(hWindow, INFINITE)         'Does the handle still exist?
             DoEvents
             hWindow = FindWindow(vbNullString, "Calculator")
             If IsWindow(hWindow) = 1 Then
                'The handle still exists. Use the TerminateProcess function
                'to close all related processes to this handle. See the
                'article for more information.
                MsgBox "Handle still exists."
             Else
                'Handle does not exist.
                MsgBox "Program closed."
             End If
          End Sub 
      

  3.   

    我要察看的这个程序好像没有窗口类,是数据库里的bcp复制命令,用findwidow函数不知道可不可以
      

  4.   

    第二个例子好一些,我明白了,谢谢coolstar
      

  5.   

    还是不行啊,那位大虾能告诉我怎么判断一个dos程序是否在运行?因为用winexe执行bcp复制命令时,是在后台运行的,而为了判断数据是否被复制到数据库,才不得不出次下策,急急急死我了~~~快来就我啊
    当然如果那位高手能告诉我怎么从数据库到数据库copy数据,那更好,bcp命令让我伤透了心,:(
      

  6.   

    请注意,有一类程序不能应用下面这段小程序:如果你在“生成EXE”文件时,将
    应用程序的名称设置成为零字符,那么这段程序就无效 了。这是因为很多很多窗
    口都将其标题设为零字符。另一个需要注意的地方就是:该程序用到了App.Previnstance来检查程序的另一个
    实例正在运行。这样做可以提高程序的效率, 但代价是你不能同时运行两个或以
    上的要检查的程序。如果你想这样做的话,请将有App.Previnstance的那一行语句
    注释掉。请将下面的代码放置在模块中:Declare Function GetWindowWord% Lib "User" (ByVal hWnd%, ByVal nIndex%) 
    Declare Function GetWindowText% Lib "User" (ByVal hWnd%, ByVal lpString$,
    ByVal aint%) 
    Declare Function GetWindowTextLength% Lib "User" (ByVal hWnd%) 
    Declare Function GetWindow% Lib "User" (ByVal hWnd%, ByVal wCmd%) 
    Declare Function SetFocusAPI% Lib "User" Alias "SetFocus" (ByVal hWnd%) ' get window word constants 
    Const GWW_HWNDPARENT = (-8) ' get window constants 
    Const GW_HWNDFIRST = 0 
    Const GW_HWNDNEXT = 2'-----------------------------------------------------------------------
    ------------- 
    ' 函数: Get_Other_Instance:布尔型, 参数( inhwnd Inputonly, outhwnd 
    Outputonly) 
    ' 目的: 获得要检测程序的另一个实例的窗口句柄。

    ' 描述: 同其它例子不同的是:该程序能检测哪些在运行时改变主窗口标题的程序,
    如MS WORD
    ' 实现的方法是利用VB程序独有的特点,即:每一个VB程序在运行时都会产生一个
    隐藏的父窗口。该父窗口的标题就是
    ' 你在生成EXE文件时所输入的程序名。VB程序员很少改变这些字符,并且用户看
    不到该父窗口。

    ' 输入:inhwnd -- 被叫窗口的窗口句柄 
    ' 输出:如果窗口被找到则为真,否则为假。 
    ' outhwnd -- 0 或设为另一个窗体的父窗口的hwnd 

    ' ----------------------------------------------------------------------
    -------------- 

    Public Function get_other_instance (ByVal inhwnd As Integer, outhwnd As 
    Integer) As Integer 
    Dim parent%, nlen%, ptext$, nexthwnd%, wtext$ 
    get_other_instance = False 
    outhwnd = 0 If Not app.PrevInstance Then Exit Function parent% = GetWindowWord(inhwnd, GWW_HWNDPARENT) 
    nlen% = GetWindowTextLength(parent%) + 2 
    ptext$ = Space$(nlen%) 
    nlen% = GetWindowText(parent%, ptext$, nlen%) 
    ptext$ = Left$(ptext$, nlen%) 
    nexthwnd% = GetWindow(parent%, GW_HWNDFIRST) ' get the first window 
    in the window list 
    Do While nexthwnd% > 0 
    nlen% = GetWindowTextLength(nexthwnd%) + 2 
    wtext$ = Space$(nlen%) 
    nlen% = GetWindowText(nexthwnd%, wtext$, nlen%) 
    wtext$ = Left$(wtext$, nlen%) If wtext$ = ptext$ And nexthwnd% <> parent% Then 
    get_other_instance = True 
    outhwnd = nexthwnd% 
    Exit Do 
    End If nexthwnd% = GetWindow(nexthwnd%, GW_HWNDNEXT) Loop End Function将下述代码放在窗体的Load事件中Sub Form_Load() 
    Dim otherhwnd% If Get_Other_Instance(Hwnd, otherhwnd%) then 
    MsgBox "Application is already running. Switching to existing 
    Application" 
    SetFocusAPI otherhwnd% 
    End 
    End If End Sub
      

  7.   

    如果要简单的话,就用VB中的app.title,与你的字符串比较就能得到结果了.