Closing a exe in vb code

smoochy boys on tour

mazz

Dedicated Member
Dedicated Member
Aug 6, 2008
193
2
65
Im using vb 6.0 to create a server launcher so ive use the shell command to open the server but when its comes to closing ive had trouble anyone know an easy way to end a exe using vb

The only way i know is to call APIs
Could use findwindow and sendmessage APIs
Ive tryed to dont seem to work or im doing something wrong


Updated

ok i got the APIs findwindow and sendmessage working
but now i run into the dialogbox msg yes or no lol
when i use the Spy ++ app to find the handle it returns as #32770(Dialog) its caption is confirm
when i try to close this window it does nothing

So... Ive moved on to trying API SetForeground and using the sendkeys "{enter}" command
it works but only if i pound the button i made for it lol

Anyone have any other ideas on what to try here cause im stumped


btw the scope of this project is to fix the 2.3 server launcher so you can actually end it in processes and Make the server launcher a Parent form for all parts of the server(so you dont have 6 apps open on your start bar) and also put in system tray(even more space) Also add in run on startup options/Auto run server options) making the server much more automated) Could even add in a system time check for reboot times and the ablitity to announce to game players that reboot is about to happen.
This would be very nice for anyone who is running a 2.3 mir server
 
Last edited:

DjDarkBoyZ

Dev
Golden Oldie
Aug 11, 2006
1,065
228
260
127.Ø.Ø.1
Try to use the non caption method, search about use api and the pid for the exe name of the app and you can manage it ;)
 

mazz

Dedicated Member
Dedicated Member
Aug 6, 2008
193
2
65
Ive done the non caption used vbnullstring for that
see my problems is I close out the exe for whatever part of the server using findwindow and sendmessage but then i run into the dialog which says yes or no
Ive bin messing with the findwindowex which has the power to find child windows unlike findwindow(only parent)
Im not sure exactly how it should be set for findwindowex


Mwnd = FindWindow("TFrmMain", vbnullstring)
Cwnd= FindWindowex(Mwnd, vbnullstring , "#32770", vbnullstring) ' not sure
Swnd = SendMessage(Cwnd, WM_SYSCOMMAND, SC_CLOSE, NILL)


Update


Private Sub KillYesNo()
Dim tWnd As Long, bWnd As Long, sWnd2 As Long
tWnd = FindWindow("TFrmMain", vbNullString)
bWnd = FindWindowEx(tWnd, ByVal 0&, vbNullString, "Confirm")
sWnd2 = SendMessage(bWnd, WM_SYSCOMMAND, SC_CLOSE, NILL)
MsgBox sWnd2
'this returns 0
End Sub

in fact all varitables return 0 except twnd
Ive bin reading the enumWindows and enumChildWindow APIs.....
Just hoping someone will throw me a bone here. am I on the right track?
 
Last edited:

DjDarkBoyZ

Dev
Golden Oldie
Aug 11, 2006
1,065
228
260
127.Ø.Ø.1
If you don't like complicate your life more put a sendkeys and a little sleep after calling kill process, but if your intention is kill the process immediately try to use shell (Fast and simply) or (extended) api for memory management.

This depends of the language you are programing the function, vb6 have a little limitations need to crack using the cracked vb 6.5 version accepts vb6 + asm code (the best) and in vb6 you have more examples over the www, vb.net is a little more easy but different but not have an expensive examples in www, c++ is more customizable and have a great examples in the Internet.

The methods or calls are the same (API) but the options and other are limited by language.


VB6 Code for detecting and closing app inmediately using api and using
the exe name:
'#################################################
'### VB6 Close and Detect App Module for LomCN ###
'### Dj`Wincha / DjDarkBoyZ ###
'#################################################

'API II
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" _
(ByVal lFlags As Long, ByVal lProcessID As Long) As Long

Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)

Private 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 * 260
End Type

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
ByVal uExitCode 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 PROCESS_TERMINATE = &H1
Private Const PROCESS_CREATE_THREAD = &H2
Private Const PROCESS_VM_OPERATION = &H8
Private Const PROCESS_VM_READ = &H10
Private Const PROCESS_VM_WRITE = &H20
Private Const PROCESS_DUP_HANDLE = &H40
Private Const PROCESS_CREATE_PROCESS = &H80
Private Const PROCESS_SET_QUOTA = &H100
Private Const PROCESS_SET_INFORMATION = &H200
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const SYNCHRONIZE = &H100000
Private Const PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF

Private Const WM_CLOSE As Long = &H10

'Functions - - -

Private Function Detect(Program As String) As Boolean
On Error Resume Next
Dim hSnapShot As Long, uProceso As PROCESSENTRY32
Dim res As Long, dwIDProceso As Long
Dim hProceso As Long, Resultado As Long

