可有Sample code?

解决方案 »

  1.   

    A在打开B后并没有关闭掉,而是在使用TIMER在不停的侦测B程序的状态,所以我要获得B是否仍在运行的状态来对A进行操作,请哪位哥们(姐们也行)帮个忙啦
      

  2.   


    Dim tProcInfo As PROCESS_INFORMATION, lRetVal As Long, lSuccess As Long
    Dim tStartupInf As STARTUPINFO
    Dim tSecurAttrib As SECURITY_ATTRIBUTES
    Dim hSnapShot As Long, uProcess As PROCESSENTRY32
    Const SW_HIDE = 0, SW_NORMAL = 1
    Const NORMAL_PRIORITY_CLASS = &H20&
    Const TH32CS_SNAPHEAPLIST = &H1
    Const TH32CS_SNAPPROCESS = &H2
    Const TH32CS_SNAPTHREAD = &H4
    Const TH32CS_SNAPMODULE = &H8
    Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
    Const TH32CS_INHERIT = &H80000000
    '---------------------------------------------------------------------------Private Declare Function CreateProcessA Lib "kernel32" _
    (ByVal lpApplicationName As Long, ByVal lpCommandLine As _
    String, lpProcessAttributes As Any, lpThreadAttributes As _
    Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As _
    Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As _
    Long, lpStartupInfo As Any, lpProcessInformation As Any) As LongPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function CreateToolhelp32Snapshot Lib "kernel32" _
    (ByVal lFlags As Long, ByVal lProcessID As Long) As LongPrivate Declare Function Process32First Lib "kernel32" _
    (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As LongPrivate Declare Function Process32Next Lib "kernel32" _
    (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
    '--------------------------------------------------------------------------------------------
    Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
    End TypePrivate Type STARTUPINFO
    cb As Long
    lpReserved As Long
    lpDesktop As Long
    lpTitle As Long
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
    End TypePrivate Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
    End TypePrivate Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szExeFile As String * MAX_PATH
    End TypetSecurAttrib.nLength = Len(tSecurAttrib)
    tSecurAttrib.bInheritHandle = 1&
    tSecurAttrib.lpSecurityDescriptor = 0&
    CreatePipe(lhwndReadPipe, lhwndWritePipe, tSecurAttrib, 0)
    tStartupInf.cb = Len(tStartupInf)
    tStartupInf.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
    tStartupInf.hStdOutput = lhwndWritePipe
    tStartupInf.wShowWindow = SW_HIDE
    CreateProcessA(0&, sCommandLine, tSecurAttrib, tSecurAttrib, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, tStartupInf, tProcInfo)
    Dim r, n, i
    '============================================================================================================
        hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
         uProcess.dwSize = Len(uProcess)
         r = Process32First(hSnapShot, uProcess)
        Do While r
        n = LCase(Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0)))
        For i = Len(n) To 1 Step -1
        If Mid$(n, i, 1) = "\" Then n = Mid$(n, i + 1, Len(n) - i)
        Next i
            If n = "uharc.exe" Then
                CloseHandle hSnapShot
                Exit Function
            End If
                'Retrieve information about the next process recorded in our system snapshot
            r = Process32Next(hSnapShot, uProcess)
        Loop
        'close our snapshot handle
        CloseHandle hSnapShot
        GoTo Err:
        '============================================================================================================
    Exit Function
    Err:
    'Close handles
    Call CloseHandle(tProcInfo.hProcess)
    Call CloseHandle(tProcInfo.hThread)
      

  3.   

    注:win98的
    我就是小白
    大家不要骂了
    呵呵
    我也在想这方面的问题
    交流一下:[email protected]
      

  4.   

    这个好办:Option Explicit
    ' Shows how to shell to another program, and wait until it finishes
    ' before continuing.Private Declare Function WaitForSingleObject Lib "kernel32" _
       (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" _
       (ByVal hObject As Long) As Long
       
    Private Declare Function OpenProcess Lib "kernel32" _
       (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
        ByVal dwProcessId As Long) As Long
    Private Const INFINITE = -1&
    Private Const SYNCHRONIZE = &H100000Private Sub Command1_Click()
        Dim iTask As Long, ret As Long, pHandle As Long
        iTask = Shell("notepad.exe", vbNormalFocus)
        pHandle = OpenProcess(SYNCHRONIZE, False, iTask)
        ret = WaitForSingleObject(pHandle, INFINITE)
        ret = CloseHandle(pHandle)
        MsgBox "程序运行完成!"
    End Sub
      

  5.   

    我因为用A程序中的TIMER侦测B程序中的一个窗体上的变量,所以要用到TIMER,你们说的方法我回家去试试,现在要下班了,哈哈,谢谢各位兄弟,大家多多交流呀,我有什么主意再和大家说
    谢谢xwzxwz,virtualdesktop,
      

  6.   

    to:virtualdesktop
    我所说的B程序是要随时可能关闭的,所以还是需要一个类似TIMER的东东进行监控的to: xwzxwz
    这几天都没睡好,现在看代码有点子小累(哪叫你贴那么多呀,呵呵,开个玩笑,谢谢)你们的思路我大概明白点,你们都是从进程着手突破的,我也会再试试,我在这再顶一下看看还有没有兄弟帮着出出主意有看这贴子的兄弟(姐妹)帮着顶顶呀,兄弟在此表示感谢!
      

  7.   

    首先TIMER是一定要有的,其次是用 FINDWINDOW的API,监测是否有B这个进程存在,其实也峭什么难的,B进程的窗体名字一定是固定的吧?所以FINDWINDOW这个API可以实现
      

  8.   

    to:siyou
    呵呵,我要监控的正是程序B的窗体名因为B的窗体名是有可能会变的,嘻嘻FINDWINDOW我已经试过,但中间还是有些小问题没解决我很郁闷。再次请看贴的兄弟姐妹帮我顶一下。
      

  9.   

    Shell返回进程ID,跟据这个ID得到句柄,监控拥有这个句柄的窗体是否存在
      

  10.   

    xwzxwz(xwz)
    子进程,这个方法最好,耗资源最少!!!
    用timer当然也可以!