I have HTTP server application (service application that is ran as desktop application) that runs other application and later one should activate window of yet another application. Computer X contains all applications. When I run http://192.168.16.21:225/command/TTaskType.ttTestTask from Opera web browser from computer X, my WinActivate
function works. When I run http://192.168.16.21:225/command/TTaskType.ttTestTask from computer Y, window is not activated. None debug outputs returns errors (I get FLastErrorY printed). I even tried to run HTTP server as admin. What else could I try?
function WinActivate(const AWinTitle: string): boolean;
var
_WindowHandle: HWND;
_KeyboardState: TKeyboardState;
begin
ResetError;
_WindowHandle := FindWindow(nil, PWideChar(AWinTitle));
FLastError := GetLastError;
SetCursorPos(10, 12);
OutputDebugString(PWideChar('FLastError1: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
if _WindowHandle <> 0 then
begin
//ShowWindow(_WindowHandle, SW_MINIMIZE);
if IsIconic(_WindowHandle) then
begin
ShowWindow(_WindowHandle, SW_RESTORE);
ResetError;
Result := IsIconic(_WindowHandle);
if Result then
Result := WinWaitActive(AWinTitle, 1000);
end
else
Result := SetForegroundWindow(_WindowHandle);
OutputDebugString(PWideChar('FLastErrorX: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
if not Result then
begin
FLastError := GetLastError;
OutputDebugString(PWideChar('FLastError2: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle])));
// Applications might lock focus, so, hack it around
GetKeyBoardState(_KeyboardState);
ShowWindow(_WindowHandle, SW_SHOWNORMAL);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, SPIF_UPDATEINIFILE);
if _KeyboardState[VK_MENU] <> 1 then
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY or 0, 0);
ResetError;
if not SetForegroundWindow(_WindowHandle) then
begin
FLastError := GetLastError;
if not SetForegroundWindow(_WindowHandle) then
begin
FLastError := GetLastError;
OutputDebugString(PWideChar('FLastErrorY: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
end;
end;
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
OutputDebugString(PWideChar('FLastError3: ' + IntTostr(FLastError) +
', _WindowHandle: ' + Format('%.8X', [_WindowHandle]) + ' ' + DateTimeToStr(Now)));
Result := FLastError = 0;
end;
if not Result then
begin
SetWindowPos(_WindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOMOVE);
SetWindowPos(_WindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOMOVE);
end;
end;
Result := WinWaitActive(AWinTitle, 1000);
OutputDebugString(PWideChar('Dabar'));
end;
So, I have found out whats wrong. I knew that AutoHotkey haves window activation functionality. I have downloaded its source code and window activation code is well written and commented. So I have build a test application using AutoHotkey and my window was not activated. I found out it was because no there is no active user logged in.