![](/img/trans.png)
[英]Is type casting needed when using NOT (~) operator on a literal integer against a 64bit variable
[英]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);
我這樣稱呼它為 function。
DWORD destLen = 0;
PBYTE dest = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(SIZE_T)1024);
LzmaDec_DecodeToBuf(...,dest,&destLen,...)
所以我在64位編程中確實有function問題。 謝謝閱讀
假設SizeT
是SIZE_T
:
我這樣稱呼它為 function。
DWORD destLen = 0; PBYTE dest = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(SIZE_T)1024); LzmaDec_DecodeToBuf(...,dest,&destLen,...)
將 DWORD 的地址(在 32 位窗口和 64 位窗口上為 32 位寬)傳遞到SIZE_T
的地址(在 64 位窗口上為 64 位寬,在 32 位窗口上為 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://docs.microsoft.com/en-us/cpp/build/common-visual-cpp-64-bit-migration-issues?view= VS-2019
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.