Thread: TaskManager

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

    TaskManager

    Hi I've done that little taskmanager :

    Code:
    #include <windows.h>
    #include <psapi.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.c>
    
    int Kill(int pID)
    {
    	int ret;
    	HANDLE  hProcess=OpenProcess(PROCESS_ALL_ACCESS,
    TRUE,pID);
    				
    	if(hProcess==NULL)
    	{
    		return 1;
    	}
    	
    	ret = !TerminateProcess(hProcess,0);
    	CloseHandle(hProcess);
    	return ret;
    }
    
    char *  GetProcessName(DWORD processID ,char * szProcessName, DWORD bufsiz)
    {   
    
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       0, processID );
    
    	szProcessName[0]='\0';
    
        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;
    
            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   bufsiz );
            }
            else return NULL;
        }
        else return NULL;
    
       
        CloseHandle( hProcess );
    
    	return szProcessName;
    }
    
    
    
    int main(int argc, char**argv)
    {
    
        int  volte=0;    
        char scelta;
    	char szProcessName[MAX_PATH];
    	
    	DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    
        if ( GetVersion() > 0x80000000 )
          
          {
          printf("Funziono solo su XP!!\n");
          goto fine;
          }
       	
        inizio:
       	
        volte = volte + 1;
        
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return 1;
       
        cProcesses = cbNeeded / sizeof(DWORD);
    
    	    
        printf("\n");
        printf(" [");
        textcolor(LIGHTRED);
        printf("Bianconiglio's TaskManager");
        textcolor(WHITE);
        printf("]\n");
        printf(" [--Processi in Esecuzione--]\n");
        printf(" [ Aggiornamento numero ");
        textcolor(YELLOW);
        printf("%d",volte);
        textcolor(WHITE);
        printf("/2 ]\n"); 
        printf("\n");
    	
        for ( i = 0; i < cProcesses; ++i )
    	{	
     		GetProcessName(aProcesses[i],szProcessName,MAX_PATH);
    		if ((szProcessName!=NULL)||(szProcessName!="")||(szProcessName!="\0"));
            printf(" %s\n", szProcessName);		
    	}
    
    	printf("\n");
    	printf(" [---Invio per Aggiornare---]\n");
    	printf(" [                          ]\n");
    	printf(" [-Possibilita di 2 refresh-]\n");
    	printf("\n");
    	
        scelta = getchar();
        
        if (volte > 1)
            goto fine;
            
        else   while (scelta='a' || 'A')
               goto inizio;	
       
        fine: 
           
        system("pause");	
    	return 0;
    }
    (if u want to compile it, u have to use psapi lib)

    the problem is that the output is not perfect :

    as u can see in the attached image, there are some empty lines
    it've tried to avoid it with

    Code:
    if ((szProcessName!=NULL)||(szProcessName!="")||(szProcessName!="\0"));
    but there is nothing to do...

    suggestions ?
    Last edited by BianConiglio; 05-03-2004 at 09:17 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if ((szProcessName!=NULL)||(szProcessName!="")||(szPr ocessName!="\0"));
    It's an array, so it can never be NULL to start with, so the first part is always true
    To test for a non-empty string
    Code:
    if ( szProcessName[0] != '\0' ) {
        printf(" %s\n", szProcessName);
    }
    You really ought to be able to do this program without using any gotos
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx I will try

    and yes, no goto in the *real* realase

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Windows taskmanager got hosed
    By psychopath in forum Tech Board
    Replies: 7
    Last Post: 06-09-2007, 08:18 PM
  3. Taskmanager open on startup?
    By skorman00 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 07-16-2006, 03:59 PM
  4. Keeping a window on top like the Taskmanager
    By Queatrix in forum Windows Programming
    Replies: 2
    Last Post: 08-08-2005, 01:49 PM