Reading from VM process

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
HauptmannEck
Posts: 10
Joined: 24. Oct 2013, 20:35

Reading from VM process

Post by HauptmannEck »

Hello,
My system is making good progress but I have hit my next snag. I get my VM to startup, i can transfer an exe to it and run it, but I cannot seem to grab the output. the exe is a basic console app that just prints every second:
Loop: 1
Loop: 2
...
The code to run and read is as follows:

Code: Select all

            var progStart = vm.LaunchVMProcess(session, "gui", "");
            progStart.WaitForCompletion(5000);

            File.Copy(@"C:\Users\...\Documents\Visual Studio 2010\Projects\vBoxService\TestClient\bin\Debug\TestClient.exe", @"\\VMOFFICE2010\Users\Public\Documents\TestClient.exe");

            var console = session.Console;
            var guest = console.Guest;
            var guestSession = guest.CreateSession("admin", "admin", "", "anon");
            while (guestSession.Status != GuestSessionStatus.GuestSessionStatus_Started)
            {
                Thread.Sleep(100);
            }

            string[] arr = { "loop" };
            var progExe = guestSession.ProcessCreate(@"C:\Users\Public\Documents\TestClient.exe", arr, null, null, 0);
            progExe.WaitForArray(new ProcessWaitForFlag[] { ProcessWaitForFlag.ProcessWaitForFlag_StdOut }, 0);
            while (progExe.Status == ProcessStatus.ProcessStatus_Started)
            {
                var read = progExe.Read(0, 2048, 1000);
                Thread.Sleep(1000);
            }
When I look at what is being returned to 'read' it is always an empty array. {sbyte[0]}
not sure what I might be doing wrong.
Magnus Madsen
Posts: 22
Joined: 11. Jun 2013, 08:35
Primary OS: MS Windows 7
VBox Version: PUEL
Guest OSses: WinXP, WinVista, Win7, Win8

Re: Reading from VM process

Post by Magnus Madsen »

Hi Hauptmann,

You need to specify the WaitForStdOut flag when starting the process in order to be able to read from it. If you change the line to:

Code: Select all

var progExe = guestSession.ProcessCreate(@"C:\Users\Public\Documents\TestClient.exe", arr, null, new ProcessCreateFlag[] { ProcessCreateFlag.ProcessCreateFlag_WaitForStdOut }, 0);
You should be able to get data from the process.
In addition it looks like the handle you are using to read from the process is incorrect - change the line to:

Code: Select all

var read = progExe.Read(1, 65536, 1000);
Be aware though, that I have found reading from processes to be a bit finicky. For instance the aHandle to read from the process (which you specify as 0) but I changed to 1 - but I cannot find any documentation of this value. The same with the buffer size. I have found using a value less than 64k can often result in the VM hosting process crashing.
HauptmannEck
Posts: 10
Joined: 24. Oct 2013, 20:35

Re: Reading from VM process

Post by HauptmannEck »

Nice, I am now seeing a return, I just cant figure out how to convert the sbyte[] to a string, as the Read method says it returns byte[] but when debugging it returns sbyte[] so the compiler is getting uppity.

Once again Thanks for all the help you have given me.
HauptmannEck
Posts: 10
Joined: 24. Oct 2013, 20:35

Re: Reading from VM process

Post by HauptmannEck »

ok I figured out a solution to the convertion issue.

Code: Select all

                var read = progExe.Read(1, 65536, 1000);
                Byte[] sb = new byte[read.Length];
                for (var i=0; i < read.Length; i++)
                {
                    sb[i] = read[i];
                }
                var str = Encoding.UTF8.GetString(sb);
not sure if the sbyte to byte was required but it works
Post Reply