PDA

View Full Version : Killing an instance


Bruce
10-31-2008, 06:19 PM
I need to kill a PowerPoint.exe/instance any ideas?
ie. function TerminatePowePoints()

RizlaUK
10-31-2008, 06:55 PM
is there any reason why the below code wont work with powerpoint ?

instances_of_file = 0;
file_to_check_for = "powerpoint.exe"; --have all lowercase
processes = System.EnumerateProcesses();

for j, file_path in processes do
file = String.SplitPath(file_path);
if (String.Lower(file.Filename..file.Extension)) == file_to_check_for then
System.TerminateProcess(j);
end
end

or is it that you just want to kill 1 of many running instances

Bruce
10-31-2008, 07:01 PM
is there any reason why the below code wont work with powerpoint ?

instances_of_file = 0;
file_to_check_for = "powerpoint.exe"; --have all lowercase
processes = System.EnumerateProcesses();

for j, file_path in processes do
file = String.SplitPath(file_path);
if (String.Lower(file.Filename..file.Extension)) == file_to_check_for then
System.TerminateProcess(j);
end
end

or is it that you just want to kill 1 of many running instances

I've seen this code RizlaUK good find, I have run into another problem sence I posted dealing w/ the timer event which is where this code belongs. I'll update after I speak w/ Tigg.
Oh BTY I wnat to kill any instances that pop-up as the end user is taking a test. Thanks for your reply.

RizlaUK
10-31-2008, 07:50 PM
just place that code in the timer, set the timer for 1000 and it will check for all instances of powerpoint.exe every second, any cheaters will have to be real fast readers!

Bruce
11-02-2008, 06:06 PM
Ok I have this on timer:

instances_of_file = 0;
file_to_check_for = "pptview.exe";
processes = System.EnumerateProcesses();

for j, file_path in processes do
file = String.SplitPath(file_path);
if (String.Lower(file.Filename..file.Extension)) == file_to_check_for then
System.TerminateProcess(j);
end
end

And the timer set for:

Page.StartTimer(1000);

NOT working :huh

Unless the system sees it as something else.

OK I found what it's really called: file_to_check_for = "POWERPNT.EXE";
the red below I think is causing an issue

if (String.Lower(file.Filename..file.Extension)) == file_to_check_for then

Ideas?

Bruce
11-02-2008, 06:22 PM
errrrr "Upper"

Bruce
11-02-2008, 07:15 PM
Funny, this will not stop another instance of the program if it's running from the same CD, file_to_check_for = "POWERPNT.EXE"; or file_to_check_for = "autorun.exe";. Not good

qwerty
11-02-2008, 09:11 PM
as pointed out you need to be 100% correct in case sensitivity, best to use the code as supplied and make sure your file name is in lowercase also, i'd make it check for both examples of the file name... with and without the file type ;)

as for your last post, you have a stray ; in the middle of your script ;)

Bruce
11-02-2008, 09:17 PM
also, i'd make it check for both examples of the file name... with and without the file type ;)

as for your last post, you have a stray ; in the middle of your script ;)

"with and without the file type" explane
"you have a stray ;"
Where?

qwerty
11-03-2008, 03:20 AM
ok, my bad.... i see you were using the OR as it normally is in speach/typing, i thought that line you typed was part of your code, i get it now that you meant

file_to_check_for = "POWERPNT.EXE";

or

file_to_check_for = "autorun.exe";

:p


for the other thing, in the script you have it sorting out the filename and extension, joining the 2 texts together prior to comparing with your file to check for, i tend to do it this way just to cover the bases....
if (String.Lower(file.Filename..file.Extension)) == "POWERPNT.EXE" or (String.Lower(file.Filename)) == "POWERPNT" then -- must be in lowercase here

in theory, this will allow you to trigger the same action code after the "then" for 2 different filenames, one compared using the filename+extension and one just by filename

TJ_Tigger
11-03-2008, 07:43 AM
This worked for me

--Define a table of application names you don't want running during this time
--This table includes powerpoint and the powerpoint viewer.
tbfiles_to_check_for = {"powerpnt.exe","pptview.exe","powerpoint.exe"}; --have all lowercase

--Enumerate the system processes
processes = System.EnumerateProcesses();

--loop through the table looking at each process
for j, file_path in processes do
--Split the filepath into a table
file = String.SplitPath(file_path);
--Combine the filename and extension so we can compare
-- with the table of processes we defined above
strFnExt = String.Lower(file.Filename..file.Extension);
--loop through the table containing the filenames for us to check
for i,filenamecheck in tbfiles_to_check_for do
--compare each entry against the process name plus extension
if strFnExt == filenamecheck then
--if there is a match then terminate that process
System.TerminateProcess(j);
end
end
end

