3

I am trying to get a list of all the executable paths of running processes

The do-while loop (shown below) starts off and after about 90 something iterations it fails with a ERROR_INSUFFICIENT_BUFFER error. I presume thats the pBuffer, I tried with a very large buffer and it still failed. The ProcessEntry struct on the failed iteration has garbage in szExeFile. Please advise (I close the handles, not shown below)

Code:

// Retrieve a handle to the process snapshot
HANDLE hProcessSnapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (INVALID_HANDLE_VALUE == hProcessSnapshot) ERROR;

// Retrieve information about the first process and exit if unsuccessful
PROCESSENTRY32 oProcessEntry;
memset(&oProcessEntry, 0x00, sizeof(oProcessEntry));
oProcessEntry.dwSize = sizeof(PROCESSENTRY32);
BOOL bFileFound(Process32First(hProcessSnapshot, &oProcessEntry));
DWORD dwError;
if(!bFileFound) {        
    dwError = GetLastError();
    if(ERROR_NO_MORE_FILES == dwError) return TPathList();

    // Error
    ERROR; 
}

// Walk the snapshot of processes
TCHAR pBuffer[MAX_PATH];
TPathList lExecutablePaths;
do {
    // Get handle to process
    HANDLE hProcess(OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 
                                FALSE, 
                                oProcessEntry.th32ProcessID));
    if(INVALID_HANDLE_VALUE == hProcess) ERROR;
    if (!hProcess) continue;

    // Get the module path
    if(GetModuleFileNameEx(hProcess, 0, pBuffer, MAX_PATH) == 0) ERROR;      
    lExecutablePaths.push_back(CPath(pBuffer));        
} 
// Get next process 
while(Process32Next(hProcessSnapshot, &oProcessEntry));

// If we ran out of files return what has been found
dwError = GetLastError();
if(ERROR_NO_MORE_FILES == dwError) return lExecutablePaths;
ERROR;
ababeel
  • 435
  • 1
  • 8
  • 25

1 Answers1

-1

You can use like this:

CString sBuffer;
DWORD dwSize = MAX_PATH + 1, dwError = 0;

do
{
    GetModuleFileName( NULL, sBuffer.GetBuffer( dwSize ), dwSize );

    // Retrieve the last error. If we've succeeded ERROR_SUCCESS
    // will be returned; otherwise, we'll get an ERROR_INSUFFICIENT_BUFFER
    // error.
    dwError = ::GetLastError( );

    // Buffer may not be big enough so double its size
    dwSize *= 2;
}
while( dwError == ERROR_INSUFFICIENT_BUFFER
    && dwError != ERROR_SUCCESS );

// Release the buffer (turn the string back to const)
sBuffer.ReleaseBuffer( );

I hope it will work.

Also you can prefer How can I calculate the complete buffer size for GetModuleFileName?

Community
  • 1
  • 1
jiten
  • 5,128
  • 4
  • 44
  • 73
  • Thank you, but the problem I am having is not with the GetModuleFileNameEx but the Process32Next function. That's the one that fails with the ERROR_INSUFFICIENT_BUFFER – ababeel Jun 05 '12 at 00:00