Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetForegroundWindow isn't working, but not errors are being generated #30

Closed
OlHall opened this issue Dec 8, 2022 · 4 comments
Closed

Comments

@OlHall
Copy link
Contributor

OlHall commented Dec 8, 2022

Describe the bug
I have created a group of commands using SetForegroundWindow. It is enabled, and the AppName(s) are correct.
I wish to be able to switch the PC between apps from a the control system. MCEC is connecting to the control system successfully.
When I issue a command, MCEC receives the command, matches it with the definition and runs the steps.
My commands are nested.
No errors are reported (I'm sure I have the correct AppName - and if it's wrong, I DO get an error saying the app cannot be found - example below)

2022-12-08 14:30:34,605 INFO  - Client: Received; goPres
2022-12-08 14:30:34,615 INFO  - SetForegroundWindowCommand: GetProcessByName for POWERPNT.exe failed

I have tried running MCEC as admin in case this is a permissions issue.
I have increased logging to DEBUG but don't get any additional information.
I've tried disabling all AntiVirus in case this is catching the SetForegroundWindow API call - not sure how else to debug that though.

To Reproduce
Steps to reproduce the behavior:

  1. Create a custom command
    <setforegroundwindow cmd="goPres" enabled="true" appname="POWERPNT">
      <sendmessage enabled="true" msg="274" lparam="0" wparam="61488" />
    </setforegroundwindow>
  1. Invoke the command
  2. Observe the log output
  3. The CURRENT window IS maximised, but the initial focus does not change

Expected behaviour
The desired app should gain focus and then maximise.

Version Information
Include the first line from the log window, like this:

2022-11-22 14:15:27,097 INFO  - MCE Controller v2.2.10.2 - OS: Microsoft Windows NT 10.0.22621.0 on x64 - .NET: 4.0.30319.42000

Snippets from the log
Include any other relevant logs (either copy/pasted from the MCE Controller window or from the log files found in %appdata%\Kindel Systems\MCE Controller which contain more debug info).

2022-12-08 14:25:22,926 INFO  - Client: Received; goPres
2022-12-08 14:25:22,939 INFO  - SetForegroundWindowCommand: SetForegroundWindow(POWERPNT)
2022-12-08 14:25:22,946 INFO  - SendMessageCommand: SendMessage(<forground window>, 274, 61488, 0) - Cmd="" Msg="274" lParam="0" wParam="61488" ClassName="" WindowName=""

Desktop (please complete the following information):
Dell Laptop, Windows 11, Crestron control system

@OlHall
Copy link
Contributor Author

OlHall commented Dec 8, 2022

Small Update:
Sometimes it works, but most of the time it doesn't.
HOWEVER - The icon for the App "flashes" when SetForegroundWindow(...) is called - just no other indication of why the window cannot be selected.

@OlHall
Copy link
Contributor Author

OlHall commented Dec 8, 2022

In case it helps anyone else: On further investigation, this seems to be a general problem in that Windows doesn't like apps to push themselves forward automagically. Which is the use we NEED ;-)

The solution was to FIRST set focus to MCEC, and then it can request a new window:

	<setforegroundwindow cmd="goPres" enabled="true" appname="MCEControl">
		<setforegroundwindow enabled="true" appname="POWERPNT" />
		<sendmessage enabled="true" msg="274" lparam="0" wparam="61488" />	
	</setforegroundwindow>

I hope that helps.

@OlHall OlHall closed this as completed Dec 8, 2022
@tig
Copy link
Owner

tig commented Dec 9, 2022

Thanks for tracking this down.

Yes, somewhere around Win7 the behavior changed to prevent apps from spamming users.

Glad my decision to nest commands turned out useful ;-).

@OlHall
Copy link
Contributor Author

OlHall commented Dec 9, 2022

It's great! Many thanks for the hard work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants