PDA

View Full Version : Function: FindAndCloseProcessByName


Brett
10-07-2004, 12:32 PM
Function: FindAndCloseProcessByName
Last Revision: October 7, 2004 (001)

Information
This function can be used to find a process by it's filename and then close it by sending it a close message.

To use this function in your Setup Factory 7.0 projects, simply copy and paste it into your Global Functions. You can get there by selecting Resources > Global Functions from the main menu.


function FindAndCloseProcessByName(strName)
local tblProcesses = Window.EnumerateProcesses();
local bProcessFound = false;
local nProcessHandle = nil;
if(tblProcesses)then
local strProcessName;
local nHandle;
for nHandle, strProcessName in tblProcesses do
if(String.Find(strProcessName,strName,1,false) ~= -1)then
nProcessHandle = nHandle;
bProcessFound = true;
end
end
end

if(bProcessFound and nProcessHandle)then
Window.Close(nProcessHandle,CLOSEWND_SENDMESSAGE);
end
end


To call the function from your project, try copying and pasting the following script onto an action tab, such as the On Startup event. You can get there by choosing Project > Actions.

-- Close Internet Explorer if running
FindAndCloseProcessByName("iexplore.exe");


Function Name: FindAndCloseProcessByName
Type: Setup Factory 7.0 Function
Created By: Brett Kapilik

Comments? Please post them here.

Frackounet
03-12-2005, 09:09 PM
Here the code for programs like MSN Messenser, the CLOSEWND_SENDMESSAGE don't close completly the program.
Change it in CLOSEWND_TERMINATE

Information
This function can be used to find a process by it's filename and then close it by sending it a close message.

To use this function in your Setup Factory 7.0 projects, simply copy and paste it into your Global Functions. You can get there by selecting Resources > Global Functions from the main menu.



function FindAndCloseProcessByName(strName)
local tblProcesses = Window.EnumerateProcesses();
local bProcessFound = false;
local nProcessHandle = nil;
if(tblProcesses)then
local strProcessName;
local nHandle;
for nHandle, strProcessName in tblProcesses do
if(String.Find(strProcessName,strName,1,false) ~= -1)then
nProcessHandle = nHandle;
bProcessFound = true;
end
end
end

if(bProcessFound and nProcessHandle)then
Window.Close(nProcessHandle,CLOSEWND_TERMINATE);
end
end



To call the function from your project, try copying and pasting the following script onto an action tab, such as the On Startup event. You can get there by choosing Project > Actions.



-- Close MSN Messenger if running
FindAndCloseProcessByName("msnmsgr.exe");

Intrigued
03-12-2005, 10:19 PM
Here is a footnote to remember though and it may or may not affect said application being terminated (closed):

"Immediately terminate the program's process. The state of global data maintained by the program's dynamic-link libraries (DLLs) may be compromised if this option is used. (When this option is selected, the AutoPlay application calls the Windows API function "TerminateProcess" to immediately terminate the specified program.)"

and, this was interesting to find in another area:

"Tip: Pro Edition users can use Window.Close(Application.GetWndHandle(), CLOSEWND_TERMINATE) in place of Application.Exit(). This will close the window before the window draws itself (i.e. the user will see nothing)."

ddstrg
12-11-2005, 08:05 PM
Very good,
Thanks you!

TuanKTCDCN
12-22-2005, 07:45 AM
Help me to create a function FindAndCloseProcessByClass(strClass)
Thanks a lot!

Staggan
06-06-2006, 03:12 AM
This function is extremely useful...

How can I get it to close ALL instances of explorer though if the user has multiple browsers open ?

It works fine for single windows... but any more than 1 and it leaves the rest open...

Thanks

Staggan
06-06-2006, 10:08 AM
Its ok, I resolved this issue.

Thanks

Adam
06-07-2006, 11:07 AM
Good to hear.

Adam Kapilik

kymaita
02-06-2007, 10:26 AM
Hi

I'm testing TrueUpdate software and I'm just in this Step.

I want to close my application if it is running at the moment an update is available and user press a next button after accepting the license agreement.

I tried this code ( FindAndCloseProcessByName) with a sample windows form application made in .Net 2.0 and it works well but my application is like Messenger application run minimized in system try . One feature of my application is that it has timers running in the background and those timers stop the application for closing; maybe because those timers run in separate threads. I need to use a more powerful instruction to dispose the application and all threads associated to it.

What can I do?

Kenny

pww
06-21-2007, 01:28 PM
cool, but closes only one instance of the app.
In case it's possible more than one instance of the app to be opened (as is the case for most apps), should be something like this


function FindAndCloseProcessByName(strName)
local tblProcesses = Window.EnumerateProcesses();
if(tblProcesses)then
local strProcessName;
local nHandle;
for nHandle, strProcessName in tblProcesses do
if(String.Find(strProcessName,strName,1,false) ~= -1)then
Window.Close(nHandle,CLOSEWND_SENDMESSAGE);
end;
end;
end;
end;

kk250040
01-08-2009, 06:50 AM
Hello friends,

What if i need to detect a batch file running. I can check for the "cmd.exe", but there are chances that the user opens his own cmd.exe for some other purposes. :huh
Is there any other way of detecting a batch file running. My batch file consists of scripts to take backup of the host machine. But when i tried to detect the "xcopy.exe" for the copy command in the bacth file, its not being detected though the list of processes in task manager show the xcopy.exe in the list. So how do i detect this kind of a bacth file running...

Please suggest me how i can do it...

Thanks,
Kusuma

DanCooperstock
10-18-2009, 09:06 AM
Here's a version of the code above, that is for checking whether the application being installed is currently running (rather than the installer), since that is likely to cause a lot of files to be unable to be installed. It confirms whether it's OK to stop the app.

-- Look for an existing instance of strName, ask whether it's
-- OK to close it, return true if there was no instance or we
-- closed it, false otherwise
function FindAndCloseDonation(strName)
local tblProcesses;
local bProcessFound = true;

while bProcessFound do
tblProcesses = Window.EnumerateProcesses();
bProcessFound = false;
if (tblProcesses) then
local strProcessName;
local nHandle;
for nHandle, strProcessName in tblProcesses do
if (String.Find(strProcessName,strName) ~= -1) then
if Dialog.Message("Already Running", "Your application is currently running, and the installation cannot complete while it is running.\n\nIs it OK for the application to be closed, before continuing this installation?",
MB_YESNO, MB_INCONQUESTION) == IDYES then
bProcessFound = true;
Window.Close(nHandle, CLOSEWND_TERMINATE);
Application.Sleep(1000);
else
return false;
end
end
end
end
end

return true -- not found, or successfull closed
end

This would likely be called in one of your Next button actions, and the installer would be exited if it returned false.