Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

We have the following piece of C# code in our application:

private IntPtr _DefaultWndProc = IntPtr.Zero;

[DllImport("user32")]
private static extern IntPtr SetWindowLongPtr(IntPtr hwnd, int index, 
                                              WndProcPointer wndProcPointer);

[DllImport("user32")]
private static extern IntPtr SetWindowLongPtr(IntPtr hwnd, int index, 
                                              IntPtr wndProc);

[DllImport("user32")]
private static extern IntPtr GetWindowLongPtr(IntPtr hwnd, int index);

[DllImport("User32.dll", CharSet = CharSet.Auto)]
private static extern int CallWindowProc(IntPtr wndProc, IntPtr hwnd, int msg, 
                                         IntPtr wparam, IntPtr lparam);

private const int GWL_WNDPROC = (-4);
private const int WM_NCDESTROY = 0x0082;
private delegate int WndProcPointer(IntPtr hwnd, nt msg, 
                                    IntPtr wparam, IntPtr lparam);

protected virtual int WndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam)
{ 
  return CallWindowProc(_DefaultWndProc, hwnd, msg, wparam, lparam);
}

_DefaultWndProc is being set as follows:

_DefaultWndProc = SetWindowLongPtr(control.Handle, GWL_WNDPROC, _MyWndProc);

When we compile and execute this program for 32 bit configuration everything works fine.

However, if we set the configuration for 64 bit, the method call CallWindowProc(_DefaultWndProc, hwnd, msg, wparam, lparam) listed above is throwing the following exception:

System.OverflowException - {"Arithmetic operation resulted in an overflow."}

This occurs only when we move the mouse cursor over certain columns in a ListView.

Any idea ?

share|improve this question
    
OK, let's get the silly questions out of the way first. Are you running the code on a 64-bit PC? – Jason Evans Jul 17 '13 at 10:19
    
Might be of interest? social.msdn.microsoft.com/Forums/vstudio/en-US/… – Jason Evans Jul 17 '13 at 10:25
    
Yes, we are running in Windows 7 64 bit. Also, as I mentioned in the original post, we are not getting this error when we set the build configuration to 32 bit and run in the same Windows 7 64 bit machine. – user2508039 Jul 17 '13 at 11:32

2 Answers 2

up vote 1 down vote accepted
 private delegate int WndProcPointer(IntPtr hwnd, nt msg, 
                                     IntPtr wparam, IntPtr lparam);

Your declaration is wrong, the return value is IntPtr, not int. I assume the type of msg was a typo. And yes, this is likely to cause trouble in 64-bit mode.

There are better ways to do this:

  1. Derive your own class from ListView and override its WndProc() method

  2. Derive your own class from NativeWindow and override its WndProc() method. Call this.DefWndProc() to pass messages. Use its Attach() method once you have a valid window handle. Call Detach() inside your WndProc() override when you get WM_NCDESTROY.

share|improve this answer
    
Changing the return type from int to IntPtr worked. Thanks Hans! :) – user2508039 Jul 18 '13 at 6:06

Out of curisoity, what happens if you change:

private static extern IntPtr SetWindowLongPtr(IntPtr hwnd, int index, WndProcPointer wndProcPointer);

to

private static extern IntPtr SetWindowLongPtr(IntPtr hwnd, IntPtr index, WndProcPointer wndProcPointer);

Changing index to be an IntPtr rather then an int. I got this idea from the following SO question

Does P/Invoke on 64-bit windows require different signatures than on 32-bit?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.