I have a block code below.
I want to get all processes in tasks manager with C but it not work.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <psapi.h>
void PrintProcessNameAndID( DWORD processID )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModulesEx( hProcess, &hMod, sizeof(hMod),
&cbNeeded, LIST_MODULES_ALL) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
}
_tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );
CloseHandle( hProcess );
}
int main( void )
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
{
return 1;
}
cProcesses = cbNeeded / sizeof(DWORD);
for ( i = 0; i < cProcesses; i )
{
if( aProcesses[i] != 0 )
{
PrintProcessNameAndID( aProcesses[i] );
}
}
return 0;
}
I want to get all processes in task manager but it return error like this images.
How to solve it, thanks for any answer.
I try to get all processes in task manager.
CodePudding user response:
You should try to link with Psapi.lib.
Your errors are linker errors. The linker is creating the final exe, after the compiler compiled the source files to object files.
When you use functions like EnumProcessModulesEx
from from <psapi.h>
, you need to link with their implementation.
It is recommended to read the documentation for API you are using.
For example for EnumProcessModulesEx
you should read:
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-enumprocessmodulesex