We're testing a windows based system and use a user defined function to identify the window, but sometimes the function won't identify the window and will only work after closing and re-opening the window. The function is as follows:

Public Function getForegroundWindow_notQTP
' Declare the GetForegroundWindow function for use in the script.
Extern.Declare micInteger, "IsWindowVisible", "user32.dll", "IsWindowVisible", micHwnd
Extern.Declare micHwnd, "GetForegroundWindow", "user32.dll", "GetForegroundWindow"
' obtain foreground window handle
wndHandle = Extern.GetForegroundWindow
' obtain title of application with wndHandle
topWindow = Window("hwnd:="& wndHandle).GetROProperty("text")