0

Ok, so I'm having an issue starting the the Windows XP mode VM through a c# program. The command I'm using is vmwindow -file "absolute path to vmcx file" , but the problem is that the command does not work with the cmd prompt that my program kicks off. So, it's very weird. I can go to command prompt on my computer and run this command on my computer and it works, but if i have the same command on my c# program, the command prompt that pops up tells me the "vmwindow" is not a recognized command. I even looked at the paths of each of the command prompts and they're different, but they still both contain "C:\Windows\system32\" which is where vmwindow.exe exists. So, I navigate on the command prompt window that my program populated and the file "vmwindow.exe" is not there, but if I open a command prompt window from my computer and navigate to that folder, it exists there. I can't think of anything else as I already made sure they're both running in administrator mode, and also i tried starting a bat file which contained that command instead of running the command directly. Hope anyone knows anything about this. Here is the code I'm using:

private void button1_Click(object sender, EventArgs e)
    { 

        Process process = new System.Diagnostics.Process();
        ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        startInfo.FileName = "cmd.exe";
        startInfo.WorkingDirectory = @"<my path>";
        startInfo.Arguments = "/k vmwindow.exe -file \"<path to vcmx file>\\Windows XP Mode.vmcx\"";
        process.StartInfo = startInfo;
        process.Start();
    }
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • The program (file name) should be `vmwindow.exe`, not cmd.exe. The arguments are everything after `vmwindow` in your example. – Ron Beyer Jul 15 '15 at 15:03
  • Ok, i tried this and it gave me this: An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll Additional information: The system cannot find the file specified. I feel this has to do with the command prompt window not finding vmwindow.exe when i navigate to that folder within the command prompt window that is populated by visual studio. – user1919646 Jul 15 '15 at 15:09
  • You need to specify the full path to `vmwindow.exe`. – Ron Beyer Jul 15 '15 at 15:09
  • same error. I tried : startInfo.FileName = "C:\\Windows\\system32\\vmwindow.exe"; – user1919646 Jul 15 '15 at 15:13

2 Answers2

1

What you can do is using Powershell. It has a native integration for Hyper V control and is easy to call from c#

You can see all HV-cmdlets here

a simple command to start your machine would be

Start-VM "Windows 8.1 Pro" -Computername HV-Host1
// etcetc
Stop-VM "Windows 8.1 Pro" -Save

So this should be something like this in C#

using (PowerShell PowerShellInstance = PowerShell.Create())
{
    PowerShellInstance.AddScript("Start-VM "Windows 8.1 Pro" -Computername HV-Host1");
}
Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41
0

Probably it's because of the bitness setting you compile your program with. ("Platform target" and "Prefer 32-bit" settings under the build tab of the project). 32 and 64 bit processes see different files under System32. See https://stackoverflow.com/a/950011

Community
  • 1
  • 1
Loonquawl
  • 1,058
  • 1
  • 11
  • 16
  • yeah, tried this and this works. It is now starting my vm. I changed the platform target to "x64" which automatically unchekced the "Prefer 32-bit" setting, and it works now. Thanks!! – user1919646 Jul 15 '15 at 15:31