有两段代码 代码一是单独写成函数来调用的, 我把他们合成一个主函数的代码 就出错 好像是变量递增的问题,我怎么也想不通,麻烦大家了。
代码一 别人写的:
#include
#include
#include
#include
#pragma comment(lib,"Psapi.lib")
int FindModules( DWORD processID )
{
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded;
unsigned int i;
// Get a handle to the process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );
if (NULL == hProcess) return 1;
// Get a list of all the modules in this process.
if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
for ( i = 0; i < (cbNeeded / sizeof(HMODULE)); i++ )
{
TCHAR szModName[MAX_PATH];
if (GetModuleBaseName(hProcess, hMods[i], szModName,sizeof(szModName) / sizeof(TCHAR)))
{
if(strcmp(szModName,"sfc.dll")==0)
{
printf("%s PID:%d\n",szModName,processID);
break;
}
}
}
}
CloseHandle( hProcess );
return 0;
}
int main( void )
{
DWORD aProcesses[1024];
DWORD cbNeeded;
unsigned int i;
// Get the list of process identifiers.
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return 1;
// Print the names of the modules for each process.
for ( i = 0; i < cbNeeded / sizeof(DWORD); i++ )
{
FindModules( aProcesses[i] );
}
return 0;
}
代码2:我想合到一个函数中:
#include
#include
#include
#pragma comment(lib,"Psapi.lib")
int main( void )
{
DWORD aProcesses[1024];
DWORD cbNeeded;
unsigned int i;
HMODULE hMods[1024];
HANDLE hProcess;
DWORD cbNeeded2;
unsigned int ii;
// Get the list of process identifiers.
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return 1;
// Print the names of the modules for each process.
for ( i = 0; i < cbNeeded / sizeof(DWORD); i++ )
{
// 错误下面这句,分开些函数就没问题 i都等于0,写在一个函数里 就不行了,就返回2,必须把i加1,跳过过pid为0才行.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i+1] );//我直接用aProcesses[i]怎么不行啊?
if (NULL == hProcess) return 2;
// Get a list of all the modules in this process.
if( EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded2))
{
for ( ii = 0; ii < (cbNeeded2 / sizeof(HMODULE)); ii++ )
{
TCHAR szModName[MAX_PATH];
if (GetModuleBaseName(hProcess, hMods[ii], szModName,sizeof(szModName) / sizeof(TCHAR)))
{
if(strcmp(szModName,"sfc.dll")==0)
{
printf("%s PID:%d\n",szModName,aProcesses[i+1]);
break;
}
}
}
CloseHandle( hProcess );
}
}
return 0;
}