(抱歉我的英语不好)。我有 2 个关于 64 位 C 编程的问题。
我的代码运行良好,但今天出现错误(没有编译器或代码分析)。而且是随机的。例如,有时代码可以工作,有时却不能。当我在 WinDbg 中打开可执行文件时,我的代码总是出错。我认为是 64 位类型转换问题。
代码示例:
DWORD hash_string_len = 0;
hash_string_len = (DWORD)strlen(hash_string); //hash_string is 32 character hash (A998B0FE08AB295072965B5A4B0C835E)
if (hash_string_len != (DWORD)(MD5_DIGEST_LENGTH * 2)) //MD5_DIGEST_LENGTH (#define MD5_DIGEST_LENGTH 16)
{
debug_this(); //printf("%d\n",__LINE__)
HeapFree(GetProcessHeap(), 0, ENGINE_HASHLIST_MD5_ENTRY);
return FALSE;
}
没有WinDbg代码不会打印错误(通过debug_this()),但在WinDbg代码中打印行号(原因是hash_string_len!= 32),但我知道hash_string_len = 32,所以我认为它是64位截断问题。有人可以帮助我吗?
我的第二个问题是:
在 64 位编程中它会给我错误吗?
DWORD a = 0;
some_func(&a);
some_func(PDWORD pA)
{
*pA = 1;
}
因为我在我的项目中使用了很多这样的代码。就这样。
LZMA 库
SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
我这样称呼这个函数。
DWORD destLen = 0;
PBYTE dest = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(SIZE_T)1024);
LzmaDec_DecodeToBuf(...,dest,&destLen,...)
所以我在 64 位编程中确实遇到了函数问题。感谢您的阅读
最佳答案
假设SizeT
是 SIZE_T
:
I call this function like that.
DWORD destLen = 0; PBYTE dest = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(SIZE_T)1024); LzmaDec_DecodeToBuf(...,dest,&destLen,...)
将 DWORD 的地址(在 32 位窗口和 64 位窗口上为 32 位宽)传递到 SIZE_T
的地址。 (64 位 Windows 上为 64 位宽,32 位 Windows 上为 32 位宽)预计效果不好。
<罢工>
投 DWORD
至SIZE_T
在获取其地址之前,如下所示:
LzmaDec_DecodeToBuf(..., dest, &((SIZE_T) destLen), ...)
罢工><罢工>罢工>
引入临时变量SIZE_T
并传递其地址:
{
SIZE_T s = destLen; /* The initialisation is only necessary if
LzmaDec_DecodeToBuf() expects a value. */
LzmaDec_DecodeToBuf(..., dest, &s, ...);
if (((SIZE_T) UINT_MAX) < s) {
/* handle overflow of destLen! */
}
destLen = s;
}
有关在 Windows 上使用 VC 将代码从 32 位迁移到 64 位问题的更多信息,请参见:https://learn.microsoft.com/en-us/cpp/build/common-visual-cpp-64-bit-migration-issues?view=vs-2019
关于c - 64位windows api编程类型转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352504/