|
-
January 4th, 2009, 06:26 AM
#1
Find handle Process when know Name
i want Find Handle of process ,ex alg.exe in windowsxp???
-
January 4th, 2009, 07:53 AM
#2
Re: Find handle Process when know Name
Why don't you search in MSDN or Google Groups ?!
it needs 2 seconds to find OpenProcess on the pid got from name...
-
January 4th, 2009, 11:11 AM
#3
Re: Find handle Process when know Name
OK,thank you ,i were do word
Code:
#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;
}
Last edited by maiami_2911; January 4th, 2009 at 11:49 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|