Click here to Skip to main content
15,844,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
}


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.
Posted
Updated 22-Jul-16 2:23am
v4
Comments
Philippe Mori 22-Jul-16 14:11pm    
Before copying someone else code, make sure that the code is working and well written. This code is too complex. There are no reason to have any condition in LoWord function.
Brian C Hart 22-Jul-16 14:18pm    
Can people stop bullying me? This question is so old, that I don't even give a crap about answers to this question any more.

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.

[EDIT] 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.
 
Share this answer
 
v2
Comments
Brian C Hart 12-May-10 15:48pm    
That's a good point, however I am using VS2008 Express Edition. Is there a way to turn off Overflow Checking with VS2008 Express?
Brian C. Hart, Ph.D. wrote:
return ((uint)ptr >> 16);


Brian C. Hart, Ph.D. wrote:
return ((uint)ptr >> 16) & 0xffff;


What's the difference?

:-)
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900