【发布时间】:2021-09-27 09:25:39
【问题描述】:
当我从 Win8.1 升级 #includes winnt.h 的 C 文件时,我在 Win 10 SDK for Winnt.h 中遇到了这个断言的问题:
C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um\winnt.h(2487,1):错误 C2118:否定 下标
#if defined(__cplusplus) && (_MSC_VER >= 1600)
static_assert(__alignof(LARGE_INTEGER) == 8, "Windows headers require the default packing option. Changing this can lead to memory corruption."
" This diagnostic can be disabled by building with WINDOWS_IGNORE_PACKING_MISMATCH defined.");
#elif _MSC_VER >= 1300
#pragma warning(push)
#pragma warning(disable: 4116)
C_ASSERT(TYPE_ALIGNMENT(LARGE_INTEGER) == 8); <========= LN2487
#pragma warning(pop)
#endif
#endif
错误只是告诉我 CASSERT 失败,但我已明确设置 /Zp8 并且没有任何区别。
所以我破解了 Winnt.h:
// Much of the Windows SDK assumes the default packing of structs.
#if !defined(WINDOWS_IGNORE_PACKING_MISMATCH) && !defined(__midl) && !defined(MIDL_PASS) && !defined(SORTPP_PASS) && !defined(RC_INVOKED)
#if defined(__cplusplus) && (_MSC_VER >= 1600)
static_assert(__alignof(LARGE_INTEGER) == 8, "Windows headers require the default packing option. Changing this can lead to memory corruption."
" This diagnostic can be disabled by building with WINDOWS_IGNORE_PACKING_MISMATCH defined.");
#elif _MSC_VER >= 1300
#pragma warning(push)
#pragma warning(disable: 4116)
#pragma pack(show)
C_ASSERT(TYPE_ALIGNMENT(LARGE_INTEGER) == 8);
#pragma warning(pop)
#endif
#endif
测试代码:
#pragma pack(show)
#include "LibraryHeader.h" //somehow includes Windows.h
#pragma pack(show)
现在我得到了结果:
value of pragma pack(show) == 8 value of pragma pack(show) == 4 value of pragma pack(show) == 8
因此,我们使用的第 3 方标头的混乱似乎一定是造成这种情况的原因。这很奇怪,因为它适用于 8.1 SDK,并且因为标头本身明确告诉我们必须在编译器中设置 8 字节打包/对齐。
我想问题归结为:W10 SDK 是否有任何方式导致这种情况没有发生任何代码更改并且它针对 W8.1 SDK 进行编译?或者,一直都是这样,W8.1 SDK检查失败?
【问题讨论】:
-
您是直接包含 winnt.h 还是因为包含 Windows.h 而包含它?你在使用预编译的头文件吗?
-
所以我破解了 Winnt.h: 不错。那么是什么第三方标头破坏了它?所以我们知道要避免使用该产品... ;-)
-
@AndrewHenle 我将不得不找出答案,我只需要这样
#pragma pack对吗?正如我所说,它以前也有效。 -
这个问题的公认答案对您有帮助吗? stackoverflow.com/questions/2163044/…
-
@engf-010 我认为这是一个单独的问题,尽管我可以在只有具有 Windows 10 SDK 的机器上进行测试以确保
标签: c++ c visual-studio winapi c-preprocessor