VirtualBox

source: vbox/trunk/src/VBox/Main/cbinding/libXPCOMtoC.cpp@ 16487

Last change on this file since 16487 was 16487, checked in by vboxsync, 16 years ago

cbinding: No $Id$ keywords in samples, just $Revision$. The keyword string goes before the @file. The file start with one sentence outlining the purpose of the file, further explanation follows in a separate paragraph. Files use *one* brace and space-before-paranthesis style, esp. sample files. Do not exit() from main(), return from it. stuff -> _stuff since the double underscore namespace is reserved the system/compiler/libc. Finally, why unconditional logging and why doing it using cout/cerr?

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

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