Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Win32
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 12-May-10 6:36am
Edited 12-May-10 11:17am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
Brian C. Hart, Ph.D. at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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?
 
Smile | :)
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 12 May 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100