GetWindowLong vs GetWindowLongPtr in C #

I used GetWindowLong as follows:

[DllImport("user32.dll")] private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex); 

But according to the MSDN docs, I have to use GetWindowLongPtr for compatibility with the 64-bit version. http://msdn.microsoft.com/en-us/library/ms633584(VS.85).aspx

The MSDN docs for GetWindowLongPtr say that I should define it like this (in C ++):

 LONG_PTR GetWindowLongPtr(HWND hWnd, int nIndex); 

I used to use IntPtr as the return type, but what the hell would I use for the equivalent for LONG_PTR? I also saw that GetWindowLong is defined like this in C #:

 [DllImport("user32.dll")] private static extern long GetWindowLong(IntPtr hWnd, int nIndex); 

What is correct and how can I ensure proper 64-bit compatibility?

+2
c #
Nov 26 '08 at 3:30
source share
3 answers

Unfortunately, this is not so simple, because GetWindowLongPtr does not exist on 32-bit Windows. On 32-bit systems, GetWindowLongPtr is simply a C macro that points to GetWindowLong. If you really need to use GetWindowLongPtr on both 32 and 64-bit systems, you will need to determine the correct one to call at runtime. See Description on pinvoke.net

+6
Nov 26 '08 at 8:22
source share

You must define GetWindowLongPtr using IntPtr. In C / C ++, LONG_PTR is a 32-bit bit on a 32-bit system and 64-bit on a 64-bit system (see here ). IntPtr in C # is designed to work the same way (see here ).

So you want:

 [DllImport("user32.dll")] private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex); 
+7
Nov 26 '08 at 3:43 a.m.
source share

SoapBox is correct.

Also, if you ever need to see how a type or function should be a marshal in Win32, try using the PInvoke Interop Assistant . It will have built-in generations for most Win32 APIs and can do its own generation based on code fragments.

+3
Nov 26 '08 at 4:19
source share



All Articles