Batch file as shortcut to guest program on host?

Discussions related to using VirtualBox on Windows hosts.
Lendri Mujina
Posts: 17
Joined: 29. Jun 2021, 00:33

Batch file as shortcut to guest program on host?

Post by Lendri Mujina »

Hi. I heard that it was possible to make a batch file that will launch a VM and then launch a specific program in that VM. Unfortunately, nobody has told me how I might do this.

Reasons I want to do this include things like preferring an older version of Microsoft Office, or linking to older games within game organizers like Launchbox.

If this is possible, how is it done? Is it possible?

In addition, would there be a way to incorporate attaching a given CD image/floppy image to that batch file, for games that require the disk be in the drive?
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Batch file as shortcut to guest program on host?

Post by scottgus1 »

A vast majority of those tasks can be done with "vboxmanage" commands, see the manual section 8. Most of such commands will be done with "vboxmanage startvm", "vboxmanage controlvm" and "vboxmanage modifyvm". Running a VM program is done with "vboxmanage guestcontrol". Some others, like vboxmanage guestproperty get/set", may be needed, too. Guest Additions will be needed inside the VM.

Read through that manual section and try to put together a batch file that will start a VM, wait for the VM to get booted up, then launch a program. Let's see what you can come up with, then we can guide you more
Lendri Mujina
Posts: 17
Joined: 29. Jun 2021, 00:33

Re: Batch file as shortcut to guest program on host?

Post by Lendri Mujina »

"Entry Point Not Found" and there's some problem with "RTFileGetSize". There's nothing about these things in the manual.

I get the same error regardless of whether I use start or run. And yes, I do set the directory to the folder VBoxManage.exe is in first.

Code: Select all

