how to kill a program in window and restart the program again ?

netcool
netcool used Ask the Experts™
on
I need some advice on how to kill a current runnning program (example xxx.exe) and start the program again immediately in order to refresh the winsock connection.
This is because i created a wincock client server program, sometime the client lose connection with the server after a few day or hours. so, i need the above solution to solve my problem to make sure the connection is always alive.
Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
hi... mmm. I think that's not the "elegant solution" but if is that what you wants... look:
there are so many ways tu unload a running program.
the first thing you need is the hWnd for the windows you want to close.... we can call an API function in order to get that, but you have to know the Window's Title... (yes the name at the top of the windows)

we need 2 command button called "command1" and "command2" in a form, and paste this code:

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Command1_Click()
  Dim mWnd As Long
  WM_QUIT = &H12
  WM_CLOSE = &H10
 
  mWnd = FindWindow(vbNullString, "Here Goes The Exact Title of the Windows to close")
  if mWnd =0 then exit sub 'CRASHHHHHHH if mWnd =0
  ShowWindow mWnd, 1
  PostMessage mWnd, WM_QUIT, 0&, 0&
  PostMessage mWnd, WM_CLOSE, 0&, 0&
End Sub

'and if you know where the program is installed (the one you just close) then call it again

Private Sub Command2_Click()
  Dim CallApp As String
  CallApp = "c:\windows\Notepad.exe" 'the program
  Shell CallApp, vbNormalFocus
End Sub



well, hope that works..


and after compiled, you can use the task sheduler to call this program.


hope it works, anything else, just call

O:-)

Commented:
As danmian mentioned,it wouldnt be good to do it this way, however, if you want the code, check this URL out

http://www.coderoom.com/system_vb3.html

KC

Author

Commented:
Thanks. It work just the way i want : )

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial