Calling devcon from a batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Calling devcon from a batch

#1 Post by sambul35 » 17 Mar 2016 16:50

Its a known issue that accessing some monitors' EDID at Windows PC wakeup, connected via HDMI or DisplayPort, or some DP-to-HDMI or DVI-to-HDMI adapter or Audio Receiver, may be delayed or fail, resulting in no picture or garbled image on-screen. There're a few methods to re-access EDID without PC reboot. One is mentioned here. However, when manually launching the batch below, it exits without querying the monitor (i.e. nothing happens), where devcon is placed in c:\windows\system32. Despite when the same command is run from Win 10 64-bit Cmd Prompt, it queries the monitor as it should (screen blinks, resolution is restored to set). Why is that?

Code: Select all

@echo off
ping 127.0.0.1 -w 5000>nul
"%~dp0devcon.exe restart =display *ven_1002*"


Also tried devcon restart =display *ven_1002* instead of the above, or giving full path to devcon, but no change.

kwsiebert
Posts: 43
Joined: 20 Jan 2016 15:46

Re: Calling devcon from a batch

#2 Post by kwsiebert » 17 Mar 2016 21:40

Are there any error messages when it is directly launched? Place a pause as the final line to see them before it exits. It is also specifically looking for devcon in the same folder that the batch is in, rather than c:\windows\system32, but that should be the same no matter how you launch it.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Calling devcon from a batch

#3 Post by sambul35 » 17 Mar 2016 22:46

Added pause, here we go ( I removed GPU details):

Code: Select all

PCI\VEN_1002&DEV_.....: Restart failed
No matching devices found.
Press any key to continue . . .


Why does it fail to restart the device from batch, despite restarting it OK from Admin Cmd Prompt without any errors? A copy of devcon 64 is added to the same folder. If I choose "Run as admin", the batch window opens and immediately closes without wait, no way to read anything. May be some dll packed in that bin prevents it from running from a batch?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Calling devcon from a batch

#4 Post by penpen » 21 Mar 2016 09:43

One issue is the use of doublequotes in the third line:
It searches for a command or file '%~dp0devcon.exe restart =display *ven_1002*',
but you only wanted to start '%~dp0devcon.exe' with some parameters.

This should work (closing doublequotes after the file name and not at the end):

Code: Select all

"%~dp0devcon.exe" restart =display *ven_1002*


to get the output i would use no "@echo off" and redirect the rest to a file:

Code: Select all

