ERROR_BAD_LENGTH when calling Process32First on Windows 7

I was just trying to undo some old code from Windows XP that generates a list of all running processes, but it failed in Windows 7. Before proceeding, here is the code:

#include <windows.h> #include <tlhelp32.h> int main() { HANDLE hSnap, hTemp; PROCESSENTRY32 pe; hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if(Process32First(hSnap, &pe)) { do { ... } } while(Process32Next(hSnap, &pe)); } ... } 

I checked which function failed, and it turned out to be Process32First. GetLastError () returned 24: "ERROR_BAD_LENGTH" I cannot understand what the problem is. Any suggestions?

+6
source share
2 answers

From MSDN: http://msdn.microsoft.com/en-us/library/ms684834(VS.85).aspx

The calling application must set the dwSize PROCESSENTRY32 member in the size in bytes of the structure.

To get information about other processes recorded in the same snapshot, use the Process32Next function.


EDIT: You probably want to do something like this:

 PROCESSENTRY32 pe = {0}; pe.dwSize = sizeof(PROCESSENTRY32); 
+8
source

There is an error in tlhelp32.h when called in WIN64 :

If the pack #pragma somewhere before turning on tlhelp32.h , it will create a PROCESSENTRY32 structure with the wrong size. Then anything can happen, including Process32First errors or even crashes.

Try enabling tlhelp32.h as follows:

  #pragma pack(push,8) /* Work around a bug in tlhelp32.h in WIN64, which generates the wrong structure if packing has been changed */<br/> #include &lt;tlhelp32.h&gt;<br/> #pragma pack(pop) 
+1
source

Source: https://habr.com/ru/post/891642/


All Articles