Changeset 19477 in vbox
- Timestamp:
- May 7, 2009 11:06:29 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/testcase/tstVBoxAPIWin.cpp
r17737 r19477 58 58 #include "VirtualBox.h" 59 59 60 #define SAFE_RELEASE(x) \ 61 if (x) { \ 62 x->Release(); \ 63 x = NULL; \ 64 } 60 65 61 66 int listVMs(IVirtualBox *virtualBox) … … 96 101 } 97 102 103 98 104 int testErrorInfo(IVirtualBox *virtualBox) 99 105 { 100 106 HRESULT rc; 101 107 102 /* try to find a machine that doesn't exist */108 /* Try to find a machine that doesn't exist */ 103 109 IMachine *machine = NULL; 104 110 BSTR machineName = SysAllocString(L"Foobar"); … … 133 139 } 134 140 135 if (machine) 136 machine->Release(); 137 141 SAFE_RELEASE(machine); 138 142 SysFreeString(machineName); 139 143 … … 142 146 143 147 148 int testStartVM(IVirtualBox *virtualBox) 149 { 150 HRESULT rc; 151 152 /* Try to start a VM called "WinXP SP2". */ 153 IMachine *machine = NULL; 154 BSTR machineName = SysAllocString(L"WinXP SP2"); 155 156 rc = virtualBox->FindMachine(machineName, &machine); 157 158 if (FAILED(rc)) 159 { 160 IErrorInfo *errorInfo; 161 162 rc = GetErrorInfo(0, &errorInfo); 163 164 if (FAILED(rc)) 165 printf("Error getting error info! rc = 0x%x\n", rc); 166 else 167 { 168 BSTR errorDescription = NULL; 169 170 rc = errorInfo->GetDescription(&errorDescription); 171 172 if (FAILED(rc) || !errorDescription) 173 printf("Error getting error description! rc = 0x%x\n", rc); 174 else 175 { 176 printf("Successfully retrieved error description: %S\n", errorDescription); 177 178 SysFreeString(errorDescription); 179 } 180 181 SAFE_RELEASE(errorInfo); 182 } 183 } 184 else 185 { 186 ISession *session = NULL; 187 IConsole *console = NULL; 188 IProgress *progress = NULL; 189 BSTR sessiontype = SysAllocString(L"gui"); 190 BSTR guid; 191 192 do 193 { 194 rc = machine->get_Id(&guid); /* Get the GUID of the machine. */ 195 if (!SUCCEEDED(rc)) 196 { 197 printf("Error retrieving machine ID! rc = 0x%x\n", rc); 198 break; 199 } 200 201 /* Create the session object. */ 202 rc = CoCreateInstance(CLSID_Session, /* the VirtualBox base object */ 203 NULL, /* no aggregation */ 204 CLSCTX_INPROC_SERVER, /* the object lives in a server process on this machine */ 205 IID_ISession, /* IID of the interface */ 206 (void**)&session); 207 if (!SUCCEEDED(rc)) 208 { 209 printf("Error creating Session instance! rc = 0x%x\n", rc); 210 break; 211 } 212 213 /* Start a VM session using the delivered VBox GUI. */ 214 rc = virtualBox->OpenRemoteSession (session, guid, sessiontype, 215 NULL, &progress); 216 if (!SUCCEEDED(rc)) 217 { 218 printf("Could not open remote session! rc = 0x%x\n", rc); 219 break; 220 } 221 222 /* Wait until VM is running. */ 223 printf ("Starting VM, please wait ...\n"); 224 rc = progress->WaitForCompletion (-1); 225 226 /* Get console object. */ 227 session->get_Console(&console); 228 229 /* Bring console window to front. */ 230 machine->ShowConsoleWindow(0); 231 232 printf ("Press enter to power off VM and close the session...\n"); 233 getchar(); 234 235 /* Power down the machine. */ 236 rc = console->PowerDown(); 237 238 /* Close the session. */ 239 rc = session->Close(); 240 241 } while (0); 242 243 SAFE_RELEASE(console); 244 SAFE_RELEASE(progress); 245 SAFE_RELEASE(session); 246 SysFreeString(guid); 247 SysFreeString(sessiontype); 248 SAFE_RELEASE(machine); 249 } 250 251 SysFreeString(machineName); 252 253 return 0; 254 } 255 256 144 257 int main(int argc, char *argv[]) 145 258 { … … 149 262 do 150 263 { 151 /* initialize the COM subsystem*/264 /* Initialize the COM subsystem. */ 152 265 CoInitialize(NULL); 153 266 154 /* instantiate the VirtualBox root object*/267 /* Instantiate the VirtualBox root object. */ 155 268 rc = CoCreateInstance(CLSID_VirtualBox, /* the VirtualBox base object */ 156 269 NULL, /* no aggregation */ … … 169 282 testErrorInfo(virtualBox); 170 283 171 /* release the VirtualBox object */ 284 /* Enable the following line to get a VM started. */ 285 //testStartVM(virtualBox); 286 287 /* Release the VirtualBox object. */ 172 288 virtualBox->Release(); 173 289
Note:
See TracChangeset
for help on using the changeset viewer.