Announcement

Collapse
No announcement yet.

Function: FindAndCloseProcessByName

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Function: FindAndCloseProcessByName

    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.

    Code:
    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.
    Code:
    -- 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.

  • #2
    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.

    Code:
    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.

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

    Comment


    • #3
      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)."
      Intrigued

      Comment


      • #4
        Very good,
        Thanks you!

        Comment


        • #5
          FindAndCloseProcessByClass?

          Help me to create a function FindAndCloseProcessByClass(strClass)
          Thanks a lot!

          Comment


          • #6
            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

            Comment


            • #7
              Its ok, I resolved this issue.

              Thanks

              Comment


              • #8
                Good to hear.

                Adam Kapilik

                Comment


                • #9
                  closing which seems Multithread application

                  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

                  Comment


                  • #10
                    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

                    Code:
                    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;

                    Comment


                    • #11
                      Detect a batch file running

                      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.
                      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

                      Comment


                      • #12
                        Close application with confirmation

                        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.

                        Code:
                        -- 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.
                        Dan Cooperstock [email protected]
                        416-423-7722

                        Comment


                        • #13
                          I was using a more polite method of first detecting if the app is running and then popping a warning asking to close the app before proceeding.

                          But I have just noticed that there is a problem... although the action script has popped the warning and the uninstaller exits, Add Remove programs still removes some entries which cause the next attempt to fail. How this happens is that while the pop up is waiting for acknowledgment by clicking ok, the Anti-Virus program has already been asked for permission to let the uninstaller run. By approving uninstall in the AV prompt the uninstall gets mangled.

                          Ok, so who would click ok for AV to allow uninstall when they have already been warned about closing the app?

                          Just about everyone :-(

                          -- These actions are performed before the screen is shown.
                          instances_of_file = 0;
                          -- List of browsers to check - must be all lowercase.
                          file_to_check_for_0 = "chrome.exe";
                          file_to_check_for_1 = "firefox.exe";
                          file_to_check_for_2 = "iexplore.exe";
                          file_to_check_for_3 = "mozilla.exe";
                          file_to_check_for_4 = "netscape.exe";
                          file_to_check_for_5 = "opera.exe";
                          file_to_check_for_6 = "safari.exe";
                          -- Check if browser is open.
                          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_2 then
                          Dialog.Message("Warning","Close all Internet Explorer browser windows before continuing!");
                          Application.Exit(0);
                          end
                          end

                          So I am looking for a better solution.
                          ArtistScope - Copy Protection & DRM Software

                          Comment


                          • #14
                            What's the difference bewteen...

                            Window.EnumerateProcesses()

                            and

                            System.EnumerateProcesses()?
                            ArtistScope - Copy Protection & DRM Software

                            Comment


                            • #15
                              With Setup Factory 9.0 you must replace
                              Code:
                              for nHandle, strProcessName in tblProcesses do
                              with
                              Code:
                              for nHandle, strProcessName in pairs(tblProcesses) do

                              Comment

                              Working...
                              X