End Google Ads 201810 - BS.net 01 --> Hi all.

I've spent 3 days searching for this and trying different alternatives, but nothing works.

I have an app which works perfectly on Windows XP:

1) Monitorizes some windows.
2) When one of them reaches a certain state, my app brings it to the foreground.
3) Then, does something with it (moves the mouse, clicks something...).
4) Finally, my app brings to the foreground the window that was visible before step 2, so the user can resume his work where he was before.

My problem is that, when I execute this app on Windows Vista, the step 4) seems not to work. I've been reading a lot these days, and I've found that the "SetForegroundWindow()" method I was using works differently on Vista, so I started using "AttachThreadInput()" following an example that I found somewhere:

private void activateWindow(IntPtr hWnd)
{
if (IsIconic(hWnd))
ShowWindowAsync(hWnd, SW_RESTORE);

ShowWindowAsync(hWnd, SW_SHOW);

SetForegroundWindow(hWnd);

IntPtr foregroundWindow = GetForegroundWindow();
IntPtr Dummy = IntPtr.Zero;

uint foregroundThreadId = GetWindowThreadProcessId(foregroundWindow, Dummy);
uint thisThreadId = GetWindowThreadProcessId(hWnd, Dummy);

if (AttachThreadInput(thisThreadId, foregroundThreadId, true))
{
BringWindowToTop(hWnd);
SetForegroundWindow(hWnd);
AttachThreadInput(thisThreadId, foregroundThreadId, false);
}

if (GetForegroundWindow() != hWnd)
{
IntPtr Timeout = IntPtr.Zero;
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, Timeout, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Dummy, SPIF_SENDCHANGE);
BringWindowToTop(hWnd);
SetForegroundWindow(hWnd);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Timeout, SPIF_SENDCHANGE);
}
}

From my app, I do the following when a timer ticks and finds that one of the background windows must be activated:


IntPtr originalWHwnd = GetForegroundWindow(); //To keep the handle of the window in which the user is currently working.

/* Let's say that window with handle "hWnd" must be visible now: */
activateWindow(hWnd); //It works in Vista and XP: brings the window to foreground.

/*
Here I do the stuff with the window "hWnd":
- move the mouse somewhere,
- click somewhere else...
*/

/* Finally I want to bring back the original window: */
activateWindow(originalWHwnd); //It does not work on Vista; it does on XP.


That code still works on Windows XP, but still doesn't work on Vista: step 4 will not bring to foreground the first ********

Please, could you enlighten me?

Thanks in advance.