警告 C4267“参数":从“size_t"到“DWORD"的转换,可能会丢失数据 [英] Warning C4267 'argument': conversion from 'size_t' to 'DWORD', possible loss of data

查看:68
本文介绍了警告 C4267“参数":从“size_t"到“DWORD"的转换,可能会丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将代码从 32 位 vs2012 迁移到 64 位 vs2015.

我在我的程序中遇到了以下函数调用:

CryptHashData(hHash,(BYTE*)AUTH_ENCRYPTION_KEY,wcslen(AUTH_ENCRYPTION_KEY) * sizeof(wchar_t),0u))

其声明位于 c:Program Files (x86)Windows Kits8.0Includeumwincrypt.h 中的 wincrypt.h(看起来像不得编辑).

声明是:

WINADVAPI布尔值WINAPI加密哈希数据(_In_ HCRYPTHASH hHash,_In_reads_bytes_(dwDataLen) CONST BYTE *pbData,_In_ DWORD dwDataLen,_In_ DWORD dwFlags);

<块引用>

DWORD dwFlags:这里的问题是 0u 是 unsigned int 并且该函数需要一个 DWORD.

为了解决这个错误,我做了:

  • c-style 在函数 call(tried size_t, unsigned int)
  • 中转换为 (DWORD)(0U)
  • static_cast
  • 尝试创建一个新变量并将其强制转换

但警告仍然存在

看起来我必须改变函数调用

有人可以建议我如何解决这个问题.

请询问是否需要更多详细信息.

警告图片详情
稍后警告图片详情

解决方案

您认为 0u 是问题所在.
对我来说,问题应该在第三个参数附近,其中 size_t 值用于提供 dword 参数.

正如 SomeProgrammerDude 所解释的,size_t 在新环境中可能是 64 位,而 DWORD 是 32 位.这就解释了新平台上的不匹配.

在 32 位平台上,您没有收到警告(我假设),可能是因为 size_t 是 32 位,没有丢失信息的风险.

您报告强制转换避免了警告,这表明 0u 不是问题.
警告似乎与 0u 一致的事实可能是由于编译器抱怨整个函数调用并指出它的末尾,即结束 ),恰好与 0u 在同一行.
将所有 ) 移动到单独的行的实验(结果可见两个屏幕截图之间的差异)已经证实了这一点.

注意,强制转换只是避免了警告,这不等于解决问题.
(您明智地询问了铸造的安全性,我建议在单独的问题中这样做.)

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))

whose declaration is in wincrypt.h located in c:Program Files (x86)Windows Kits8.0Includeumwincrypt.h (looks like not to be edited).

The declaration is:

WINADVAPI
BOOL
WINAPI
CryptHashData(
_In_                    HCRYPTHASH  hHash,
_In_reads_bytes_(dwDataLen)  CONST BYTE  *pbData,
_In_                    DWORD   dwDataLen,
_In_                    DWORD   dwFlags
);

DWORD dwFlags: Problem here is 0u is unsigned int and the function needs a DWORD.

To solve this error I did:

  • c-style casting as (DWORD)(0U) in function call(tried size_t, unsigned int)
  • static_cast
  • tried creating a new variable and casted it

But the warning still persists

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.

Warning image details
later Warning image details

解决方案

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.

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.

On the 32bit platform you did not get a warning (I assume), probably because size_t was 32bit there, without risk of loosing information.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