cd "C:\Users\[redacted]\Dropbox\PortableApps\Portable-VirtualBox\app64"
VBoxManage --nologo guestcontrol "Windows XP" run --exe "C:\\SIERRA\\PA4GOLD\\PRTARTST.EXE" --wait-exit --wait-stdout
(Important to note is that the folder name is a relic. I no longer use the unofficial Portable Virtualbox; I just installed the official one there so the shortcuts I made at the time wouldn't break, along with so that my data would sync between computers.)
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Batch file as shortcut to guest program on host?

Post by scottgus1 »

Lendri Mujina wrote:"Entry Point Not Found" and there's some problem with "RTFileGetSize".
Are these error messages you receive when you try:
Lendri Mujina wrote:VBoxManage --nologo guestcontrol "Windows XP" run --exe "C:\\SIERRA\\PA4GOLD\\PRTARTST.EXE" --wait-exit --wait-stdout
If so, please post the exact error message as it appears in the command window.

Start the VM from full normal shutdown, not save-state. Run until you see the problem happen, then shut down the VM from within the VM's OS if possible. If not possible, close the Virtualbox window for the VM with the Power Off option set.

Right-click the VM in the main Virtualbox window's VM list, choose Show Log. Save the far left tab's log and the hardening log next to it, zip them, and post the zip file, using the forum's Upload Attachment tab.
Lendri Mujina
Posts: 17
Joined: 29. Jun 2021, 00:33

Re: Batch file as shortcut to guest program on host?

Post by Lendri Mujina »

scottgus1 wrote:
Lendri Mujina wrote:"Entry Point Not Found" and there's some problem with "RTFileGetSize".
Are these error messages you receive when you try:
Lendri Mujina wrote:VBoxManage --nologo guestcontrol "Windows XP" run --exe "C:\\SIERRA\\PA4GOLD\\PRTARTST.EXE" --wait-exit --wait-stdout
If so, please post the exact error message as it appears in the command window.

Start the VM from full normal shutdown, not save-state. Run until you see the problem happen, then shut down the VM from within the VM's OS if possible. If not possible, close the Virtualbox window for the VM with the Power Off option set.

Right-click the VM in the main Virtualbox window's VM list, choose Show Log. Save the far left tab's log and the hardening log next to it, zip them, and post the zip file, using the forum's Upload Attachment tab.
It doesn't start the VM. The Command Prompt window appears, and then this message:

Code: Select all

[VBoxManage.exe - Entry Point Not Found]
The procedure entry point RTFileGetSize could not be located in the dynamic link library C:\[...]\VBoxManage.exe
Just now, I tried to see if substituting the UUID for the VM's name would work, but trying to check the UUID gave me the exact same error.

And then I also got the same error when I try to run VBoxManage from Windows Explorer. (As I'm aware VBoxManage isn't a GUI-based program, I didn't expect it to do anything if it worked, but I wanted to test if I would get that message even in that case. And I did.)

It also happens when I try to pause Dropbox syncing first (as that's how problems caused by Dropbox in programs are usually solved), and also when Dropbox is closed entirely.

The VM itself operates without any problems if started from the GUI.

EDIT: And now I just feel stupid. Agh. I forgot that I installed it to the normal directory on this particular machine. I'm sorry for wasting your time.

EDIT 2: ...and the batch file still doesn't work. After changing the path to the actual installation instead of the "ghost installation" I had it set to before, the VM still doesn't start; the command prompt window just closes without anything else happening. There are no error messages. This is also true when I run the .bat file as an administrator.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Batch file as shortcut to guest program on host?

Post by scottgus1 »

Lendri Mujina wrote:I forgot that I installed it to the normal directory
No problem! It's hard to remember everything we do to our computers... :D
Lendri Mujina wrote:the batch file still doesn't work.
Batch files crash out completely with a closed window if there is a syntax error in the command. The command window will stay open if you manually enter each command from the batch file into a command prompt one at a time. The you will be able to see the error message.

Running your command, tuned to my XP vm and asking for MSpaint to open, has the following error:
VBoxManage.exe: error: Unknown option: --wait-exit
The manual has an old/deprecated argument "--wait-exit" for the "guestcontrol" command in its examples.

This command started Paint on my XP and paused until I closed Paint in the VM:
VBoxManage --nologo guestcontrol "XP1" run --exe "C:\WINDOWS\system32\mspaint.exe" --wait-stdout --username snc --password daddy1
The XP account's username and password were required to get the program to start, otherwise an error "The specified user was not able to logon on guest" occurred.


One other thing:
Lendri Mujina wrote:"C:\\SIERRA\\PA4GOLD\\PRTARTST.EXE"
The double backslashes are not needed on Windows hosts, only singles. I tried double backslashes in my command above and it worked, but it's better to program as much as possible to the expected data pattern.
Lendri Mujina
Posts: 17
Joined: 29. Jun 2021, 00:33

Re: Batch file as shortcut to guest program on host?

Post by Lendri Mujina »

Current Batch file:

Code: Select all

cd "C:\Program Files\Oracle\VirtualBox" 
VBoxManage --nologo startvm "Windows XP"
VBoxManage --nologo guestcontrol "Windows XP" run --exe "C:\SIERRA\PA4GOLD\PRTARTST.EXE" --wait-stdout --username User --password admin
pause
Command Prompt text:

Code: Select all

C:\Users\tcbro\Dropbox\My PC (LAPTOP-M7R2B28K)\Desktop>cd "C:\Program Files\Oracle\VirtualBox"

C:\Program Files\Oracle\VirtualBox>VBoxManage --nologo startvm "Windows XP"
Waiting for VM "Windows XP" to power on...
VM "Windows XP" has been successfully started.

C:\Program Files\Oracle\VirtualBox>VBoxManage --nologo guestcontrol "Windows XP" run --exe "C:\SIERRA\PA4GOLD\PRTARTST.EXE" --wait-stdout --username User --password admin
VBoxManage.exe: error: Waiting for guest process (flags 0x1) failed: The guest execution service is not ready (yet)
VBoxManage.exe: error: Details: code VBOX_E_IPRT_ERROR (0x80bb0005), component GuestProcessWrap, interface IGuestProcess, callee IUnknown
VBoxManage.exe: error: Context: "WaitForArray(ComSafeArrayAsInParam(aWaitStartFlags), gctlRunGetRemainingTime(msStart, cMsTimeout), &waitResult)" at line 1497 of file VBoxManageGuestCtrl.cpp

C:\Program Files\Oracle\VirtualBox>pause
Press any key to continue . . .
The second portion doesn't wait until Windows actually logs in before trying to run.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Batch file as shortcut to guest program on host?

Post by scottgus1 »

Lendri Mujina wrote:The second portion doesn't wait until Windows actually logs in before trying to run.
Correct. The batch file will just plow on, because all 'startvm' does is start the Virtualbox 'hardware', it does not have any reporting on the state of the OS inside the VM.

You'll need something that can report when the VM OS has logged in and is ready to run programs. 'vboxmanage guestproperty' on the host and 'vboxcontrol guestproperty' in the VM provide a basic text communication channel.

Before running the 'guestcontrol' command, run 'vboxmanage guestproperty set' to write a custom guestproperty value, perhaps to 1. Then in a loop with a delay ('ping -n 5 127.0.0.1 > nul' is about a 5-second delay), run 'vboxmanage guestproperty get', and read the custom guestproperty to see if the value has cleared, perhaps to 0, at which point you break out of the loop and continue the batch file.

Inside the VM, make another batch file that runs 'vboxcontrol guestproperty set' to set the custom guestproperty to a cleared value, like 0, that the host batch file loop is waiting for. Set the start for this VM batch file so it runs when the VM OS is in the correct state. For an XP VM, for example, set an account to auto-log-on, and then put a shortcut to the vboxcontrol batch file in that account's Start menu Startup folder. Then the VM will clear the guestproperty value after it has fully logged on and is ready to receive guest control commands.
Lendri Mujina
Posts: 17
Joined: 29. Jun 2021, 00:33

Re: Batch file as shortcut to guest program on host?

Post by Lendri Mujina »

I... don't think I'm doing this right. But I have almost zero experience with batch files. With programming, I can understand the logic, it's always the syntax that stumps me...

Code: Select all

CD "C:\Program Files\Oracle\VirtualBox" 
VBOXMANAGE --nologo startvm "Windows XP"
VBOXMANAGE guestproperty set "Windows XP" 1
:DoUntil
PING -n 5 127.0.0.1 > nul
IF VBOXMANAGE guestproperty get "Windows XP"==0 GOTO EndLoope
ECHO "No response from guest. Retrying..."
pause
GOTO DoUntil
:EndLoope
ECHO "Response from guest recieved. Starting program."
pause
VBoxManage --nologo guestcontrol "Windows XP" run --exe "C:\SIERRA\PA4GOLD\PRTARTST.EXE" --wait-stdout --username User --password admin
pause
(The "pause"s are so I can read suspected error messages before it crashes.)
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Batch file as shortcut to guest program on host?

Post by scottgus1 »

You're getting there! Looks good except for a couple things:

1. I think you need a 'subkey' to hold the guestproperty. Instead of this:
VBOXMANAGE guestproperty set "Windows XP" 1
use this:

VBOXMANAGE guestproperty set "Windows XP" /vmcontrol/readykey 1

and in the VM:

VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey 0
VBOXCONTROL guestproperty set /vmcontrol/readykey 0
 Edit: The first version of this post had the VM name as an argument for "VBOXCONTROL". This is an error on my part. VBOXCONTROL does not use the VM's name in the command line. 
"vmcontrol" and "readykey" can be just about whatever words you want. Open the VM's .vbox file in Wordpad to see other guestproperties' key names.

*****************

2. I have not seen if a Windows batch file can use a whole command as an IF test. I believe this will not work:
IF VBOXMANAGE guestproperty get "Windows XP"==0 GOTO EndLoope
Pass the output of 'vboxmanage guestproperty get "Windows XP" ' to a text file, then find in the text file the output you hope to get.

'guestproperty get' outputs the value of the guestproperty with the text "value: " prefixed to the property value, like this:

Value: 0

So you need to find this text in the text file.

Try substituting the above quoted line with this:
vboxmanage guestproperty get "Windows XP" /vmcontrol/readykey > tempfile.txt
find /i /n "Value: 0" tempfile.txt > nul
if %Errorlevel%==0 goto EndLoope
'find' exit codes are 0 if the file contains the text and 1 or something else if it does not. '> nul' causes the text output of the command to not be printed; you can take '> nul' off if you want to see everything.
Lendri Mujina
Posts: 17
Joined: 29. Jun 2021, 00:33

Re: Batch file as shortcut to guest program on host?

Post by Lendri Mujina »

I'm so close... but unfortunately, either machine writing to the text file successfully is inconsistent. Sometimes the host writes it successfully. Sometimes the guest writes successfully. Usually neither. But never both on the same attempt.
I tried putting the text file in different directories with no change in results.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Batch file as shortcut to guest program on host?

Post by scottgus1 »

Lendri Mujina wrote:either machine writing to the text file successfully is inconsistent. ... Sometimes the guest writes successfully.
The guest 'vmcontrol guestproperty set' command is not supposed to write to a text file. The text file is only for gathering the output of the host 'vboxmanage guestproperty get' command on the host so 'find' can look through it.

I recall that when I used 'guestproperty' for comms between host and VM, on rare occasions the 'set' wouldn't set. I surmise that since 'set' is actually writing to the .vbox file, 'get' and 'set' might happen at the same time, and one or the other would fail. Repeating the 'set' a few times with a different delay than the host loop can de-synchronize the 'get' and 'set'. Three or four 'set's with a 'ping -n 1...' between should make the 'set' go through.

If this doesn't help, please post both host and VM batch files.
Lendri Mujina
Posts: 17
Joined: 29. Jun 2021, 00:33

Re: Batch file as shortcut to guest program on host?

Post by Lendri Mujina »

I feel like I'm learning a ton while learning nothing at all.

Host:

Code: Select all

CD "C:\Program Files\Oracle\VirtualBox"
::		 Sets path to where Vboxmanage is
VBOXMANAGE --nologo startvm "Windows XP"
::		 Just pushes the power button
VBOXMANAGE guestproperty set "Windows XP" /vmcontrol/readykey 1 > "C:\Users\tcbro\Dropbox\tempfile.txt"
::		 Makes a flag that indicates the host isn't ready yet
:DoUntil
::		 Waits until it gets the signal from the guest
SET /A varDelay=(%RANDOM% %%8)
::		 So conflicts won't happen
PING -n %varDelay% 127.0.0.1 > nul
::		 Pauses for (x) seconds -- This part is tested and working
vboxmanage guestproperty get "Windows XP" /vmcontrol/readykey > "C:\Users\tcbro\Dropbox\tempfile.txt"
::		 Reads the flag to see if it's been changed
find /i /n "Value: 0" tempfile.txt > nul
::		 
if %Errorlevel%==0 goto EndLoope
::		 Flag change detected. Start program.
ECHO No response from guest. Retrying...
GOTO DoUntil
:EndLoope
ECHO Response from guest recieved. Starting program.
VBoxManage --nologo guestcontrol "Windows XP" run --exe "C:\SIERRA\PA4GOLD\PRTARTST.EXE" --wait-stdout --username User --password admin
Guest:

Code: Select all

CD "C:\Program Files\Oracle\"
SET /A varDelay=(%RANDOM% %%8)
PING -n %varDelay% 127.0.0.1 > nul
VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
SET /A varDelay=(%RANDOM% %%8)
PING -n %varDelay% 127.0.0.1 > nul
VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
SET /A varDelay=(%RANDOM% %%8)
PING -n %varDelay% 127.0.0.1 > nul
VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
SET /A varDelay=(%RANDOM% %%8)
PING -n %varDelay% 127.0.0.1 > nul
VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
SET /A varDelay=(%RANDOM% %%8)
PING -n %varDelay% 127.0.0.1 > nul
VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
SET /A varDelay=(%RANDOM% %%8)
PING -n %varDelay% 127.0.0.1 > nul
VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
SET /A varDelay=(%RANDOM% %%8)
PING -n %varDelay% 127.0.0.1 > nul
VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
SET /A varDelay=(%RANDOM% %%8)
PING -n %varDelay% 127.0.0.1 > nul
VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
SET /A varDelay=(%RANDOM% %%8)
PING -n %varDelay% 127.0.0.1 > nul
VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
ECHO If the program hasn't opened by now, just shut down.
PAUSE
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Batch file as shortcut to guest program on host?

Post by scottgus1 »

Ok, you're doing well putting the batch files together, and the commenting shows you understand the commands. :D

Here, in the VM batch file, is where the glitch comes in:
Lendri Mujina wrote:VBOXCONTROL guestproperty set "Windows XP" /vmcontrol/readykey ON> tempfile.txt
First, "> tempfile.txt" is not necessary in this command. I surmise you've put Dropbox on both host and VM and are hoping to have the Dropbox'd 'tempfile.txt' record the changed 'guestproperty' on the VM so the host batch file will notice it. However, this is not how 'guestproperty' works. I'm not even sure that passing 'guestproperty' to the tempfile would produce usable output. In fact it might damage what output the host's text file is hoping to read.

'guestproperty' is a comm channel between Virtualbox and the Guest Additions. So the place to store the write, the 'guestproperty set', is fully taken care of by Virtualbox and does not need a text file. The only reason for passing the read, the 'guestproperty get', to a text file is so 'find' can read it.

The second problem is the value the VM is setting the guestproperty to. The VM sets it to ON, the host is looking for 0. :lol:

I also made a mistake in my usage of 'vboxcontrol' earlier, this command does not reference the VM name. Having the VM name in the command probably made the command syntax-error out and never run at all. Sorry about that.

Here is what you should use for the VM batch file command:
VBOXCONTROL guestproperty set /vmcontrol/readykey 0
One last thing, the randomized delays would not be necessary. An odd-numbered-second delay (three or greater) on the host script and an even number delay (greater than 1 but less than the host's delay) on the VM script will cause at least one 'guestproperty set' while the 'get' loop is delaying, and make the script work. Though the problems with the vboxcontrol command may have been the real problem. I'd go with 5 seconds in the host delay and 2 in the VM.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Batch file as shortcut to guest program on host?

Post by scottgus1 »

One other thing: in the host batch file, the first 'guestproperty set' that sets the "flag":
Lendri Mujina wrote:VBOXMANAGE guestproperty set "Windows XP" /vmcontrol/readykey 1 > "C:\Users\tcbro\Dropbox\tempfile.txt"
also does not need a redirect to a temp file.

Make this read like so:
VBOXMANAGE guestproperty set "Windows XP" /vmcontrol/readykey 1
'set' does not get redirected to a text file, only 'get' is redirected, so 'find' can search the text file.
Post Reply