mark一下,为了学习VIX API,花了两天时间才搞定,要么编译不过,各种link的lib错误,要么是VixHost_Connect连接不上,要么是VixVM_Open打开失败,今天总算是搞定了,下面是一个示例, 效果是启动VMware的界面并启动虚拟机系统,然后再关机.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include "vix.h" #pragma comment(lib,"user32.lib") //#pragma comment(lib,"C:\\Program Files (x86)\\VMware\\VMware VIX\\VixAllProducts.lib") //#pragma comment(lib,"C:\\Program Files (x86)\\VMware\\VMware VIX\\VixAllProductsd.lib") //#pragma comment(lib,"C:\\Program Files (x86)\\VMware\\VMware VIX\\VixAllProductsDyn.lib") #pragma comment(lib,"C:\\Program Files (x86)\\VMware\\VMware VIX\\Workstation-10.0.0-and-vSphere-5.5.0\\32bit\\vix.lib") #pragma comment(lib,"C:\\Program Files (x86)\\VMware\\VMware VIX\\Workstation-10.0.0-and-vSphere-5.5.0\\32bit\\vixd.lib") #pragma comment(lib,"ws2_32.lib") #define CONNTYPE VIX_SERVICEPROVIDER_VMWARE_WORKSTATION #define HOSTNAME "" #define HOSTPORT 0 #define USERNAME "" #define PASSWORD "" #define VMPOWEROPTIONS VIX_VMPOWEROP_LAUNCH_GUI int main() { VixError err; char *vmxPath="E:\\vm\\Windows XP AVG\\Windows XP AVG.vmx"; VixHandle hostHandle = VIX_INVALID_HANDLE; VixHandle jobHandle = VIX_INVALID_HANDLE; VixHandle vmHandle = VIX_INVALID_HANDLE; jobHandle = VixHost_Connect(VIX_API_VERSION, CONNTYPE, HOSTNAME, // *hostName, HOSTPORT, // hostPort, USERNAME, // *userName, PASSWORD, // *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_FAILED(err)) { goto abort; } Vix_ReleaseHandle(jobHandle); jobHandle = VIX_INVALID_HANDLE; jobHandle = VixVM_Open(hostHandle, vmxPath, NULL, // VixEventProc *callbackProc, NULL); // void *clientData); err = VixJob_Wait(jobHandle, VIX_PROPERTY_JOB_RESULT_HANDLE, &vmHandle, VIX_PROPERTY_NONE); if (VIX_FAILED(err)) { printf("VM_Open error\n"); goto abort; } Vix_ReleaseHandle(jobHandle); jobHandle = VixVM_PowerOn(vmHandle, VMPOWEROPTIONS, VIX_INVALID_HANDLE, NULL, // *callbackProc, NULL); // *clientData); err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE); if (VIX_FAILED(err)) { goto abort; } Vix_ReleaseHandle(jobHandle); jobHandle = VixVM_PowerOff(vmHandle, VIX_VMPOWEROP_NORMAL, NULL, // *callbackProc, NULL); // *clientData); err = VixJob_Wait(jobHandle, VIX_PROPERTY_NONE); if (VIX_FAILED(err)) { goto abort; } abort: Vix_ReleaseHandle(jobHandle); Vix_ReleaseHandle(vmHandle); VixHost_Disconnect(hostHandle); return 0; } |
转载请注明:exchen's blog » VMware Vix API 操作虚拟机