Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [C] Controllo che fallisce

    Ciao..ho creato il codice seguente :

    legge tutti i processi attivi e ne restituisce nome e PID :

    codice:
    #include <windows.h>
    #include <stdio.h>
    #include "psapi.h"
    
    
    void LookforMyProcessAndPID( DWORD processID )
    {
        char szProcessName[MAX_PATH] = "<unknown>";
        
        // Get a handle to the process.
    
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );
    
        // Get the process name.
    
        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;
    
            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName) );
            }
        }
    
        // Print the process name and identifier.
        
        printf( "%s  (PID: %u)\n", szProcessName, processID );
               
        CloseHandle( hProcess );
    }
    
    main( )
    {
        // Get the list of process identifiers.
    
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return;
    
        // Calculate how many process identifiers were returned.
    
        cProcesses = cbNeeded / sizeof(DWORD);
    
        // Print the name and process identifier for each process.
    
        for ( i = 0; i < cProcesses; i++ )
            LookforMyProcessAndPID( aProcesses[i] );
       
       system("PAUSE");
    }
    ma se a me interessa UN SOLO PROCESSO (in questo caso svchost.exe) ed aggiungo un controllo per vedere se il nome del processo è quello che mi interessa :

    codice:
    #include <windows.h>
    #include <stdio.h>
    #include "psapi.h"
    
    
    void LookforMyProcessAndPID( DWORD processID )
    {
        char szProcessName[MAX_PATH] = "<unknown>";
        char processtofind[MAX_PATH] = "svchost.exe";
    
        // Get a handle to the process.
    
        HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, processID );
    
        // Get the process name.
    
        if (NULL != hProcess )
        {
            HMODULE hMod;
            DWORD cbNeeded;
    
            if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )
            {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName) );
            }
        }
    
        // Print the process name and identifier.
        
        if (szProcessName == processtofind)
           {
             printf( "%s  (PID: %u)\n", szProcessName, processID );
           }
           
        CloseHandle( hProcess );
    }
    
    main( )
    {
        // Get the list of process identifiers.
    
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return;
    
        // Calculate how many process identifiers were returned.
    
        cProcesses = cbNeeded / sizeof(DWORD);
    
        // Print the name and process identifier for each process.
    
        for ( i = 0; i < cProcesses; i++ )
            LookforMyProcessAndPID( aProcesses[i] );
       
       system("PAUSE");
    }
    il programma non mi restituisce nulla perchè il controllo

    if (szProcessName == processtofind)

    fallisce sempre...

    qualche idea sul motivo ?

  2. #2
    Utente di HTML.it L'avatar di pixer
    Registrato dal
    Oct 2000
    Messaggi
    614

    Re: [C] Controllo che fallisce

    Originariamente inviato da TestaDiMucca
    il programma non mi restituisce nulla perchè il controllo

    if (szProcessName == processtofind)

    fallisce sempre...

    qualche idea sul motivo ?
    le comparazioni fra array char non si effettuano in quel modo, devi usare la funzione strcmp presente nella string.h.

    if (!strcmp(szProcessName, processtofind)
    MySQL Worker - The OpenSource Multiplatform MySQL database Administrator (C++ powered)
    .:[ It resumes the development !! ]:.


  3. #3
    c'hai troppo ragionissima, provo subito
    grazie

  4. #4
    Utente di HTML.it L'avatar di pixer
    Registrato dal
    Oct 2000
    Messaggi
    614

    Re: Re: [C] Controllo che fallisce

    Originariamente inviato da pixer
    if (!strcmp(szProcessName, processtofind))
    ho dimenticato una parentesi alla fine dell'espressione.
    MySQL Worker - The OpenSource Multiplatform MySQL database Administrator (C++ powered)
    .:[ It resumes the development !! ]:.


  5. #5
    si si tranzollo ce l'ho aggiunta io

    MITICO

    grazie dell'aiuto

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.