[vbox-dev] COM API ProcessCreate and GuestProcess.Write

LeeEll info at cando.se
Tue Apr 9 13:28:21 PDT 2013


> Sounds extremely clumsy - you're trying to implement functionality which 
> the guest additions provide in a much easier way: by setting guest 
> properties which you can query. The complete network configuration 
> should be available in the /VirtualBox/GuestInfo/Net/* properties. So no 
> need to execute something at all in the guest.

Yes, you are right Klaus. However, i still need a way to get output from a 
program i have executed on the guest and some way to send information to 
that program. /VirtualBox/GuestInfo/Net/* is no solution!

I have change the code so i execute ipconfig directly (not via cmd.exe) 
but i can't get the output from it.

Simplified process start code is now:
-------------------------------------
var cmdArgs = new string[] { }; // Empty arguments array
var envVars = new string[] { }; // Empty environment vars
var flags = new ProcessCreateFlag[] {
  ProcessCreateFlag.ProcessCreateFlag_WaitForStdOut};
var guestProcess = guestSession.ProcessCreate
( <at> "c:\windows\system32\ipconfig.exe", cmdArgs, envVars, flags, 5000);


Code to read output is:
-----------------------
System.Array output = guestProcess.Read(0, 2000, 5000);

The problem is line above that generates the following exception:
Specified array was not of the expected type.
   at System.StubHelpers.MngdSafeArrayMarshaler.ConvertSpaceToManaged
(IntPtr pMarshalState, Object& pManagedHome, IntPtr pNativeHome)
   at VirtualBox.IGuestProcess.Read(UInt32 aHandle, UInt32 aToRead, UInt32 
aTimeoutMS)

I don't understand why this happens. The output type for Read method is 
System.Array.

I really need help with this!!!!!!!!







Klaus Espenlaub <klaus.espenlaub at ...> writes:

> 
> Hi,
> 
> On 08.04.2013 23:01, LeeEll wrote:
> > Hi Everybody!
> >
> > I am working on a C# (.net 4.0) app that manages VirtualBox (version 
4.2.1
> > r84104) on a number of slave machines. The slave machines run Win 7 64-
bit
> > and the guest OS is win XP 32-bit. I am using the COM API to manage VB 
on
> > the slave machines.
> >
> > My manager app now needs to be able to execute a program on the guest 
OS,
> > provide command parameters to it and to give it input via stdin and to 
get
> > output from stdout. The first use of this would be to open a command
> > window on the guest, execute command “ipconfig” and get the current IP
> > addresses from the command window output. But I can’t get it to work.
> 
> Sounds extremely clumsy - you're trying to implement functionality which 
> the guest additions provide in a much easier way: by setting guest 
> properties which you can query. The complete network configuration 
> should be available in the /VirtualBox/GuestInfo/Net/* properties. So no 
> need to execute something at all in the guest.
> 
> > I get an exception when I try to write the ipconfig command to stdin. 
See
> > code below. As an alternative to writing to the process I have tried to
> > give the ipconfig command in the arguments for ProcessCreate, but that
> > doesn’t work either. The command window is opened in the guest OS, but 
it
> > just shows a blank window with a blinking caret, no path or any of the
> > other stuff that is normally shown in a new command window. If I click 
in
> > the command window and try to write something, nothing happens. I don’t
> > know if this is an indication of a problem or not. I guess it might be
> > that VB has captured stdin/stdout?
> 
> Can't follow at all why you want to start ipconfig mysteriously through 
> sending the command to stdin. Super strange approach. Start ipconfig.exe 
> directly, not through cmd.exe.
> 
> Your code lacks the necessary flags to get the output back anyway, and 
> your timeout values are quite ambitious - 500ms for the process start 
> and 1 second for the completion. This can easily fail. Multiply those 
> timeouts by at least a factor of 10.
> 
> > If I change the code so the command is “c:\windows\notepad.exe“, I get 
a
> > fully functioning notepad window in the guest OS. So the code for
> > executing a file on the guest seems to work. I haven’t tried to use
> > guestProcess.Write on anything other than the cmd.exe window.
> 
> > Can someone please show me how I can use C# and the COM API to execute 
a
> > program (including cmd.exe) on the guest OS and then provide input to 
it
> > and get output from it?
> 
> Never used C#, but others did successfully.
> 
> You can still have a look at the source code of VBoxManage, because that 
> contains code to invoke arbitrary commands - and also uses the COM API.
> 
> Klaus
> 





More information about the vbox-dev mailing list