Click here to Skip to main content
13,192,426 members (49,318 online)
Rate this:
 
Please Sign up or sign in to vote.
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 12-May-10 5:36am
Updated 22-Jul-16 1: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.
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. 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?

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

Solution 1

  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171017.2 | Last Updated 22 Jul 2016
Copyright © CodeProject, 1999-2017
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