FindWindow Error

I am having problems with FindWindow using the pywin32 extension. Simple C code:

 int main() { HWND h = FindWindow(NULL, TEXT("SomeApp")); if (h != INVALID_HANDLE_VALUE) SetForegroundWindow(h); return 0; } 

It works well. Same thing with python:

 import win32gui h = win32gui.FindWindow(None, "SomeApp") if h: win32gui.SetForegroundWindow(h) else: print "SomeApp not found" 

Failed, SomeApp not found. I suggest that text encoding can cause problems here, but does not find any information in the documents how to specify the text.

Update: I tested the code on another machine and do not see any problems. So the configuration on my first machine should be wrong. When a problem is detected, I update the results of my research.

+4
source share
1 answer

In C code, you check h != INVALID_HANDLE_VALUE , in Python h != None . INVALID_HANDLE_VALUE not 0 / null / None .

Python defines win32file.INVALID_HANDLE_VALUE using a win32file import.

Alternatively, instead of typing "SomeApp not found", you can do something like:

 gle = win32api.GetLastError() err = win32api.FormatMessage(gle)[:-2] print 'SomeApp not found: LastError=%d - %s' % (gle, err) 

This should give you more details about the failure if FindWindow for some reason made a mistake (or "Success" if it worked).

+1
source

Source: https://habr.com/ru/post/1439355/


All Articles