适用于C#的HIWORD(),LOWORD()宏 [英] The Proper HIWORD(), LOWORD() macros for C#

查看:356
本文介绍了适用于C#的HIWORD(),LOWORD()宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinVista Business上使用VS2008 Express Edition。



我正在处理一些代码,这些代码在整个地方给我OverflowExceptions,我不够熟悉与涉及的数据类型,以解决问题。我怀疑它可能导致我的程序反复崩溃,但我无法确定。



我已经下载了C#文件浏览器的代码,在< a href =http://www.codeproject.com/KB/miscctrl/FileBrowser.aspx> FileBrowser.aspx [ ^ ]并且代码调用一个名为HiWord的静态属性,即解压缩m.WParam的高位字,其中m是传递给WndProc的数据结构( )覆盖任何.NET表单。



代码是

I am using VS2008 Express Edition on WinVista Business.

I am working with some code which is giving me OverflowExceptions all over the place and I am not familiar enough with the data types involved in order to solve the problem. I have a suspicion it might be causing my program to crash repeatedly but I cannot be sure.

I have downloaded the code for the "C# File Browser," at FileBrowser.aspx[^] and the code calls a static property called HiWord, i.e. to unpack the high-order word of a m.WParam where m is the data structure passed to the WndProc() override of any .NET Form.

The code is

public static uint HiWord(IntPtr ptr)
{
    if (((uint)ptr & 0x80000000) == 0x80000000)
        return ((uint)ptr >> 16);
    else
        return ((uint)ptr >> 16) & 0xffff;
}





,例外是 if(((uint) )ptr& 0x80000000)== 0x80000000)



任何人都可以帮我找出正确的方法来获取高阶词(这是从上下文菜单发送的消息,我可以不使用Windows Forms事件,因为我正在与Windows Shell连接)?



[提示]当您使用< a href>包含链接时标签,你需要在它和< / a>之间放置一些东西。或者没有显示任何链接。



and the exception is on the line if (((uint)ptr & 0x80000000) == 0x80000000).

Can anyone please help me to find out what the proper way to get at the high-order word (it's for a message sent from a Context Menu, and I can't use Windows Forms events because I am interfacing with the Windows Shell) is?

[Tip] When you include a link using the <a href> tags, you need to put something between it and </a> or there won't be any link shown.

推荐答案

嗨Brian,

我怀疑该项目最初是用溢出检查编译的,您无意中更改了该设置。



引发异常的原因是IntPtr到uint的转换并不像代码所暗示的那样简单。它更类似于通过中间有符号整数的双重转换。



uint value =(uint)(int)ptr;



如果中间值为负,那么当溢出检查开启时,向前转换为无符号整数将失败。



Alan。



IDE可以很好地隐藏溢出检查开/关开关。在2005版本中,它位于Project Properties .. Build .. Advanced。
Hi Brian,
I suspect the project was originally compiled with overflow checking off and that you have inadvertantly changed that setting.

The reason the exception is raised is that conversion of IntPtr to uint is not quite as straightforward as the code implies. It is more akin to a double conversion via an intermediate signed integer.

i.e. uint value = (uint)(int)ptr;

If the intermediate value is negative then the onward conversion to an unsigned integer will fail when overflow checking is on.

Alan.

The IDE does a good job of hiding the overflow checking on/off switch. In the 2005 edition it's under Project Properties.. Build.. Advanced.


Brian C. Hart,Ph.D 。写道:
Brian C. Hart, Ph.D. wrote:

return((uint)ptr>> 16);

return ((uint)ptr >> 16);







Brian C. Hart,博士。写道:
Brian C. Hart, Ph.D. wrote:

return((uint)ptr>> 16)& 0xffff;

return ((uint)ptr >> 16) & 0xffff;





有什么区别?



: - )



What's the difference?

:-)

< br>

C#Bitwise Helper Class [ ^ ]


这篇关于适用于C#的HIWORD(),LOWORD()宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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