c++ - Process ID function getting error "ERROR_NO_MORE_FILES" -


im trying id process using function below, getlasterror keeps returning error_no_more_files never gets chance iterate through processes. im not sure whats causing this. ideas? test passing in param "notepad.exe"

int getprocid(string procname){     processentry32 pe32;     handle procsnapshot = createtoolhelp32snapshot(th32cs_snapprocess,0);      pe32.dwsize = sizeof(processentry32);      if(!process32first(procsnapshot,&pe32))         return 0;     else         if(pe32.szexefile == procname)             return pe32.th32processid;         else         {             while(getlasterror() != error_no_more_files){                 process32next(procsnapshot,&pe32);                     if(pe32.szexefile == procname)                         return pe32.th32processid;             }             return 0;         } } 

your while loop wrong. shouldn't checking error value before calling process32next function. function return without doing if prior call windows api function (probably process32next function) returned error_no_more_files, , name of first process in snapshot happens other name want search for.

keep in mind not api functions set error code error_success have succeeded (and don't forget close snapshot handle after you're done it).


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -