VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxHeadless/testcase/tstHeadless.cpp@ 31905

Last change on this file since 31905 was 31070, checked in by vboxsync, 15 years ago

Main: rename ISession::close() to ISession::unlockMachine(); API documentation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1/** @file
2 *
3 * VBox frontends: VBoxHeadless frontend:
4 * Testcases
5 */
6
7/*
8 * Copyright (C) 2006-2009 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include <VBox/com/com.h>
20#include <VBox/com/string.h>
21#include <VBox/com/Guid.h>
22#include <VBox/com/ErrorInfo.h>
23#include <VBox/com/errorprint.h>
24#include <VBox/com/EventQueue.h>
25
26#include <VBox/com/VirtualBox.h>
27
28using namespace com;
29
30#include <VBox/log.h>
31#include <iprt/initterm.h>
32#include <iprt/stream.h>
33
34
35////////////////////////////////////////////////////////////////////////////////
36
37/**
38 * Entry point.
39 */
40int main (int argc, char **argv)
41{
42 // initialize VBox Runtime
43 RTR3Init();
44
45 // the below cannot be Bstr because on Linux Bstr doesn't work
46 // until XPCOM (nsMemory) is initialized
47 const char *name = NULL;
48 const char *operation = NULL;
49
50 // parse the command line
51 if (argc > 1)
52 name = argv [1];
53 if (argc > 2)
54 operation = argv [2];
55
56 if (!name || !operation)
57 {
58 RTPrintf ("\nUsage:\n\n"
59 "%s <machine_name> [on|off|pause|resume]\n\n",
60 argv [0]);
61 return -1;
62 }
63
64 RTPrintf ("\n");
65 RTPrintf ("tstHeadless STARTED.\n");
66
67 RTPrintf ("VM name : {%s}\n"
68 "Operation : %s\n\n",
69 name, operation);
70
71 HRESULT rc;
72
73 rc = com::Initialize();
74 if (FAILED(rc))
75 {
76 RTPrintf("ERROR: failed to initialize COM!\n");
77 return rc;
78 }
79
80 do
81 {
82 ComPtr <IVirtualBox> virtualBox;
83 ComPtr <ISession> session;
84
85 RTPrintf ("Creating VirtualBox object...\n");
86 rc = virtualBox.createLocalObject (CLSID_VirtualBox);
87 if (FAILED(rc))
88 RTPrintf("ERROR: failed to create the VirtualBox object!\n");
89 else
90 {
91 rc = session.createInprocObject(CLSID_Session);
92 if (FAILED(rc))
93 RTPrintf("ERROR: failed to create a session object!\n");
94 }
95
96 if (FAILED (rc))
97 {
98 com::ErrorInfo info;
99 if (!info.isFullAvailable() && !info.isBasicAvailable())
100 {
101 com::GluePrintRCMessage(rc);
102 RTPrintf("Most likely, the VirtualBox COM server is not running or failed to start.\n");
103 }
104 else
105 com::GluePrintErrorInfo(info);
106 break;
107 }
108
109 // create the event queue
110 // (here it is necessary only to process remaining XPCOM/IPC events
111 // after the session is closed)
112 EventQueue eventQ;
113
114 ComPtr <IMachine> m;
115
116 // find ID by name
117 CHECK_ERROR_BREAK(virtualBox, FindMachine(Bstr(name), m.asOutParam()));
118
119 if (!strcmp(operation, "on"))
120 {
121 ComPtr <IProgress> progress;
122 RTPrintf ("Opening a new (remote) session...\n");
123 CHECK_ERROR_BREAK (m,
124 LaunchVMProcess(session, Bstr("vrdp"),
125 NULL, progress.asOutParam()));
126
127 RTPrintf ("Waiting for the remote session to open...\n");
128 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
129
130 BOOL completed;
131 CHECK_ERROR_BREAK (progress, COMGETTER(Completed) (&completed));
132 ASSERT (completed);
133
134 LONG resultCode;
135 CHECK_ERROR_BREAK (progress, COMGETTER(ResultCode) (&resultCode));
136 if (FAILED (resultCode))
137 {
138 ComPtr<IVirtualBoxErrorInfo> errorInfo;
139 CHECK_ERROR_BREAK(progress,
140 COMGETTER(ErrorInfo) (errorInfo.asOutParam()));
141 ErrorInfo info(errorInfo, COM_IIDOF(IVirtualBoxErrorInfo));
142 com::GluePrintErrorInfo(info);
143 }
144 else
145 {
146 RTPrintf ("Remote session has been successfully opened.\n");
147 }
148 }
149 else
150 {
151 RTPrintf ("Opening an existing session...\n");
152 CHECK_ERROR_BREAK(m, LockMachine(session, LockType_Shared));
153
154 ComPtr <IConsole> console;
155 CHECK_ERROR_BREAK(session, COMGETTER(Console)(console.asOutParam()));
156
157 if (!strcmp (operation, "off"))
158 {
159 ComPtr <IProgress> progress;
160 RTPrintf ("Powering the VM off...\n");
161 CHECK_ERROR_BREAK (console, PowerDown(progress.asOutParam()));
162
163 RTPrintf ("Waiting for the VM to power down...\n");
164 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
165
166 BOOL completed;
167 CHECK_ERROR_BREAK (progress, COMGETTER(Completed) (&completed));
168 ASSERT (completed);
169
170 LONG resultCode;
171 CHECK_ERROR_BREAK (progress, COMGETTER(ResultCode) (&resultCode));
172 if (FAILED (resultCode))
173 {
174 ComPtr<IVirtualBoxErrorInfo> errorInfo;
175 CHECK_ERROR_BREAK(progress,
176 COMGETTER(ErrorInfo) (errorInfo.asOutParam()));
177 ErrorInfo info(errorInfo, COM_IIDOF(IVirtualBoxErrorInfo));
178 com::GluePrintErrorInfo(info);
179 }
180 else
181 {
182 RTPrintf ("VM is powered down.\n");
183 }
184 }
185 else
186 if (!strcmp (operation, "pause"))
187 {
188 RTPrintf ("Pausing the VM...\n");
189 CHECK_ERROR_BREAK (console, Pause());
190 }
191 else
192 if (!strcmp (operation, "resume"))
193 {
194 RTPrintf ("Resuming the VM...\n");
195 CHECK_ERROR_BREAK (console, Resume());
196 }
197 else
198 {
199 RTPrintf ("Invalid operation!\n");
200 }
201 }
202
203 RTPrintf("Closing the session (may fail after power off)...\n");
204 CHECK_ERROR(session, UnlockMachine());
205 }
206 while (0);
207 RTPrintf ("\n");
208
209 com::Shutdown();
210
211 RTPrintf ("tstHeadless FINISHED.\n");
212
213 return rc;
214}
215
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette