从假定相同尺寸的类型进行铸造时发出警告
我无法弄清楚这个警告。它说我正在将 MY_KEY 转换为更大大小的 HKEY,但它们看起来大小相同(32 位)?
//test.c
//compile as C
//compile with warning level 4
#include <windows.h>
typedef enum MY_KEY
{
//from WinReg.h :
//#define HKEY_CLASSES_ROOT ((HKEY)(ULONG_PTR)((LONG)0x80000000))
//ULONG_PTR cast to avoid 'constant expression is not integral' error
//(caused by HKEY being a HANDLE, and so a void*)
MY_KEY_1 = (ULONG_PTR)HKEY_CLASSES_ROOT,
} MY_KEY;
void Func1(HKEY b)
{
b;
}
void Func2(MY_KEY a)
{
//produces warning C4306:
//'type cast' : conversion from 'MY_KEY' to 'HKEY' of greater size
Func1((HKEY)a);
}
(C代码,VS2005,警告级别4。)
I can't figure this warning out. It says I am casting a MY_KEY to a HKEY of greater size, but they would appear to be the same size (32 bits)?
//test.c
//compile as C
//compile with warning level 4
#include <windows.h>
typedef enum MY_KEY
{
//from WinReg.h :
//#define HKEY_CLASSES_ROOT ((HKEY)(ULONG_PTR)((LONG)0x80000000))
//ULONG_PTR cast to avoid 'constant expression is not integral' error
//(caused by HKEY being a HANDLE, and so a void*)
MY_KEY_1 = (ULONG_PTR)HKEY_CLASSES_ROOT,
} MY_KEY;
void Func1(HKEY b)
{
b;
}
void Func2(MY_KEY a)
{
//produces warning C4306:
//'type cast' : conversion from 'MY_KEY' to 'HKEY' of greater size
Func1((HKEY)a);
}
(C code, VS2005, warning level 4.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 MY_KEY 是 HKEY 的 ULONG_PTR。当您将其传递给函数 1 时,您将其类型转换为实际的 HKEY,这是一个坏主意。如果你想要一个 HKEY,为什么不直接使用 HKEY?除非出于某种原因需要指针,在这种情况下,请传递指针指向的数据。
The problem is that MY_KEY is a ULONG_PTR to an HKEY. You are type casting it to an actual HKEY when you pass it to function 1, which is a bad idea. If you want an HKEY, why are you not using an HKEY directly? Unless you need a pointer for some reason, in which case, pass along the data that the pointer is pointing to.