Bruce
11-03-2008, 09:26 AM
WOW, I'll try it out! Thx Tigg. :yes

drgfx
01-19-2009, 06:34 PM
I am also trying to kill an instance, I am able to (System.TerminateProcess) but for some reason the taskbar tray icon remains until I move the mouse over it then it disappears as it should.
Is there anyway to make the icon disappear on its own?

Thanks.

----------------------
Douglas Greaser
DRG EFFECTS (http://www.drgfx.com)

Bags
01-20-2009, 09:00 AM
I am also trying to kill an instance, I am able to (System.TerminateProcess) but for some reason the taskbar tray icon remains until I move the mouse over it then it disappears as it should.
Is there anyway to make the icon disappear on its own?

Thanks.

----------------------
Douglas Greaser
DRG EFFECTS (http://www.drgfx.com)

Because you are 'terminating' a process, that process is not "allowed" so to speak, to enter any possible shutdown sequences it may have. In this case the terminated application is not allowed to release the trayicon from the system tray. For this reason it is generally better to ASK the application to shutdown rather then terminate it. But if the application is needing to be terminated then of course terminate it.

This sends a request to the application to shut down, it is a polite way to close a running program because you simply say "Hey mr. program, will you close please?"
Window.Close(application_handle, CLOSEWND_SENDMESSAGE)

This is a rude way to close an application. It's like saying "die die DIIIEEE!!" and not giving the application a chance to respond. It is the same when working with System.TerminateProcess().
Window.Close(application_handle, CLOSEWND_TERMINATE);

The biggest difference between the 2 ways of terminating an application [System.TerminateProcess() & Window.Close(TERMINATE)] is that System.TerminateProcess() uses the PID (Process ID) and Window.Close() uses window handles. In essence the two do the same thing, they both impolitely close a program.

There are things to think about though when you want to close an application.
For instance:
Is my closing this program a priority before it can carry out any extra orders?
Does this application need to be politely shut down so that any changes to it can be properly saved, and it can carry out any freeing of resources it might need to do.

Or things like that. In your case to allow the application to properly shutdown and release the system tray icon you should politely ask it to close. However, you need to find the window handle of the application which is not the same as the ProcessID required for System.TerminateProcess()

The example for the System.TerminateProcess() in AMS7's help will work just fine for enumerating the windows and closing the one you want, you just have to change to using the equivalent Window functions in place of the System functions.

An example of what I mean:
local String, Window = String, Window; -- speed up these functions by making them local to this chunk
local file_to_check_for = "autorun.exe"; --have all lowercase
local processes = Window.EnumerateProcesses();

for wnd_handle, file_path in processes do
local file = String.SplitPath(file_path);
if (String.Lower(file.Filename..file.Extension)) == file_to_check_for then
Window.Close(wnd_handle, CLOSEWND_TERMINATE);
Window.Close(wnd_handle, CLOSEWND_SENDMESSAGE);
end
end


So basically, unless the program you want to shut down is a threat or just absolutely needs to be terminated, you should ask it to close. And if you find that asking it to close doesn't close it then of course you need to die die DIEEE the sucker!

drgfx
01-21-2009, 01:55 AM
Thank you that is an excellent description!

The app I'm "killing" is minimized to the system tray, and is why I have to terminate it.
Just tried your example to make sure it didn't fix the problem.
I tried many ways/ideas when I originally set it up, including maximizing it to close.
Finally I had to use the following code :


instances_of_file = 0;
file_to_check_for = "alert.exe"; --have all lowercase
processes = System.EnumerateProcesses();

for j, file_path in processes do
file = String.SplitPath(file_path);
if (String.Lower(file.Filename..file.Extension)) == file_to_check_for then
System.TerminateProcess(j);
end
end

Only problem is that darn icon hangs in the taskbar tray after the termination. : (
Was thinking of using : SendTo(nSendHWnd, "HWND", and sending a close command to the app that way.
Just haven't got around to trying it yet..

Thanks for the reply.

----------------------
Douglas Greaser
DRG EFFECTS (http://drgfx.com)

drgfx
01-23-2009, 02:22 AM
Thanks to WORM, this post includes in the example exactly what I needed. Running two Autoplay applications, and needed to kill the other app instantly with Exit of 1st one with no remnants like a leftover icon in the system taskbar like when you kill it. Even if the application/program you want to kill is minimized and set off screen - (SendTo(nRecHWnd, "QUIT")) works like a charm!

Thank you worm. : )

http://www.indigorose.com/forums/showthread.php?t=15551

----------------------
Douglas Greaser
DRG EFFECTS (http://drgfx.com)