How to Terminating process?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • varsha desai
    New Member
    • Mar 2007
    • 7

    #1

    How to Terminating process?

    Hello All,

    I am calling one of the exe with the help of shell command as follows

    ProcessId = Shell(ProgramNa me, Window)
    hProcess = OpenProcess(PRO CESS_QUERY_INFO RMATION, False, ProcessId)

    Do
    Call GetExitCodeProc ess(hProcess, exitCode)
    DoEvents
    Loop While exitCode = STATUS_PENDING
    Call CloseHandle(hPr ocess)

    In some cases I want to terminate exe and come out from it. For that I am using code like below,
    Call PostMessage(hPr ocess, WM_CLOSE, 0&, 0&)
    Call PostMessage(hPr ocess, WM_DESTROY, 0&, 0&)
    Call PostMessage(hPr ocess, WM_NCDESTROY, 0&, 0&)
    TerminateProces s hProcess, 0&

    But then also it is not able to terminate exe. ProcessId and hProcess stuff is ok. What can be the reason?
  • vijaydiwakar
    Contributor
    • Feb 2007
    • 579

    #2
    try this code
    Code:
     
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    Const GW_HWNDNEXT = 2
    Dim mWnd As Long
    Function InstanceToWnd(ByVal target_pid As Long) As Long
        Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
        'Find the first window
        test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
        Do While test_hwnd <> 0
            'Check if the window isn't a child
            If GetParent(test_hwnd) = 0 Then
                'Get the window's thread
                test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
                If test_pid = target_pid Then
                    InstanceToWnd = test_hwnd
                    Exit Do
                End If
            End If
            'retrieve the next window
            test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
        Loop
    End Function
    Private Sub Form_Load()
        Dim Pid As Long
        'Lock the window update
        LockWindowUpdate GetDesktopWindow
        'Execute notepad.Exe
        Pid = Shell("c:\windows\system32\notepad.exe", vbNormalFocus)
        If Pid = 0 Then MsgBox "Error starting the app"
        'retrieve the handle of the window
        mWnd = InstanceToWnd(Pid)
        'Set the notepad's parent
        SetParent mWnd, Me.hwnd
        'Put the focus on notepad
        Putfocus mWnd
        'Unlock windowupdate
        LockWindowUpdate False
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        'Unload notepad
        DestroyWindow mWnd
        'End this program
        TerminateProcess GetCurrentProcess, 0
    End Sub

    Comment

    • varsha desai
      New Member
      • Mar 2007
      • 7

      #3
      Thanks for your reply,
      Actually I am calling exe of one of the vb project in my project. This called project does necessary calculations after calling and after doing calculations comes out from it. I am adding stop fuctionality in my project so if user want to stop in between then it will stop calculation of that project and will come out from it again to my project.

      I tried your code but it doesn't work for me. Does there can be something in that project which prevent it coming out from it. I don't think so.

      Originally posted by vijaydiwakar
      try this code
      Code:
       
      Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
      Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
      Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
      Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
      Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
      Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
      Private Declare Function GetDesktopWindow Lib "user32" () As Long
      Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
      Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
      Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
      Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
      Const GW_HWNDNEXT = 2
      Dim mWnd As Long
      Function InstanceToWnd(ByVal target_pid As Long) As Long
          Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
          'Find the first window
          test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
          Do While test_hwnd <> 0
              'Check if the window isn't a child
              If GetParent(test_hwnd) = 0 Then
                  'Get the window's thread
                  test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
                  If test_pid = target_pid Then
                      InstanceToWnd = test_hwnd
                      Exit Do
                  End If
              End If
              'retrieve the next window
              test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
          Loop
      End Function
      Private Sub Form_Load()
          Dim Pid As Long
          'Lock the window update
          LockWindowUpdate GetDesktopWindow
          'Execute notepad.Exe
          Pid = Shell("c:\windows\system32\notepad.exe", vbNormalFocus)
          If Pid = 0 Then MsgBox "Error starting the app"
          'retrieve the handle of the window
          mWnd = InstanceToWnd(Pid)
          'Set the notepad's parent
          SetParent mWnd, Me.hwnd
          'Put the focus on notepad
          Putfocus mWnd
          'Unlock windowupdate
          LockWindowUpdate False
      End Sub
      Private Sub Form_Unload(Cancel As Integer)
          'Unload notepad
          DestroyWindow mWnd
          'End this program
          TerminateProcess GetCurrentProcess, 0
      End Sub

      Comment

      • vijaydiwakar
        Contributor
        • Feb 2007
        • 579

        #4
        Originally posted by varsha desai
        Thanks for your reply,
        Actually I am calling exe of one of the vb project in my project. This called project does necessary calculations after calling and after doing calculations comes out from it.
        if this is the main functionality then y don't u go for dll insted of exe

        Comment

        • varsha desai
          New Member
          • Mar 2007
          • 7

          #5
          What is the purpose of using exe than dll? Is it difference of in-process and out-process? Do you mean to say if I will use exe rather than dll then it may work. If yes then I should ask for other team members(we don't have authority do it) to make dll. Do you think that there can be anything in project itself which prevent's by termainating it because if I click stop then it finish current calculation which it is doing and then comes out of exe, means it comes out of exe but after doing all calculations.

          Comment

          • vijaydiwakar
            Contributor
            • Feb 2007
            • 579

            #6
            Originally posted by varsha desai
            What is the purpose of using exe than dll? Is it difference of in-process and out-process? Do you mean to say if I will use exe rather than dll then it may work. If yes then I should ask for other team members(we don't have authority do it) to make dll. Do you think that there can be anything in project itself which prevent's by termainating it because if I click stop then it finish current calculation which it is doing and then comes out of exe, means it comes out of exe but after doing all calculations.
            See versha
            the main purpose of useing dll is not in process or out process
            the main aim is the communication between tow applications
            just visualise the scope

            Comment

            • pongscript
              New Member
              • Mar 2007
              • 27

              #7
              Try using
              CreateProcess() Instead of of Shell()

              you can have more precise control on the out come.

              Comment

              • varsha desai
                New Member
                • Mar 2007
                • 7

                #8
                Till no luck. Does this problem can be because of window I am using? I am using window XP. Does WinXP is more strict regarding to process permissions?

                Comment

                • pongscript
                  New Member
                  • Mar 2007
                  • 27

                  #9
                  Code:
                  Public Function EndProcess(ProcessID) As Boolean
                  On Error Resume Next
                  Dim Process_handle As Long
                  Dim Process_exit_Handle As Long
                  Dim Proc_id As Long
                  Proc_id = ProcessID
                          Process_handle = OpenProcess(PROCESS_ALL_ACCESS, False, Proc_id)
                          If Process_handle <> 0 Then
                              Process_exit_Handle = TerminateProcess(Process_handle, 0&)
                              CloseHandle Process_handle
                              CloseHandle Process_exit_Handle
                              EndProcess = (Process_handle <> 0) And (Process_exit_Handle <> 0)
                          End If
                  End Function

                  try to use this code on terminating your process, i didnt include the declaration for API... you could pass the processid that came from the createprocess method...hope this solves your problem

                  Comment

                  Working...