I am working on an app that need to execute a program in the guest, send input to it and get output from it. I use the COM API, but there are several problems and I now need some help on this.
I use C# NET 4.0, host is Win7 64-bit, the guest OS is WinXP 32-bit, VirtualBox is ver 4.2.10.
I need to be able to interact with any program on the guest i have executed. But because I also need to get the current IP addresses in the guest, I have used ipconfig as a first attempt.
The problem now is that i get an exception (see code below) that i can't get around. The return type for IGuestProcess.Read() is System.Array (that is at least what mouse over in VisualStudio is showing and that is the only return type that compiles). But when the code is executed the exception suggests that the return type should be something else. HELP!!!!
The code i use to execute ipconfig and get output from it looks like this (simplified for brevity):
Code: Select all
// Get GuestSession
session = new Session(); // (Virtualbox running on x64 OS requires that this prog is also running as x64!)
machine.LockMachine(session, LockType.LockType_Shared);
guestSession = session.Console.Guest.CreateSession(userName, password, "", "");
// Setup for Command Execution
var cmdArgs = new string[] { }; // Empty arguments array
var envVars = new string[] { }; // Empty environment vars
var flags = new ProcessCreateFlag[] {
ProcessCreateFlag.ProcessCreateFlag_WaitForStdOut,
};
// Execute ipconfig on guest
var guestProcess = guestSession.ProcessCreate(@"c:\windows\system32\ipconfig.exe", cmdArgs, envVars, flags, 5000);
if (guestProcess == null) {
// Return error. Code omitted for brevity
return mrd;
}
// Wait up to 5 seconds for process to start
var waitCount = 0;
if (guestProcess.Status != ProcessStatus.ProcessStatus_Error) {
while (waitCount <= 200 && guestProcess.Status != ProcessStatus.ProcessStatus_Started && guestProcess.Status != ProcessStatus.ProcessStatus_Error) {
Debug.WriteLine("Waiting... ({0})", waitCount);
waitCount++;
Thread.Sleep(25);
}
}
// Timeout or process error?
if (waitCount > 200 || guestProcess.Status != ProcessStatus.ProcessStatus_Started) {
// Return error. Code omitted for brevity
}
// Read output from ipconfig
System.Array output = guestProcess.Read(0, 2000, 5000); // Exception!!!
// Exception on line above:
// 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)