How to terminate a process

This is a discussion on How to terminate a process within the Windows Programming forums, part of the Platform Specific Boards category; I've wrote that (for terminate calc.exe) .... Code: #include <stdio.h> #include <stdlib.h> #include <windows.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    How to terminate a process

    I've wrote that (for terminate calc.exe) ....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char * lpCmdLine, int  nCmdShow)
    
    {
    
     HWND hWnd; 
         DWORD procId; 
         HANDLE hProc; 
     
         if ( (hWnd = FindWindow ("SciCalc", "Calcolatrice")) && 
              GetWindowThreadProcessId(hWnd, &procId)) 
         { 
     
              if ( hProc = OpenProcess (PROCESS_TERMINATE, FALSE, procId) ) 
              { 
                   if ( TerminateProcess(hProc, 0) ) 
                        MessageBox (NULL, "CLOSED", NULL, 0); 
                   else 
                        MessageBox (NULL, "NOT CLOSED", NULL, 0); 
     
                   CloseHandle (hProc); 
              } else 
                   MessageBox (NULL, "I CANT CLOSE", NULL, 0); 
         } else 
              MessageBox (NULL, "THE PROCESS WAS NOT FOUND", NULL, 0); 
    
    
    
      
      return 0;
    }
    and it works !

    now, 2 questions :

    how can i find what to put in ("SciCalc", "Calcolatrice") ?? I put that coz i found it in a site but i want to have the 2 identificator for every exe that i want to close.

    if i dont know anything but the dimension of the file ( of the exe ) can i terminate all the exe running that has that dimension?

    thanx

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >>how can i find what to put in ("SciCalc", "Calcolatrice")<<

    The first is the window class. You can find the window class of a window with a tool. Visual Studio includes Spy++. Otherwise you can get WinSpy from Catch22. The second argument is the window caption.

    Both of these are subject to change. For example the caption, on the english version of windows is "Calculator". Therefore, this is not a reliable way of terminating a process.

    >>if i dont know anything but the dimension of the file ( of the exe ) can i terminate all the exe running that has that dimension?<<

    I assume by dimension you mean the file size of the exe. Yes, this can be done. You need to enumerate the running programs and then get the file size of each executable.

    http://www.google.com/search?q=enume...ning+processes

    Attached is an old project of mine that terminates processes by executable name. It may help you.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thank u!!

    ok, the program I want to shut down is seti@home.. that program starts a dos windows. If I try to see it's CLASS with WinSpy, the program seti@home crushes and the WinSpy cant see the class

    And what about to determinate the caption?

    Ayway I will try to enumerate the process running and look for theyre size, too bad I cant find some nice C examples all for vb delphi and so on

    thanx

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >>ok, the program I want to shut down is seti@home.. that program starts a dos windows.<<

    You can't use the method that you posted on a console program as a console program does not have a window.

    The best method is to use the executable name. For example, to shutdown "seti.exe" with the program I posted:
    Code:
    KillProcess seti.exe 1500
    >>too bad I cant find some nice C examples<<

    The code I posted contains process enumeration code in C.

    EDIT:
    Their is a command line utility in XP, "taskkill" that can shutdown a process based on various arguments. Type "taskkill /?" in the console for more information.

    If "seti@home" is a windows service, you need to use another method to shut it down.
    Last edited by anonytmouse; 03-17-2004 at 10:37 AM.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    yes I saw
    And ur help is really appreciated !!

    But I was talking about examples on how to kill by file size... because every one rename the seti client and I wanted to do an automatic tool

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Well, the code I posted allows you to enumerate process ids. Form these, you must get the exe path and then the file size with GetFileSize().

    Replace:
    Code:
    		if (_tcsicmp( pe32.szExeFile, exeFile) == 0) {
    			bRet = KillProcess( pe32.th32ProcessID, TRUE, (DWORD) lngTimeout );
    			//break;
    		}
    with:
    Code:
    		TCHAR szPath[MAX_PATH];
    
    		If (GetProcessPath(&pe32, szPath) &&
    		    CheckFileSize(szPath))
    		{
    			bRet = KillProcess(pe32.th32ProcessID, TRUE, (DWORD) lngTimeout);
    		}
    Here is the GetProcessPath function:
    Code:
    BOOL GetProcessPath(PROCESSENTRY32 * pe32, LPTSTR szPath)
    {
    	HANDLE hModules;
    	MODULEENTRY32 me32 = { 0 };
    	BOOL bResult;
    
    	hModules    = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pe32->th32ProcessID);
    	me32.dwSize = sizeof(me32);
    	szPath[0]   = '\0';
    
    	if (hModules && Module32First(hModules, &me32)
    	{
    		while (TRUE)
    		{
    			if (stricmp(pe32->szExeFile, me32.szModule) == 0 ||
    			    stricmp("",              me32.szModule) == 0)
    			{
    				lstrcpy(szPath, me32.szExePath);
    				break;
    			}
    
    			if (!Module32Next(hModules, &me32)) break;
    		}
    	}
    
    	if (hModules) CloseHandle(hModules);
    
    	return (szPath[0] != '\0');
    }
    You must provide the CheckFileSize function. It should look something like:
    Code:
    BOOL CheckFileSize(LPCTSTR szPath)
    {
    	HANDLE hFile = CreateFile(szPath, ...);
    	DWORD cbFileSize = GetFileSize(hFile, NULL);
    
    	return (cbFileSize == SETI_SIZE);
    }
    Note: I'm assuming you are running 2000 or XP.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thank u very much I will surely try my best

    a question (first of a lot)

    when i try to compile you nice kill program, dev cpp says :


    [Linker error] undefined reference to `Process32FirstW@8'
    [Linker error] undefined reference to `Process32NextW@8'
    C:\Programmi\Dev-Cpp\Termina da dimensione\Makefile.win
    [Build Error] ["Termina] Error 1


    I've included kernel32 library.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    [Linker error] undefined reference to `Process32FirstW@8'
    [Linker error] undefined reference to `Process32NextW@8'
    C:\Programmi\Dev-Cpp\Termina da dimensione\Makefile.win
    [Build Error] ["Termina] Error 1

    I've included kernel32 library.
    These exports must be missing from the Dev-Cpp kernel32 library file. You can try removing
    Code:
    #define UNICODE
    #define _UNICODE
    form the top of the source. Maybe Process32FirstA and Process32NextA are available.

    I don't use Dev-Cpp so I can't help you too much with problems with the compiler. Possibly other members know how to deal with missing functions.

    The other library files required by the code are shell32 and user32.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    nothing to do
    i've tried till today to compile ur code or to modify it and try to close the process by the exe size but i was not albe

    what an hard life

  10. #10
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Zip up your Dev-Cpp project files, and attach them to a new post. Maybe someone can help you.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Quote Originally Posted by BianConiglio
    thank u very much I will surely try my best

    a question (first of a lot)

    when i try to compile you nice kill program, dev cpp says :


    [Linker error] undefined reference to `Process32FirstW@8'
    [Linker error] undefined reference to `Process32NextW@8'
    C:\Programmi\Dev-Cpp\Termina da dimensione\Makefile.win
    [Build Error] ["Termina] Error 1


    I've included kernel32 library.
    These functions reside in libth32.a. Pass -lth32 to gcc or g++.

  12. #12
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477

    Post

    Hi BianConiglio,

    Well I have written a process control code a while back. It's in C++ and Win32, but it was designed for a console program. Feel free to modify it anyway you want its open source at the moment.

    Process Control

    What this code does is it Enumerate's all processes running, you can also enumerate a process by its name. You can also terminate processes by Process ID number or Exe name. It's a really neat program, hope you enjoy it!


    Hope this helps,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx a lot to everyone, I will post here my new code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Terminate Process by Specific FullFilePath
    By MattKamil in forum Windows Programming
    Replies: 1
    Last Post: 07-05-2006, 04:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21