By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,097 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,097 IT Pros & Developers. It's quick & easy.

How to Terminating process?

P: 7
Hello All,

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

ProcessId = Shell(ProgramName, Window)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)

Do
Call GetExitCodeProcess(hProcess, exitCode)
DoEvents
Loop While exitCode = STATUS_PENDING
Call CloseHandle(hProcess)

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

But then also it is not able to terminate exe. ProcessId and hProcess stuff is ok. What can be the reason?
Mar 29 '07 #1
Share this Question
Share on Google+
8 Replies


vijaydiwakar
100+
P: 579
try this code
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
  3. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
  4. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
  5. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
  6. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  7. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
  8. Private Declare Function GetDesktopWindow Lib "user32" () As Long
  9. Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
  10. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
  11. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
  12. Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
  13. Const GW_HWNDNEXT = 2
  14. Dim mWnd As Long
  15. Function InstanceToWnd(ByVal target_pid As Long) As Long
  16.     Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
  17.     'Find the first window
  18.     test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
  19.     Do While test_hwnd <> 0
  20.         'Check if the window isn't a child
  21.         If GetParent(test_hwnd) = 0 Then
  22.             'Get the window's thread
  23.             test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
  24.             If test_pid = target_pid Then
  25.                 InstanceToWnd = test_hwnd
  26.                 Exit Do
  27.             End If
  28.         End If
  29.         'retrieve the next window
  30.         test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
  31.     Loop
  32. End Function
  33. Private Sub Form_Load()
  34.     Dim Pid As Long
  35.     'Lock the window update
  36.     LockWindowUpdate GetDesktopWindow
  37.     'Execute notepad.Exe
  38.     Pid = Shell("c:\windows\system32\notepad.exe", vbNormalFocus)
  39.     If Pid = 0 Then MsgBox "Error starting the app"
  40.     'retrieve the handle of the window
  41.     mWnd = InstanceToWnd(Pid)
  42.     'Set the notepad's parent
  43.     SetParent mWnd, Me.hwnd
  44.     'Put the focus on notepad
  45.     Putfocus mWnd
  46.     'Unlock windowupdate
  47.     LockWindowUpdate False
  48. End Sub
  49. Private Sub Form_Unload(Cancel As Integer)
  50.     'Unload notepad
  51.     DestroyWindow mWnd
  52.     'End this program
  53.     TerminateProcess GetCurrentProcess, 0
  54. End Sub
  55.  
  56.  
Mar 29 '07 #2

P: 7
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.

try this code
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
  3. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
  4. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
  5. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
  6. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
  7. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
  8. Private Declare Function GetDesktopWindow Lib "user32" () As Long
  9. Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
  10. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
  11. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
  12. Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
  13. Const GW_HWNDNEXT = 2
  14. Dim mWnd As Long
  15. Function InstanceToWnd(ByVal target_pid As Long) As Long
  16.     Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
  17.     'Find the first window
  18.     test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
  19.     Do While test_hwnd <> 0
  20.         'Check if the window isn't a child
  21.         If GetParent(test_hwnd) = 0 Then
  22.             'Get the window's thread
  23.             test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
  24.             If test_pid = target_pid Then
  25.                 InstanceToWnd = test_hwnd
  26.                 Exit Do
  27.             End If
  28.         End If
  29.         'retrieve the next window
  30.         test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
  31.     Loop
  32. End Function
  33. Private Sub Form_Load()
  34.     Dim Pid As Long
  35.     'Lock the window update
  36.     LockWindowUpdate GetDesktopWindow
  37.     'Execute notepad.Exe
  38.     Pid = Shell("c:\windows\system32\notepad.exe", vbNormalFocus)
  39.     If Pid = 0 Then MsgBox "Error starting the app"
  40.     'retrieve the handle of the window
  41.     mWnd = InstanceToWnd(Pid)
  42.     'Set the notepad's parent
  43.     SetParent mWnd, Me.hwnd
  44.     'Put the focus on notepad
  45.     Putfocus mWnd
  46.     'Unlock windowupdate
  47.     LockWindowUpdate False
  48. End Sub
  49. Private Sub Form_Unload(Cancel As Integer)
  50.     'Unload notepad
  51.     DestroyWindow mWnd
  52.     'End this program
  53.     TerminateProcess GetCurrentProcess, 0
  54. End Sub
  55.  
  56.  
Mar 31 '07 #3

vijaydiwakar
100+
P: 579
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
Mar 31 '07 #4

P: 7
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.
Apr 2 '07 #5

vijaydiwakar
100+
P: 579
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
Apr 2 '07 #6

P: 27
Try using
CreateProcess() Instead of of Shell()

you can have more precise control on the out come.
Apr 6 '07 #7

P: 7
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?
Apr 12 '07 #8

P: 27
Expand|Select|Wrap|Line Numbers
  1. Public Function EndProcess(ProcessID) As Boolean
  2. On Error Resume Next
  3. Dim Process_handle As Long
  4. Dim Process_exit_Handle As Long
  5. Dim Proc_id As Long
  6. Proc_id = ProcessID
  7.         Process_handle = OpenProcess(PROCESS_ALL_ACCESS, False, Proc_id)
  8.         If Process_handle <> 0 Then
  9.             Process_exit_Handle = TerminateProcess(Process_handle, 0&)
  10.             CloseHandle Process_handle
  11.             CloseHandle Process_exit_Handle
  12.             EndProcess = (Process_handle <> 0) And (Process_exit_Handle <> 0)
  13.         End If
  14. 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
Apr 20 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.