[英]64bit windows api programming type casting problem
(Sorry for my bad English). (对不起,我的英语不好)。 I have 2 questions about 64bit c programming.
我有 2 个关于 64 位 c 编程的问题。
My code worked good but today I get error (no compiler or code analysis).我的代码运行良好,但今天出现错误(没有编译器或代码分析)。 And its randomly.
而且是随机的。 For instance sometimes the code works sometimes it doesn't.
例如,有时代码有效,有时无效。 When I open executable inside WinDbg my code always gets error.
当我在 WinDbg 中打开可执行文件时,我的代码总是出错。 I think its 64bit type casting problem.
我认为它的 64 位类型转换问题。
Code Sample:代码示例:
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;
}
Without WinDbg code don't print error (by debug_this()) but inside WinDbg code print Line number (reason is hash_string_len.= 32) but I know hash_string_len = 32 so I think its 64 bit truncation problem?没有 WinDbg 代码不打印错误(通过 debug_this())但在 WinDbg 代码打印行号(原因是 hash_string_len.= 32)但我知道 hash_string_len = 32 所以我认为它的 64 位截断问题? Can anybody help me?
有谁能够帮我?
My Second question is:我的第二个问题是:
Can it give me error in 64bit programming?它会在 64 位编程中给我错误吗?
DWORD a = 0;
some_func(&a);
some_func(PDWORD pA)
{
*pA = 1;
}
Because I use a lot codes like this inside my project.因为我在我的项目中使用了很多这样的代码。 Like that.
像那样。
LZMA Lib LZMA库
SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen,
const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status);
I call this function like that.我这样称呼它为 function。
DWORD destLen = 0;
PBYTE dest = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(SIZE_T)1024);
LzmaDec_DecodeToBuf(...,dest,&destLen,...)
So I really have function problem in 64bit programming.所以我在64位编程中确实有function问题。 Thanks for reading
谢谢阅读
Assuming SizeT
is SIZE_T
:假设
SizeT
是SIZE_T
:
I call this function like that.
我这样称呼它为 function。
DWORD destLen = 0; PBYTE dest = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(SIZE_T)1024); LzmaDec_DecodeToBuf(...,dest,&destLen,...)
Passing the address of DWORD (32bit wide on 32-bit-windows as well as on 64-bit-windows) to where the address of a SIZE_T
(64bit wide on 64-bit-windows and 32bit wide on 32-bit-windows) is expected is bad.将 DWORD 的地址(在 32 位窗口和 64 位窗口上为 32 位宽)传递到
SIZE_T
的地址(在 64 位窗口上为 64 位宽,在 32 位窗口上为 32 位宽)预计是坏的。
Cast the
DWORD
to
SIZE_T
before taking its address like this:
在获取地址之前将
DWORD
转换为
SIZE_T
:
LzmaDec_DecodeToBuf(..., dest, &((SIZE_T) destLen), ...)
Introduce a temporay variable of SIZE_T
and pass its address:引入一个
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;
}
More on issue migrating code from 32bit to 64bit using VC on Windows are here: https://docs.microsoft.com/en-us/cpp/build/common-visual-cpp-64-bit-migration-issues?view=vs-2019更多关于在 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.