Click to See Complete Forum and Search --> : Find handle Process when know Name


maiami_2911
January 4th, 2009, 05:26 AM
i want Find Handle of process ,ex alg.exe in windowsxp???

carl666
January 4th, 2009, 06:53 AM
Why don't you search in MSDN or Google Groups ?!
it needs 2 seconds to find OpenProcess on the pid got from name...

maiami_2911
January 4th, 2009, 10:11 AM
OK,thank you :D,i were do word

#define UNICODE
#ifdef UNICODE
#define _UNICODE
#endif

#define DEV_CPP
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#ifdef VISUAL_CPP
#define NOT_USE_DEV_CPP
#include <psapi.h>//-->For using EnumProcesses,...Source:MSDN
#endif

#ifdef NOT_USE_DEV_CPP
#undef DEV_CPP
#endif

#ifdef DEV_CPP
#define EnumProcesses pfnEnumProcesses
#define EnumProcessModules pfnEnumProcessModules
#define GetModuleBaseName pfnGetModuleBaseName
#endif

#ifdef DEV_CPP
typedef int (WINAPI *PFNINT_PSAPI_EP) (DWORD*, DWORD, DWORD*);
PFNINT_PSAPI_EP pfnEnumProcesses=NULL;
typedef int (WINAPI *PFNINT_PSAPI_EPM) (HANDLE, HMODULE*, DWORD, DWORD*);
PFNINT_PSAPI_EPM pfnEnumProcessModules=NULL;
typedef int (WINAPI *PFNINT_PSAPI_GMBN) (HANDLE, HMODULE, LPTSTR, DWORD);
PFNINT_PSAPI_GMBN pfnGetModuleBaseName=NULL;
#endif

HINSTANCE hLibrary=NULL;
HANDLE PrintProcessNameAndID( DWORD processID ,TCHAR ProcessName[30])
{
TCHAR szProcessName[MAX_PATH] = TEXT("<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)/sizeof(TCHAR) );
}
}

// Print the process name and identifier.

if(lstrcmp( szProcessName,ProcessName)==0)
{
return hProcess ;
CloseHandle( hProcess );
}
return 0;
}

INT main( )
{
#ifdef DEV_CPP
hLibrary=LoadLibrary(TEXT("psapi.dll"));
if(hLibrary==NULL)
{
MessageBox(NULL,TEXT("Error when loading DLL"),TEXT("Msg"),MB_ICONERROR);
return 0;
}

EnumProcesses = (PFNINT_PSAPI_EP) GetProcAddress (hLibrary, "EnumProcesses");
EnumProcessModules = (PFNINT_PSAPI_EPM) GetProcAddress (hLibrary, "EnumProcessModules");
#ifndef UNICODE
GetModuleBaseName = (PFNINT_PSAPI_GMBN) GetProcAddress (hLibrary, "GetModuleBaseNameA");
#else
GetModuleBaseName = (PFNINT_PSAPI_GMBN) GetProcAddress (hLibrary, "GetModuleBaseNameW");
#endif
if((EnumProcesses==NULL)||(EnumProcessModules==NULL)||(GetModuleBaseName==NULL))
{
MessageBox(NULL,TEXT("Error when loading function"),TEXT("Msg"),MB_ICONERROR);
return 0;
}
#endif
///////////////////////////
// Get the list of process identifiers.
TCHAR ProcessName[30];
lstrcpy(ProcessName,L"alg.exe");

DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;

if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 0;

// Calculate how many process identifiers were returned.

cProcesses = cbNeeded / sizeof(DWORD);

// Print the name and process identifier for each process.

HANDLE handle;
for ( i = 0; i < cProcesses; i++ )
{
handle=PrintProcessNameAndID( aProcesses[i],ProcessName ) ;
if( aProcesses[i] != 0&& handle !=0)
_tprintf( TEXT("\n%d"),handle);
}

#ifdef DEV_CPP
FreeLibrary(hLibrary);
#endif
system("pause");
return 0;
}