关于"Process32First"返回false的问题
问题描述
1 | http://blog.csdn.net/hbu_dcf/article/details/3491276 |
解决
基本上和上面这个遇到的一样但是是在编译x64版本的时候出的问题,首先想到的就是结构体对齐的问题但是看了下结构体都是默认对齐,新建一个工程测试同样的代码也没问题,google搜了也只搜到上面这条但是好像并没有什么卵用啊。。。。。
一个个对比编译选项也没发现差别,然后一个个修改结构体对齐一直都没变都是0x230,在新建的能正确运行的大小是0x238但是始终这里都是有问题的,一直不变想想太古怪了。。。。。。
难道只有在写成这样硬编码ProcessEntry.dwSize = 0x238
在搜索试试,把错误代码换成英文”process32first” The program issued a command but the command length is incorrect
果然出现了些东西,在msdn找到了答案
https://social.msdn.microsoft.com/Forums/en-US/6f43716f-fdd3-4c92-bfba-6a23178c32bf/size-of-processentry32-structure?forum=vcgeneral
There's a bug in tlhelp32.h, when invoked in WIN64:
If there's a #pragma pack directive somewhere before including tlhelp32.h, it will generate a PROCESSENTRY32 structure with the wrong size.
Then anything can happen, including Process32First failures, or even crashes.
Try including tlhelp32.h this way:
#pragma pack(push,8) /* Work around a bug in tlhelp32.h in WIN64, which generates the wrong structure if packing has been changed */
#include <tlhelp32.h>
#pragma pack(pop)
果然为了平台统一在其他头文件里使用了#pragma pack,把tlhelp32.h写在这个头文件前面问题解决。