haimgel / display-switch

Turn a $30 USB switch into a full-featured multi-monitor KVM switch
https://haim.dev/posts/2020-07-28-dual-monitor-kvm/
MIT License
2.86k stars 112 forks source link

running external program on windows with spaces and parameter does not work #72

Closed marcelser closed 2 years ago

marcelser commented 2 years ago

Hi,

I'm trying to run an external program called voicemeeter on usb_connect, I have put the following line in the configuration file: on_usb_connect_execute = "C:\Program Files (x86)\VB\Voicemeeter\voicemeeter8.exe -R"

I've already tried different combinations of the string with double backslashes because the logfile was not showing backslahes with also adding a single or double backslash on the white space between "Program Files" and "(x86)" without success I always get in the log something like: 08:53:44 [ERROR] Error executing external command 'C:\Program Files (x86)\VB\Voicemeeter\voicemeeter8.exe -R': Das System kann die angegebene Datei nicht finden. (os error 2)

Which means "file not found", maybe I also have to separate the parameter "-R" from the program but how would the configuration file look then?

In the end I guess it's just a matter of putting correct string(s) to make it work, but even as a software developer with windows and linux knowledge I coudln't figure out the correct values.

Please provide the correct version here and maybe also update the readme.md so other don't have to try around and open an issue.

Thank you very much

haimgel commented 2 years ago

Oh this is a tricky one!

  1. The backslashes need to be escaped.
  2. The app needs to know that C:\Program Files (x86)\VB\Voicemeeter\voicemeeter8.exe (with the space inside) is one long path to the executable, but the -R is a separate parameter. The way to do it, is this:
on_usb_connect_execute = "'C:\Program Files (x86)\\VB\\Voicemeeter\\voicemeeter8.exe' -R"

Note the quoted path (single quotes because the whole parameter is double-quoted), and double backslashes.

Please let me know if this worked for you.

marcelser commented 2 years ago

Hi @haimgel ,

Yes the backslashes solved that program was not found but now I get an exit code when display-switch tries to run voicemeeter8

01:10:12 [ERROR] Error executing external command ''C:\Program Files (x86)\VB\Voicemeeter\voicemeeter8.exe' -R': Exited with status exit code: 0xffffffff

If this is an unsigned 32bit int then in decimal that represents -1 from what I've read.

So I executed voicemeeter in a cmd.exe command prompt (tried with and without admin rights). But in both cases this returned exit code 0 not -1 as in display-switch.

It's a bit hard to tell if voicemeeter was run and just returns some exit code for whatever reason or if it failed to run. The thing is that's restarting the audio engine in voicemeeter which is a background task without any visual indication that it worked. I will however find it out in a day or two if audio is always working when switching display or not.

haimgel commented 2 years ago

👍 ,from your log paste above, it's also possible that you misplaced the single quotes. Basically, they have to be around the binary file name, and not around the parameter.

marcelser commented 2 years ago

The single quotes are correct, I think it's github that what was doing some strange formatting to the log output. I enclosed it now in a code block what it really looks like.

here's the line in the config file.

on_usb_connect_execute = "'C:\\Program Files (x86)\\VB\\Voicemeeter\\voicemeeter8.exe' -R"

btw: I had to also escape the first backslash after the drive letter as well which you missed in your example.