Wise
Wise

Reputation: 658

win32gui.SetActiveWindow() ERROR : The specified procedure could not be found

I get the active window like so:

window = win32gui.GetForegroundWindow()

which is an Int, say 1053634. And afterwards I try to set the foreground window back to the specified window:

win32gui.SetForegroundWindow(window)

And I get this error: win32gui.SetForegroundWindow(window) error: (127, 'SetForegroundWindow', 'The specified procedure could not be found.')

Sometimes when I do this in the interpreter, I get this error:

win32gui.SetForegroundWindow(1053634)
error: (0, 'SetForegroundWindow', 'No error message is available')

What do you think is the problem?

Thanks!

Upvotes: 16

Views: 24315

Answers (4)

mxl
mxl

Reputation: 667

As of 2024 and Windows (Vista+), some of these answers may not apply to your situation, but see this thread Python on Windows: "Access is denied" sometimes when calling win32gui.SetForegroundWindow() and specifically look how prior to SetForegroundWindow they move the mouse cursor off screen (!) to circumvent the Windows security check which denies an app the possibility to change the focus window:

from pywinauto import mouse
import win32gui, win32con

# this moves the mouse off screen, so that NO WINDOW has mouse focus (!)
mouse.move(coords=(-10000, 500))

# ONLY NOW you may set focus
hwnd = win32gui.FindWindow(None, window_title)
win32gui.ShowWindow(hwnd, win32con.SW_SHOW)
win32gui.SetForegroundWindow(hwnd)

Upvotes: 0

Kelvin
Kelvin

Reputation: 865

The problem is you can't set foreground if the window is minimized so I offer my poor man's improvement to the existing answers:

import win32gui
import win32con

win32gui.ShowWindow(hwnd, win32con.SW_NORMAL)
win32gui.SetForegroundWindow(hwnd)

Upvotes: 1

VectorASD
VectorASD

Reputation: 19

What worked it me...

import win32gui, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
input("Press Enter")
shell.SendKeys(' ') #Undocks my focus from Python IDLE
win32gui.SetForegroundWindow(window) #It works!
shell.SendKeys('%')

Upvotes: 2

timmyt123
timmyt123

Reputation: 599

My program works fine on my desktop with Windows 7, but when I use my laptop with Windows Vista (even with UAC off), I get the error:

pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')

The program will flash in the taskbar, but no characters are sent.

I even tried sending 'notepad' SetForegroundWindow and get the same error.

Here is a link with a workaround that combines threads to get the computer to think they work together: http://www.shloemi.com/2012/09/solved-setforegroundwindow-win32-api-not-always-works/

This article has more information about the problem.

UPDATE: I'm sorry that link goes to a C program. I researched some more and found out it will let you SetForegroundWindow, if you send an alt key first.

For example:

import win32gui, win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('%')
win32gui.SetForegroundWindow(window)

Upvotes: 39

Related Questions