I’ve troubles with FindWindow using pywin32 extension.
Simple C code:
int main()
{
HWND h = FindWindow(NULL, TEXT("SomeApp"));
if (h != INVALID_HANDLE_VALUE)
SetForegroundWindow(h);
return 0;
}
Works well. Same with python:
import win32gui
h = win32gui.FindWindow(None, "SomeApp")
if h:
win32gui.SetForegroundWindow(h)
else:
print "SomeApp not found"
Fails, SomeApp not found. I suggest text encoding may be cause trouble here but not found any information in docs how to specify text.
Update:
I’ve tested code on other machine and don’t see any troubles. So, configuration on my first machine should be incorrect. I’ve update my investigation results if found the problem.
In the C code, you are checking
h != INVALID_HANDLE_VALUE, in Pythonh != None.INVALID_HANDLE_VALUEis not0/null/None.Python defines
win32file.INVALID_HANDLE_VALUEvia thewin32fileimport.Also, instead of printing “SomeApp not found”, you can do something like:
This should give you more details on the failure if
FindWindowhas legitimately failed for some reason (or “Success” if it worked).