>> test.txt (
2>>&1 (
   echo("%~dp0devcon.exe" restart =display *ven_1002*
   "%~dp0devcon.exe" restart =display *ven_1002*
)
)


penpen

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Calling devcon from a batch

#5 Post by sambul35 » 21 Mar 2016 12:54

Before trying that, could you explain, what's the purpose of open extra "(", and why duplicate the same command line?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Calling devcon from a batch

#6 Post by penpen » 21 Mar 2016 15:04

The output of the (dos file) handle to the handle to stderr is redirected to stdin, and then
the handle to stdin is redirected and appended to the file "test.txt".
The outer parentheses are used to ensure this order of operation,
and proper restoration of the file handles:
I am in the habit of doing it this way although in this case it should work without the outer parentheses.

The inner parentheses are used to process the redirection for both (inner) commands.

I use opening parenthesis because of this:
http://www.dostips.com/forum/viewtopic.php?t=1900

There is no duplicated command line.
This will echo a the string behind (with Variable %~dp0 replaced by its actual value):

Code: Select all

echo("%~dp0devcon.exe" restart =display *ven_1002*

This will execute the file "%~dp0devcon.exe" with the given parameters:

Code: Select all

"%~dp0devcon.exe" restart =display *ven_1002*


It could be that the file is empty:
In this case "devcon.exe" directly writes the output to the console directly;
but this is improbable (bad style), so i hope you are able to see all output and errors of this utility.


penpen

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Calling devcon from a batch

#7 Post by sambul35 » 25 Mar 2016 13:56

Sorry for some delay, I tested the above correction:

Code: Select all

"%~dp0devcon.exe" restart =display *ven_1002* >> test.txt (
2>>&1 (
   echo("%~dp0devcon.exe" restart =display *ven_1002*
   "%~dp0devcon.exe" restart =display *ven_1002*
)
)

It does run well when the batch is started from an open Admin Cmd Prompt window, or as Admin by double-click:

Code: Select all

PCI\VEN_1002&DEV_....: Restarted
1 device(s) restarted.

However, devcon doesn't do the job when the batch is started as a user, and also can't save test.txt to a system drive, but can to a non-system drive, which is expected:

Code: Select all

PCI\VEN_1002&DEV_....: Restart failed
No matching devices found.

Once the monitor loses picture again, I'll check if calling the batch by a keyboard shortcut will actually result in restoring the pic. :wink:

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Calling devcon from a batch

#8 Post by penpen » 25 Mar 2016 18:29

sambul35 wrote:

Code: Select all

"%~dp0devcon.exe" restart =display *ven_1002* >> test.txt (
2>>&1 (
   echo("%~dp0devcon.exe" restart =display *ven_1002*
   "%~dp0devcon.exe" restart =display *ven_1002*
)
)
You might have misunderstood my post, or done a copy-paste error.

The first codeblock was the corrected command only.
The second codeblock contains a (complete) batch file, that should save the full output to disk.

You shouldn't combine both.
Just use the second one (only) to check the output to find possible issues.
And if (somewhere in future) all is working fine, then replace the code with the first code.


penpen

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Calling devcon from a batch

#9 Post by sambul35 » 25 Mar 2016 21:00

Oooops... Sorry for my mistake - loosing focus in multitasking. :)

Yet another way to address picture loss by monitor at PC wakeup may be GPU driver restart with Restart64.exe tool supplied with CRU package.

Its interesting how devcon or driver restart work in this scenario. Windows first sends picture to a default virtual VGA monitor, then a started GPU driver sends it to SIMULATED XGA virtual monitor. Then driver re-scans connected physical monitors, reads Windows Display resolution & scale settings, and (may) re-scale back open desktop windows to fit current Main Display. This process is deficient also in another scenario: when Windows is running in no-sleep mode, and monitor is switched off by its power saving feature or button, Windows re-scales all open desktop windows to SIMULATED monitor resolution. When the real monitor is turned back on and then signal to it from GPU awaken (by moving a mouse to start sending pic to the monitor again), all on-screen windows are often shown rescaled to SIMULATED monitor resolution. This represents a real headache in multi-monitor scenario, which is on top of periodic picture loss at connecting a 4K monitor to GPU via DP-port or DP-to-HDMI adapter. Lets hope, batch approach will work to restore picture, while more thinking is due to make it work to restore all open windows upon monitor re-start to their original size & position to match physical monitor resolution & scale settings.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Calling devcon from a batch

#10 Post by sambul35 » 05 Apr 2016 14:53

Just wanted to update, I was unable to restore a lost picture by running the devcon batch. In fact, nothing changed on screen at all, despite the same batch runs well if launched for testing when picture is normal. So I don't know, if devcon was able to produce any partial effect such as restarting GPU or subsequent query of the monitor, however test.txt file says: "1 device(s) (i.e. GPU) restarted", though it didn't propagate towards monitor recovery and neither resulted in temporary black screen as it usually does.

What I noticed though, picture loss often occurs when a wireless mouse is moved back-n-force or to a longer distance when awaking a PC or monitor from sleep. So it appears to be a bug in MS HID Non-User Input Data Filter installed with MS wireless HID-type devices like keyboard, mouse, or combo. AFAIK that driver filters out consecutive fast device movements or vibrations to prevent unintended PC wakeup or signal drop, which may occur when next mouse movement event and corresponding action request is sent before the previous request is fully processed through the entire chain, including reply from monitor. When I very slightly and slowly move a mouse to awaken the monitor or PC, picture drop seldom occurs.

Post Reply