2015年11月1日 星期日

如何 執行 一個 執行檔 在 虛擬機裡 Programmatically executing a program in a VMware machine

如何 執行 一個 執行檔 在 虛擬機裡 Programmatically executing a program in a VMware machine  
  

As reported in the documentation of the VMware API, the function you need is VixVM_RunProgramInGuest(), which requires you to authenticate on the guest OS (the OS running on the virtual machine) with VixVM_LoginInGuest().

// Authenticate for guest operations.   
jobHandle = VixVM_LoginInGuest(vmHandle,
  "vixuser", // userName
  "secret",  // password 
  0,         // options
  NULL,      // callbackProc
  NULL       // clientData
); 

/*
 VIX_E_PROGRAM_NOT_STARTED error code.
*/

err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);    

if (VIX_OK != err) {
  // Handle the error.
  goto abort;
}

Vix_ReleaseHandle(jobHandle);

// Run the target program.  
jobHandle = VixVM_RunProgramInGuest(vmHandle,
  "c:\\myProgram.exe",
  "/flag arg1 arg2",
  0,                  // options
  VIX_INVALID_HANDLE, // propertyListHandle
  NULL,               // callbackProc
  NULL                // clientData
); 
/*
 VIX_E_PROGRAM_NOT_STARTED error code.

VixHandle
VixVM_RunProgramInGuest(VixHandle vmHandle,
const char *guestProgramName,
const char *commandLineArgs,
VixRunProgramOptions options,
VixHandle propertyListHandle,
VixEventProc *callbackProc,
void *clientData);

*/
err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE);

if (VIX_OK != err) {
  // Handle the error.
  goto abort;
}

Vix_ReleaseHandle(jobHandle);
The part to connect to the virtual machine server is the following one.

jobHandle = VixHost_Connect(VIX_API_VERSION,
  VIX_SERVICEPROVIDER_VMWARE_SERVER,
  NULL,               // hostName
  0,                  // hostPort
  NULL,               // userName
  NULL,               // password
  0,                  // options
  VIX_INVALID_HANDLE, // propertyListHandle
  NULL,               // callbackProc
  NULL                // clientData
); 

err = VixJob_Wait(jobHandle, VIX_PROPERTY_JOB_RESULT_HANDLE, &hostHandle, VIX_PROPERTY_NONE);

if (VIX_OK != err) {
  // Handle the error.
  goto abort;
}

Vix_ReleaseHandle(jobHandle);


The documentation has an example on how to call a program in the guest OS; it's a complete example that shows how to connect to the virtual machine server, open a file defining a virtual machine, and power it on. The essential code is the following one; you should read the complete example, though.

沒有留言: