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 ?