警告C4267“参数":从"size_t"到"DWORD"的转换,可能丢失数据 [英] Warning C4267 'argument': conversion from 'size_t' to 'DWORD', possible loss of data
问题描述
我正在将代码从32位vs2012迁移到64位vs2015.
I am migrating code from 32bit vs2012 to 64bit vs2015.
我在程序中遇到了以下函数调用:
I encountered the following function call in my program:
CryptHashData(hHash,
(BYTE*)AUTH_ENCRYPTION_KEY,
wcslen(AUTH_ENCRYPTION_KEY) * sizeof(wchar_t),
0u))
其声明位于c:\Program Files (x86)\Windows Kits\8.0\Include\um\wincrypt.h
中的wincrypt.h
中(看起来好像不被编辑).
whose declaration is in wincrypt.h
located in c:\Program Files (x86)\Windows Kits\8.0\Include\um\wincrypt.h
(looks like not to be edited).
声明是:
WINADVAPI
BOOL
WINAPI
CryptHashData(
_In_ HCRYPTHASH hHash,
_In_reads_bytes_(dwDataLen) CONST BYTE *pbData,
_In_ DWORD dwDataLen,
_In_ DWORD dwFlags
);
DWORD dwFlags:问题在于
0u
是无符号的int且该函数需要一个DWORD
.
DWORD dwFlags: Problem here is
0u
is unsigned int and the function needs aDWORD
.
为解决此错误,我做了:
To solve this error I did:
-
c-style
在功能call(tried size_t, unsigned int)
中强制转换为 -
static_cast
- 尝试创建一个新变量并将其强制转换
(DWORD)(0U)
c-style
casting as(DWORD)(0U)
in functioncall(tried size_t, unsigned int)
static_cast
- tried creating a new variable and casted it
但是警告仍然存在
好像我必须在函数调用中进行更改
Looks like I have to change in function call
有人可以建议我如何解决这个问题.
Can someone suggest me how to solve this issue.
请询问是否需要更多详细信息.
Please ask if more details are required.
推荐答案
您正在考虑0u问题.
对我来说,问题似乎出在第三个参数附近,其中size_t值用于输入dword参数.
You are thinking of the 0u being the problem.
For me it looks like the problem should be around the third parameter, where a size_t value is used to feed a dword parameter.
正如SomeProgrammerDude所解释的,在新环境中,size_t可能是64位,而DWORD是32位.这就解释了新平台上的不匹配.
As SomeProgrammerDude has explained, size_t probably is 64bit in the new environment, while DWORD is 32bit. That explains the mismatch on the new platform.
在32位平台上,您没有收到警告(我认为),可能是因为size_t在32位上没有丢失信息的风险.
On the 32bit platform you did not get a warning (I assume), probably because size_t was 32bit there, without risk of loosing information.
您报告说强制转换可以避免该警告,这表明0u
不是问题.
警告似乎与0u
并存的事实可能是由于编译器抱怨整个函数调用并指出了该函数的结尾而引起的,即关闭)
恰好位于同一位置行作为0u
.
将所有)
移到单独的行上的实验(结果显示为两个屏幕截图之间的差异)已经证实了这一点.
You reported that casting avoids the warning, this indicates that the 0u
is not the problem.
The fact that the warning seems to be on the line with the 0u
is probably caused by the compiler complaining about the whole function call and pin pointing the end of it, i.e. the closing )
, which happens to be on the same line as the 0u
.
The experiment of moving all )
to separate lines (result visible as the difference bwetween the two screenshots) has confirmed this.
请注意,投射仅避免了警告,这与解决问题并不相同.
(您明智地询问了铸造的安全性,我建议在另一个问题中进行铸造.)
Note that casting only avoided the warning, which is not the same as solving the problem.
(You wisely asked about the safety of casting, I recommend to do that in a separate question.)
这篇关于警告C4267“参数":从"size_t"到"DWORD"的转换,可能丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!