64bit windows api 编程类型转换问题

64bit windows api programming type casting problem

(抱歉我的英语不好)。我有 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 位编程中确实遇到了函数问题。感谢阅读

假设 SizeTSIZE_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 位-windows 和 64 位-windows 上为 32 位宽)传递到 SIZE_T(64 位)的地址64 位宽 windows 和 32 位宽 32 位 windows) 预计是坏的。

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://docs.microsoft.com/en-us/cpp/build/common-visual-cpp-64-bit-migration-issues?view=vs-2019