1 | #include <iostream>
|
---|
2 | #include <iomanip>
|
---|
3 |
|
---|
4 | #include <nsMemory.h>
|
---|
5 | #include <nsString.h>
|
---|
6 | #include <nsIServiceManager.h>
|
---|
7 | #include <nsEventQueueUtils.h>
|
---|
8 |
|
---|
9 | #include <iprt/string.h>
|
---|
10 |
|
---|
11 | #include "VirtualBox_XPCOM.h"
|
---|
12 | #include "tstLinuxC.h"
|
---|
13 |
|
---|
14 | using namespace std;
|
---|
15 |
|
---|
16 | static ISession *Session;
|
---|
17 | static IVirtualBox *Ivirtualbox;
|
---|
18 | static nsIServiceManager *serviceManager;
|
---|
19 | static nsIComponentManager *manager;
|
---|
20 |
|
---|
21 | int VBoxUtf16ToUtf8(const PRUnichar *pszString, char **ppwszString)
|
---|
22 | {
|
---|
23 | return RTUtf16ToUtf8(pszString, ppwszString);
|
---|
24 | }
|
---|
25 |
|
---|
26 | int VBoxStrToUtf16(const char *pszString, PRUnichar **ppwszString)
|
---|
27 | {
|
---|
28 | return RTStrToUtf16(pszString, ppwszString);
|
---|
29 | }
|
---|
30 |
|
---|
31 | void VBoxUtf16Free(PRUnichar *pwszString)
|
---|
32 | {
|
---|
33 | RTUtf16Free(pwszString);
|
---|
34 | }
|
---|
35 |
|
---|
36 | void VBoxStrFree(char *pszString)
|
---|
37 | {
|
---|
38 | RTStrFree(pszString);
|
---|
39 | }
|
---|
40 |
|
---|
41 | const PRUnichar* VBoxConvertUTF8toPRUnichar(char *src)
|
---|
42 | {
|
---|
43 | return NS_ConvertUTF8toUTF16(src).get();
|
---|
44 | }
|
---|
45 |
|
---|
46 | const char* VBoxConvertPRUnichartoUTF8(PRUnichar *src)
|
---|
47 | {
|
---|
48 | return NS_ConvertUTF16toUTF8(src).get();
|
---|
49 | }
|
---|
50 |
|
---|
51 | const PRUnichar* VBoxConvertAsciitoPRUnichar(char *src)
|
---|
52 | {
|
---|
53 | return NS_ConvertASCIItoUTF16(src).get();
|
---|
54 | }
|
---|
55 |
|
---|
56 | const char* VBoxConvertPRUnichartoAscii(PRUnichar *src)
|
---|
57 | {
|
---|
58 | return NS_LossyConvertUTF16toASCII(src).get();
|
---|
59 | }
|
---|
60 |
|
---|
61 | void VBoxComUnallocStr(PRUnichar *str_dealloc)
|
---|
62 | {
|
---|
63 | if (str_dealloc) {
|
---|
64 | nsMemory::Free(str_dealloc);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | void VBoxComUnallocIID(nsIID *iid)
|
---|
69 | {
|
---|
70 | if (iid) {
|
---|
71 | nsMemory::Free(iid);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | void VBoxComInitialize(IVirtualBox **virtualBox, ISession **session)
|
---|
76 | {
|
---|
77 | nsresult rc;
|
---|
78 |
|
---|
79 | *session = NULL;
|
---|
80 | *virtualBox = NULL;
|
---|
81 |
|
---|
82 | Session = *session;
|
---|
83 | Ivirtualbox = *virtualBox;
|
---|
84 |
|
---|
85 | // All numbers on stderr in hex prefixed with 0X.
|
---|
86 | cerr.setf(ios_base::showbase | ios_base::uppercase);
|
---|
87 | cerr.setf(ios_base::hex, ios_base::basefield);
|
---|
88 |
|
---|
89 | rc = NS_InitXPCOM2(&serviceManager, nsnull, nsnull);
|
---|
90 | if (NS_FAILED(rc))
|
---|
91 | {
|
---|
92 | cerr << "XPCOM could not be initialized! rc=" << rc << endl;
|
---|
93 | VBoxComUninitialize();
|
---|
94 | return;
|
---|
95 | }
|
---|
96 |
|
---|
97 | rc = NS_GetComponentManager (&manager);
|
---|
98 | if (NS_FAILED(rc))
|
---|
99 | {
|
---|
100 | cerr << "could not get component manager! rc=" << rc << endl;
|
---|
101 | VBoxComUninitialize();
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | rc = manager->CreateInstanceByContractID(NS_VIRTUALBOX_CONTRACTID,
|
---|
106 | nsnull,
|
---|
107 | NS_GET_IID(IVirtualBox),
|
---|
108 | (void **)virtualBox);
|
---|
109 | if (NS_FAILED(rc))
|
---|
110 | {
|
---|
111 | cerr << "could not instantiate VirtualBox object! rc=" << rc << endl;
|
---|
112 | VBoxComUninitialize();
|
---|
113 | return;
|
---|
114 | }
|
---|
115 |
|
---|
116 | cout << "VirtualBox object created." << endl;
|
---|
117 |
|
---|
118 | rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID,
|
---|
119 | nsnull,
|
---|
120 | NS_GET_IID(ISession),
|
---|
121 | (void **)session);
|
---|
122 | if (NS_FAILED(rc))
|
---|
123 | {
|
---|
124 | cerr << "could not instantiate Session object! rc=" << rc << endl;
|
---|
125 | VBoxComUninitialize();
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | cout << "ISession object created." << endl;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void VBoxComUninitialize(void)
|
---|
133 | {
|
---|
134 | if (Session)
|
---|
135 | NS_RELEASE(Session); // decrement refcount
|
---|
136 | if (Ivirtualbox)
|
---|
137 | NS_RELEASE(Ivirtualbox); // decrement refcount
|
---|
138 | if (manager)
|
---|
139 | NS_RELEASE(manager); // decrement refcount
|
---|
140 | if (serviceManager)
|
---|
141 | NS_RELEASE(serviceManager); // decrement refcount
|
---|
142 | NS_ShutdownXPCOM(nsnull);
|
---|
143 | cout << "Done!" << endl;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /* vim: set ts=4 sw=4 et: */
|
---|