dwIDProceso = -1
hSnapShot = CreateToolhelpSnapshot(2&, 0&)
If hSnapShot <> 0 Then
uProceso.dwSize = Len(uProceso)
res = ProcessFirst(hSnapShot, uProceso)
Do While res
'Debug.Print Left$(LCase(uProceso.szExeFile), InStr(LCase(uProceso.szExeFile), Chr$(0)) - 1)
If Left$(LCase(uProceso.szExeFile), InStr(LCase(uProceso.szExeFile), Chr$(0)) - 1) = LCase(Program) Then
dwIDProceso = uProceso.th32ProcessID
Exit Do
End If
res = ProcessNext(hSnapShot, uProceso)
Loop
Call CloseHandle(hSnapShot)
End If

If dwIDProceso = -1 Then
Detect = False
Else
Detect = True
End If

End Function

Function CloseApp(Program As String) As Boolean
On Error Resume Next
Dim hSnapShot As Long, uProceso As PROCESSENTRY32
Dim res As Long, dwIDProceso As Long
Dim hProceso As Long, Resultado As Long

dwIDProceso = -1
hSnapShot = CreateToolhelpSnapshot(2&, 0&)
If hSnapShot <> 0 Then
uProceso.dwSize = Len(uProceso)
res = ProcessFirst(hSnapShot, uProceso)
Do While res
'Debug.Print Left$(LCase(uProceso.szExeFile), InStr(LCase(uProceso.szExeFile), Chr$(0)) - 1)
If Left$(LCase(uProceso.szExeFile), InStr(LCase(uProceso.szExeFile), Chr$(0)) - 1) = LCase(Program) Then
dwIDProceso = uProceso.th32ProcessID
Exit Do
End If
res = ProcessNext(hSnapShot, uProceso)
Loop
Call CloseHandle(hSnapShot)
End If

If dwIDProceso = -1 Then
CloseApp = False
Else
hProceso = OpenProcess(PROCESS_TERMINATE, True, dwIDProceso)
'Debug.Print hProceso
'Debug.Print uProceso.dwFlags
Resultado = TerminateProcess(hProceso, 9)
CloseHandle hProceso
If Resultado <> 0 Then
CloseApp = True
Else
CloseApp = False
End If
End If

End Function

Public Sub Test1()

'For test this functions,example:

If Detect("test.exe")=true then
call closeapp("test.exe")
End If

End Sub
This code is made in notepad (in my memory), if i dont remember or not works ok tell me ;)

Bye! ^^
 
Last edited:

mazz

Dedicated Member
Dedicated Member
Aug 6, 2008
193
2
65
If you don't like complicate your life more put a sendkeys and a little sleep after calling kill process, but if your intention is kill the process immediately try to use shell (Fast and simply) or (extended) api for memory management.

This depends of the language you are programing the function, vb6 have a little limitations need to crack using the cracked vb 6.5 version accepts vb6 + asm code (the best) and in vb6 you have more examples over the www, vb.net is a little more easy but different but not have an expensive examples in www, c++ is more customizable and have a great examples in the Internet.

The methods or calls are the same (API) but the options and other are limited by language.


VB6 Code for detecting and closing app inmediately using api and using
the exe name:
This code is made in notepad (in my memory), if i dont remember or not works ok tell me ;)

Bye! ^^


honestly its works great omg omg you make it look so easy
I wish I understood more of it guess ill have to take a deeper look at these API ty very much :brows:
 

TheBoss

Golden Oldie
Golden Oldie
Sep 6, 2004
1,791
4
145
Middlesbrough
honestly its works great omg omg you make it look so easy
I wish I understood more of it guess ill have to take a deeper look at these API ty very much :brows:

any chance of a few screenies, ? I need a server starter .
 

mazz

Dedicated Member
Dedicated Member
Aug 6, 2008
193
2
65
K that worked great but now im running into another problem i used the shell command to excute my exes and now every time a server exe launches it says it cant find the script directorys Does the shell command open exe in a temp directory?
 

Hazuki

VIP
VIP
Apr 14, 2004
3,459
38
285
United Kingdom
You need to make sure you use full paths for the server apps, not just /logingate.exe for exmaple.

(If you cant figure it out I'll post my launcher scripts later :).)

/Haz
 

mazz

Dedicated Member
Dedicated Member
Aug 6, 2008
193
2
65
Sweet that would nice ty
Cause the way i have it set up it point to all directorys
txtLaunch1 = C:\MirServer\M2Server\M2Server.exe
shell txtLaunch1 , vbnormalfocus

Or did you mean I have to point to the script files first?