4

I used GetWindowLong window api to get current window state of a window in c#.

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


    Process[] processList = Process.GetProcesses();
    foreach (Process theprocess in processList)
    {

        long windowState = GetWindowLong(theprocess.MainWindowHandle, GWL_STYLE);

        MessageBox.Show(windowState.ToString());

    }

I expected to get numbers on http://www.autohotkey.com/docs/misc/Styles.htm, but I get numbers like -482344960, -1803550644, and 382554704.

Do I need to convert windowState variable?? if so, to what?

Moon
  • 22,195
  • 68
  • 188
  • 269
  • 1
    Maybe you should convert those values to hex and compare them with what you expected. I'm not sure they are wrong. – H H Aug 13 '09 at 10:55
  • @Henk Holterman: you are right. I should convert those values and compare. – Moon Aug 13 '09 at 11:49

2 Answers2

10

What is weird about those values? For example, 482344960 is equivalent to 0x1CC00000 which looks like something you might expect to see as a window style. Looking at the styles reference you linked to, that is WS_VISIBLE | WS_CAPTION | 0xC000000.

If you wanted to test for WS_VISIBLE, for example, you would do something like:

int result = GetWindowLong(theprocess.MainWindowHandle, GWL_STYLE);
bool isVisible = ((result & WS_VISIBLE) != 0);
Nate
  • 18,752
  • 8
  • 48
  • 54
0

It appears that you probably want to use GetWindowLongPtr instead, and change the return value to a long. This method uses a different return type of LONG_PTR, which sounds like what you are looking for.

GetWindowLong http://msdn.microsoft.com/en-us/library/ms633584(VS.85).aspx

LONG GetWindowLong(      
    HWND hWnd,
    int nIndex
);

GetWindowLongPtr http://msdn.microsoft.com/en-us/library/ms633585(VS.85).aspx

LONG_PTR GetWindowLongPtr(      
    HWND hWnd,
    int nIndex
);

According to MSDN if you are running 64-bit Windows you need to use GetWindowLongPtr, because GetWindowLong only uses a 32-bit LONG, which would give you negative values after it reached the end of the 32-bit LONG. Plus it sounds like GetWindowLong has been superseded by GetWindowLongPtr, so it is probably the right way to go for future development.

This is the import you should use to return the value from GetWindowLongPtr.

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

.NET uses 64-bit longs regardless of the platform.

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
  • This is probably not wrong, but also not necessarily required for `GWL_STYLE` as all window styles are still `LONG` i.e. 32-bits values. – Lukas Aug 10 '21 at 06:39