Reputation: 35450
How do I use Binary-AND to check if a particular bit is set for an IntPtr
object?
I'm calling GetWindowLongPtr32()
API to get window style for a window. This function happens to return an IntPtr. I have defined all the flag constants in my program. Now suppose if I want to check whether a particular flag (say WS_VISIBLE
) is set, I need to Binary-AND it with my constant, but my constant is of int
type, so I cannot do that directly. Try to call ToInt32()
and ToInt64()
both result in (ArgumentOutOfRangeException) exception. What's my way out?
Upvotes: 2
Views: 1508
Reputation: 21265
How do I pinvoke to GetWindowLongPtr and SetWindowLongPtr on 32-bit platforms?
public static IntPtr GetWindowLong(HandleRef hWnd, int nIndex)
{
if (IntPtr.Size == 4)
{
return GetWindowLong32(hWnd, nIndex);
}
return GetWindowLongPtr64(hWnd, nIndex);
}
[DllImport("user32.dll", EntryPoint="GetWindowLong", CharSet=CharSet.Auto)]
private static extern IntPtr GetWindowLong32(HandleRef hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint="GetWindowLongPtr", CharSet=CharSet.Auto)]
private static extern IntPtr GetWindowLongPtr64(HandleRef hWnd, int nIndex);
GetWindowLong(int hWnd, GWL_STYLE) return weird numbers in c#
You can implicit cast IntPtr
to int
to get the result.
var result = (int)GetWindowLong(theprocess.MainWindowHandle, GWL_STYLE);
bool isVisible = ((result & WS_VISIBLE) != 0);
Upvotes: -1
Reputation: 35869
Just convert IntPtr
to an int
(it has a conversion operator) and use logical bit operators to test bits.
const int WS_VISIBLE = 0x10000000;
int n = (int)myIntPtr;
if((n & WS_VISIBLE) == WS_VISIBLE)
DoSomethingWhenVisible()`
Upvotes: 2