Checking if a process is running (problems including psapi.dll i think...)

Posted on 16th Feb 2014 by admin

Ok, so all I'm trying to do is check if a program is running...

I've found out that I need to include psapi.dll (to use the EnumProcesses() & GetModuleBaseName() functions)...now, I'm not too knowledgeable with including dll's...but I researched it and believe I have it right.

But for some reason I'm getting this error :/ (I'm using code blocks compiler btw):
Quote: main.cpp|26|error: invalid initialization of non-const reference of type 'DWORD&' from a temporary of type 'DWORD (*)[1024]
I don't really understand this message...although I think it's something to do with my typedef that's attempting to load the DLL function EnumProcesses()...

Anywho, here's my code.

Code: #include #include #include using namespace std; bool proc(string process_name) { //type definition for paspi.lib typedef int (__stdcall *process_dll)(DWORD &processes, DWORD process_size, DWORD &process); //load the psapi library HINSTANCE dll = LoadLibrary("psapi.lib"); process_dll _processes; _processes = (process_dll) GetProcAddress(dll, "EnumProcesses"); if(_processes == 0) { return _processes; } //define process vars DWORD processes[1024], process; //get current processes if(!_processes(&processes, sizeof(processes), &process)) { return false; } //iterate through each process for(unsigned int i = 0; i < process / sizeof(DWORD); i++) { if(processes[i] == 0) { continue; } HANDLE ph = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, processes[i]); char buffer[50]; GetModuleBaseName(ph, 0, buffer, sizeof(buffer)); CloseHandle(ph); if(process_name == (string) buffer) { return true; } } return false; } int main(int argc, char *argv[]) { if(proc("some proc name")) cout << "process is running"; else cout << "process is not running"; return 0; }

Any help would be greatly appreciated

Thanks a lot,

Other forums