Close process

Blitz3D Forums/Blitz3D Programming/Close process

Baystep Productions(Posted 2006) [#1]
Alrighty. I have a delema here.

I've executed an application...
Now how do I close it?

I've tried using the "RunPRogram" dll in the toolbox, and tried sending its process handle a "WM_CLOSE" command with the api's SendMessage function but I get a MAV. Then I tried ExecFile and then FindWindow(0,"AppTitle") and then using SendMessage, and PostMessage but it doesn't work.

What am I doing wrong?


b32(Posted 2006) [#2]
It could be FindWindow. Are you using the actual Window's name or just "apptitle" ? And how is FindWindow declared in the .decls file ? For using it with a zero as a classname it should be:
apiFindWindow% (lpClassName%, lpWindowName$) : "FindWindowA"
(with a % instead of a $)
I haven't used it, but if you have the user32.decls, you could give "api_CloseWindow" a try.


John Blackledge(Posted 2006) [#3]
Yes, this could be really useful if someone can nail it down. Kev? Danny? (John crouches behind a rock, and watches;-)


kevin8084(Posted 2006) [#4]
use this, instead:
api_DefWindowProc(SystemProperty("AppHwnd"),$10,0,0)

Just don't use it with debug enabled in your IDE, as it will just terminate your program but not the Blitz Debugger. Just for a little information, this function calls the default window procedure with the message 0x10 (WM_QUIT)


b32(Posted 2006) [#5]
Ehm, the * should be a % .. sorry about that


Baystep Productions(Posted 2006) [#6]
Okay, no I didn't use "appTitle"

I tired a couple methods but I lean to this one...
ExecFile("calc.exe")
Global exec=usr_FindWindow(0,"Calculator")
If Not exec RuntimeError "ERROR!"


And then I use this to terminate
usr_PostMessage(exec,WM_CLOSE,0,0)


And I have tried using closewindow, sendmessage, etc.
I like the sound of this command because it let's the program close on it's own instead of forcing it. And I use the user32.decls from the code archive so yes it's that one with a 0 as a class.


Picklesworth(Posted 2006) [#7]
Woah, you used my DLL.
I'd better help you :)

The value returned by the RunProgram function is actually a process handle, which is completely different from a window handle.
(And not usable with window commands).

If you want, it may be possible to edit the source code of that DLL to acquire the handle of the executed program, and then use that to send window messages.

However, I believe there is actually a command to end a process somewhere - maybe in kernel32 - which would close it for you.
Of course, that's a very forceful close so not recommended.
I'll try to look this up for you...

There are a lot of process functions, so a few searches at MSDN should bring up good results.


Edit:
I've done a lot of explaining today, so I'm a bit fatigued... not even in sorting mode, actually, so this could be messy. Hope it helps though. I'll stick to the functions that can generally be used straight through B3d using the process handle returned by RunProgram:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecoreos5/html/wce50lrfexitprocess.asp
Function to end a process. I'm not sure how clean it is; could be instant process killing, which isn't very safe.
Mind, this is from the Windows CE API for some reason. Probably the same in regular desktop windows.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/process_and_thread_functions.asp
List of process functions, which Blitz3d can hopefully use directly.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/child_processes.asp
All about processes.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getguiresources.asp
I'm still trying to comprehend this one, but it seems to be (part of?) a way to find windows created by a process, which is extremely useful.
Otherwise, it's just a way to count how many windows a process has, which seems quite pointless.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/terminating_a_process.asp
General info on terminating a process.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/obtaining_additional_process_information.asp
How to obtain additional process information.


By the way, a lot of these functions can be found in kernel32.decls or user32.decls.
There are a few others, too...


Baystep Productions(Posted 2006) [#8]
Yeah I figured it didn't return win handles, I'd love to adjust the code but I never much got the hang of WinAPI with c++. So I use blitz.

I've been up and down the MSDN site, but I can't help but wonder why the findwindow command doesn't return anything.

Thanks for your input.


b32(Posted 2006) [#9]
By the time the calculator is executed, the handle is allready read and returned zero, so use a while..wend to wait for the calculator to appear.
;------------------------decls
;.lib "user32.dll"
;api_FindWindow% (lpClassName%, lpWindowName$) : "FindWindowA"
;api_CloseWindow% (hwnd%) : "CloseWindow"
;------------------------------------------------------

ExecFile "Calc.exe"

find = 0
While find = 0
	find = api_FindWindow(0, "Calculator")
Wend

Input "Press enter to close"

api_CloseWindow(find)

RuntimeError "Ok!"



Baystep Productions(Posted 2006) [#10]
Still doesn't work. Nice try tho. I had to put a timer in it to tell.


b32(Posted 2006) [#11]
Okay, you were right, closewindow only minimizes the window. So again, a new attempt:
I've added some delay and safety so the computer doesn't hang when nothing is found:



Baystep Productions(Posted 2006) [#12]
Nope still doesn't work. It can't find the window. Did this work for you?


b32(Posted 2006) [#13]
Yes, I tested it before posting it. On my machine, to get FindWindow to work without a classname, I needed to replace the definition in user32.decls. lpClassName$ should be lpClassname%. Since I am using the one from the code archives too, I thought this might be the problem.
Have you tried filling in the classname (the definition should be a "$" again) ? I don't know the classname for the calculator, but maybe you could use a compiled blitz file. I believe the classname is something like 'blitz runtime class'.


Baystep Productions(Posted 2006) [#14]
usr_FindWindow% (lpClassName$, lpWindowName$) : "FindWindowA"


That's what it is for me, I'll try switching it to %.

EDIT: AHH!! IT WORKS! Thank you very much!