1 | /** @file libXPCOMtoC.cpp
|
---|
2 | *
|
---|
3 | * Utility functions to use with the C binding for XPCOM.
|
---|
4 | *
|
---|
5 | * $Id: libXPCOMtoC.cpp 16483 2009-02-03 10:52:11Z vboxsync $
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include <iostream>
|
---|
25 | #include <iomanip>
|
---|
26 |
|
---|
27 | #include <nsMemory.h>
|
---|
28 | #include <nsString.h>
|
---|
29 | #include <nsIServiceManager.h>
|
---|
30 | #include <nsEventQueueUtils.h>
|
---|
31 |
|
---|
32 | #include <iprt/string.h>
|
---|
33 |
|
---|
34 | #include "VirtualBox_XPCOM.h"
|
---|
35 | #include "cbinding.h"
|
---|
36 |
|
---|
37 | using namespace std;
|
---|
38 |
|
---|
39 | static ISession *Session;
|
---|
40 | static IVirtualBox *Ivirtualbox;
|
---|
41 | static nsIServiceManager *serviceManager;
|
---|
42 | static nsIComponentManager *manager;
|
---|
43 |
|
---|
44 | int VBoxUtf16ToUtf8(const PRUnichar *pszString, char **ppwszString)
|
---|
45 | {
|
---|
46 | return RTUtf16ToUtf8(pszString, ppwszString);
|
---|
47 | }
|
---|
48 |
|
---|
49 | int VBoxStrToUtf16(const char *pszString, PRUnichar **ppwszString)
|
---|
50 | {
|
---|
51 | return RTStrToUtf16(pszString, ppwszString);
|
---|
52 | }
|
---|
53 |
|
---|
54 | void VBoxUtf16Free(PRUnichar *pwszString)
|
---|
55 | {
|
---|
56 | RTUtf16Free(pwszString);
|
---|
57 | }
|
---|
58 |
|
---|
59 | void VBoxStrFree(char *pszString)
|
---|
60 | {
|
---|
61 | RTStrFree(pszString);
|
---|
62 | }
|
---|
63 |
|
---|
64 | const PRUnichar* VBoxConvertUTF8toPRUnichar(char *src)
|
---|
65 | {
|
---|
66 | return NS_ConvertUTF8toUTF16(src).get();
|
---|
67 | }
|
---|
68 |
|
---|
69 | const char* VBoxConvertPRUnichartoUTF8(PRUnichar *src)
|
---|
70 | {
|
---|
71 | return NS_ConvertUTF16toUTF8(src).get();
|
---|
72 | }
|
---|
73 |
|
---|
74 | const PRUnichar* VBoxConvertAsciitoPRUnichar(char *src)
|
---|
75 | {
|
---|
76 | return NS_ConvertASCIItoUTF16(src).get();
|
---|
77 | }
|
---|
78 |
|
---|
79 | const char* VBoxConvertPRUnichartoAscii(PRUnichar *src)
|
---|
80 | {
|
---|
81 | return NS_LossyConvertUTF16toASCII(src).get();
|
---|
82 | }
|
---|
83 |
|
---|
84 | void VBoxComUnallocStr(PRUnichar *str_dealloc)
|
---|
85 | {
|
---|
86 | if (str_dealloc) {
|
---|
87 | nsMemory::Free(str_dealloc);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | void VBoxComUnallocIID(nsIID *iid)
|
---|
92 | {
|
---|
93 | if (iid) {
|
---|
94 | nsMemory::Free(iid);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | void VBoxComInitialize(IVirtualBox **virtualBox, ISession **session)
|
---|
99 | {
|
---|
100 | nsresult rc;
|
---|
101 |
|
---|
102 | *session = NULL;
|
---|
103 | *virtualBox = NULL;
|
---|
104 |
|
---|
105 | Session = *session;
|
---|
106 | Ivirtualbox = *virtualBox;
|
---|
107 |
|
---|
108 | // All numbers on stderr in hex prefixed with 0X.
|
---|
109 | cerr.setf(ios_base::showbase | ios_base::uppercase);
|
---|
110 | cerr.setf(ios_base::hex, ios_base::basefield);
|
---|
111 |
|
---|
112 | rc = NS_InitXPCOM2(&serviceManager, nsnull, nsnull);
|
---|
113 | if (NS_FAILED(rc))
|
---|
114 | {
|
---|
115 | cerr << "XPCOM could not be initialized! rc=" << rc << endl;
|
---|
116 | VBoxComUninitialize();
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | rc = NS_GetComponentManager (&manager);
|
---|
121 | if (NS_FAILED(rc))
|
---|
122 | {
|
---|
123 | cerr << "could not get component manager! rc=" << rc << endl;
|
---|
124 | VBoxComUninitialize();
|
---|
125 | return;
|
---|
126 | }
|
---|
127 |
|
---|
128 | rc = manager->CreateInstanceByContractID(NS_VIRTUALBOX_CONTRACTID,
|
---|
129 | nsnull,
|
---|
130 | NS_GET_IID(IVirtualBox),
|
---|
131 | (void **)virtualBox);
|
---|
132 | if (NS_FAILED(rc))
|
---|
133 | {
|
---|
134 | cerr << "could not instantiate VirtualBox object! rc=" << rc << endl;
|
---|
135 | VBoxComUninitialize();
|
---|
136 | return;
|
---|
137 | }
|
---|
138 |
|
---|
139 | cout << "VirtualBox object created." << endl;
|
---|
140 |
|
---|
141 | rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID,
|
---|
142 | nsnull,
|
---|
143 | NS_GET_IID(ISession),
|
---|
144 | (void **)session);
|
---|
145 | if (NS_FAILED(rc))
|
---|
146 | {
|
---|
147 | cerr << "could not instantiate Session object! rc=" << rc << endl;
|
---|
148 | VBoxComUninitialize();
|
---|
149 | return;
|
---|
150 | }
|
---|
151 |
|
---|
152 | cout << "ISession object created." << endl;
|
---|
153 | }
|
---|
154 |
|
---|
155 | void VBoxComUninitialize(void)
|
---|
156 | {
|
---|
157 | if (Session)
|
---|
158 | NS_RELEASE(Session); // decrement refcount
|
---|
159 | if (Ivirtualbox)
|
---|
160 | NS_RELEASE(Ivirtualbox); // decrement refcount
|
---|
161 | if (manager)
|
---|
162 | NS_RELEASE(manager); // decrement refcount
|
---|
163 | if (serviceManager)
|
---|
164 | NS_RELEASE(serviceManager); // decrement refcount
|
---|
165 | NS_ShutdownXPCOM(nsnull);
|
---|
166 | cout << "Done!" << endl;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /* vim: set ts=4 sw=4 et: */
|
---|