PDA

Click to See Complete Forum and Search --> : force a program to close


asabi
Jun 26th, 2000, 04:35 AM
I need to force a program to kill itself
I already have the way to get the handle of it, but from some reason the program will resist the

SC_CLOSE command.

is there any other way to kill it appart from rebooting the machine ???

kedaman
Jun 26th, 2000, 06:43 PM
You could send a WM_CLOSE message with postmessage api

PostMessage appwnd&, WM_CLOSE, 0, 0

Jun 26th, 2000, 10:28 PM
I believe the CloseHandle API should work as well.

asabi
Jun 26th, 2000, 10:35 PM
I will try both (I am in the proccess of trying the first..

Someone told me that nothing would work because this game is using DirectX drivers, so It will not "listen" to the commands from windows, is that true ???

if it is true, than I need to find a way to crash the application, how do you send ALL the windows commands in a loop (open,close,minimize,maximize ...) this might crash it eventually ? what is the range of numbers I should put in the loop (I don't want to crash the whole machine).

asabi
Jun 26th, 2000, 11:06 PM
NOTHING absulutly NOTHING

this program really takes over ...

I will probably need to crash the whole machine before getting rid of it.

Any suggestions from anybody ??

kedaman
Jun 27th, 2000, 02:45 AM
You could try these, i haven't used them but they might be usefull

Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
'I have never used these but

asabi
Jun 27th, 2000, 02:58 AM
Thanks, I will try

The only way I could kill it was with causing a blue screen, and than again, the program would still continue (all screwed up , but still running) it is like a giant that can't be killed..

The whole machine was totally screwed up, but this was going on ...

I just wrote a C++ program that would REALLY crash the machine (allocating endless memorry, never use it, and than allocating more ... 100000000 times)
so I will try this as well...


Again, I will try your code though.


thanks

Supera
Jun 29th, 2000, 12:33 AM
" How can I close an application?"
You can use the FindWindow and PostMessage API functions to find a window and then close it. This example show how you can close down a Window with a caption of "Calculator".
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "Calculator")
Debug.Print winHwnd
If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox "Error posting message."
End If
Else
MsgBox "The Calculator is not open."
End If
Before this,you have to declare the API functions in a module in your project.
Declarations : ( PostMessage and FindWindow )

Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Good Luck Asabi.
Regards,Supera.

asabi
Jun 29th, 2000, 12:44 AM
Well, the thing is that this program is using directX so it won't close with this command (I have tried), infact, I sent a command to shut off windows (the operating system), and this program was still running !, just when I quit this program THAN the whole thing would reboot.

I found my way ...
I found an EXE file that would crash the whole machine ..., that's the only way I could find.

Thank you

MasterMind
Jul 1st, 2000, 11:18 PM
I need to know how to close a program that is runing?

How do I do this?

If possible can someone send me a demo of how the code works?

My E-Mail: Morph105050@aol.com

any information is helpfull to me.... Thanks

~Joe

Jul 2nd, 2000, 10:39 AM
You can close an Already running App like this. Put the following code into a Module.


Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H10


Now put this Code into a CommandButton

Private Sub Command1_Click()

' Get the Handle of Calculator
chWnd = FindWindow(CLng(0), "Calculator")
' Send a Message to close it
SendMessage chWnd, WM_CLOSE, CLng(0), CLng(0)

End Sub


This code will Close Calculator. Before you run the code, make sure that you have Calculator running. If you want, you can modify the code to suit yourself by replacing the "Calculator" with another Application's Title.

Jul 5th, 2000, 06:33 PM
I personally like this way:


Const MAX_PATH& = 260
Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

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 Type

Public Function KillApp(myName As String) As Boolean

Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim i As Integer
On Local Error Goto Finish
appCount = 0

Const TH32CS_SNAPPROCESS As Long = 2&

uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)


Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))


If Right$(szExename, Len(myName)) = LCase$(myName) Then
KillApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Finish:
End Function

asabi
Jul 6th, 2000, 02:17 PM
what is myName (in the call for the function)?


Is it the window caption ?

Jul 6th, 2000, 05:13 PM
It is the EXE ("C:\myapp\app.exe").

If you would like an project sample instead. Here is the link: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=4377

[Edited by Matthew Gates on 07-06-2000 at 06:16 PM]

V(ery) Basic
Jul 8th, 2000, 11:17 AM
You might try to use the DestroyWindow(TheThingYouWantToDestroy.hwnd)
API?

That closes the window but doesn't end the prog.

But what the hell do I know?

asabi
Jul 8th, 2000, 11:49 AM
Well, I have already used Matthew Gates method, and it did work (everything else never worked) this program does not really work in a window, and it is using the direct X libarary so it doesn't really listens to the windows commands (even killing explorer just hanged the machine, but not this program.

Now it works though (with killing the proccess).


Thank you :-)