VirtualBox

source: vbox/trunk/src/VBox/Main/glue/com.cpp@ 32928

Last change on this file since 32928 was 30632, checked in by vboxsync, 14 years ago

Main: simplify and document ComPtr<> template; speeds up compiling a bit

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/* $Id: com.cpp 30632 2010-07-05 19:36:40Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
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
18#if !defined (VBOX_WITH_XPCOM)
19
20# include <objbase.h>
21
22#else /* !defined (VBOX_WITH_XPCOM) */
23# include <stdlib.h>
24# include <nsCOMPtr.h>
25# include <nsIServiceManagerUtils.h>
26# include <nsIComponentManager.h>
27# include <ipcIService.h>
28# include <ipcCID.h>
29# include <ipcIDConnectService.h>
30# include <nsIInterfaceInfo.h>
31# include <nsIInterfaceInfoManager.h>
32// official XPCOM headers don't define it yet
33#define IPC_DCONNECTSERVICE_CONTRACTID \
34 "@mozilla.org/ipc/dconnect-service;1"
35#endif /* !defined (VBOX_WITH_XPCOM) */
36
37#include "VBox/com/com.h"
38#include "VBox/com/assert.h"
39
40#include "VBox/com/Guid.h"
41#include "VBox/com/array.h"
42
43#include <iprt/param.h>
44#include <iprt/path.h>
45#include <iprt/dir.h>
46#include <iprt/env.h>
47#include <iprt/string.h>
48
49#include <VBox/err.h>
50
51#ifdef RT_OS_DARWIN
52# define VBOX_USER_HOME_SUFFIX "Library/VirtualBox"
53#else
54# define VBOX_USER_HOME_SUFFIX ".VirtualBox"
55#endif
56
57#include "Logging.h"
58
59namespace com
60{
61
62void GetInterfaceNameByIID(const GUID &aIID, BSTR *aName)
63{
64 Assert(aName);
65 if (!aName)
66 return;
67
68 *aName = NULL;
69
70#if !defined(VBOX_WITH_XPCOM)
71
72 LONG rc;
73 LPOLESTR iidStr = NULL;
74 if (StringFromIID(aIID, &iidStr) == S_OK)
75 {
76 HKEY ifaceKey;
77 rc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"Interface",
78 0, KEY_QUERY_VALUE, &ifaceKey);
79 if (rc == ERROR_SUCCESS)
80 {
81 HKEY iidKey;
82 rc = RegOpenKeyExW(ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
83 if (rc == ERROR_SUCCESS)
84 {
85 /* determine the size and type */
86 DWORD sz, type;
87 rc = RegQueryValueExW(iidKey, NULL, NULL, &type, NULL, &sz);
88 if (rc == ERROR_SUCCESS && type == REG_SZ)
89 {
90 /* query the value to BSTR */
91 *aName = SysAllocStringLen(NULL, (sz + 1) / sizeof(TCHAR) + 1);
92 rc = RegQueryValueExW(iidKey, NULL, NULL, NULL, (LPBYTE) *aName, &sz);
93 if (rc != ERROR_SUCCESS)
94 {
95 SysFreeString(*aName);
96 aName = NULL;
97 }
98 }
99 RegCloseKey(iidKey);
100 }
101 RegCloseKey(ifaceKey);
102 }
103 CoTaskMemFree(iidStr);
104 }
105
106#else /* !defined (VBOX_WITH_XPCOM) */
107
108 nsresult rv;
109 nsCOMPtr<nsIInterfaceInfoManager> iim =
110 do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
111 if (NS_SUCCEEDED(rv))
112 {
113 nsCOMPtr<nsIInterfaceInfo> iinfo;
114 rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
115 if (NS_SUCCEEDED(rv))
116 {
117 const char *iname = NULL;
118 iinfo->GetNameShared(&iname);
119 char *utf8IName = NULL;
120 if (RT_SUCCESS(RTStrCurrentCPToUtf8(&utf8IName, iname)))
121 {
122 PRTUTF16 utf16IName = NULL;
123 if (RT_SUCCESS(RTStrToUtf16(utf8IName, &utf16IName)))
124 {
125 *aName = SysAllocString((OLECHAR *) utf16IName);
126 RTUtf16Free(utf16IName);
127 }
128 RTStrFree(utf8IName);
129 }
130 }
131 }
132
133#endif /* !defined (VBOX_WITH_XPCOM) */
134}
135
136#ifdef VBOX_WITH_XPCOM
137
138HRESULT GlueCreateObjectOnServer(const CLSID &clsid,
139 const char *serverName,
140 const nsIID &id,
141 void** ppobj)
142{
143 HRESULT rc;
144 nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);
145 if (SUCCEEDED(rc))
146 {
147 PRUint32 serverID = 0;
148 rc = ipcServ->ResolveClientName(serverName, &serverID);
149 if (SUCCEEDED (rc))
150 {
151 nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc);
152 if (SUCCEEDED(rc))
153 rc = dconServ->CreateInstance(serverID,
154 clsid,
155 id,
156 ppobj);
157 }
158 }
159 return rc;
160}
161
162HRESULT GlueCreateInstance(const CLSID &clsid,
163 const nsIID &id,
164 void** ppobj)
165{
166 nsCOMPtr<nsIComponentManager> manager;
167 HRESULT rc = NS_GetComponentManager(getter_AddRefs(manager));
168 if (SUCCEEDED(rc))
169 rc = manager->CreateInstance(clsid,
170 nsnull,
171 id,
172 ppobj);
173 return rc;
174}
175
176#endif // VBOX_WITH_XPCOM
177
178int GetVBoxUserHomeDirectory(char *aDir, size_t aDirLen)
179{
180 AssertReturn(aDir, VERR_INVALID_POINTER);
181 AssertReturn(aDirLen > 0, VERR_BUFFER_OVERFLOW);
182
183 /* start with null */
184 *aDir = 0;
185
186 char szTmp[RTPATH_MAX];
187 int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_HOME", szTmp, sizeof(szTmp), NULL);
188 if (RT_SUCCESS(vrc) || vrc == VERR_ENV_VAR_NOT_FOUND)
189 {
190 if (RT_SUCCESS(vrc))
191 {
192 /* get the full path name */
193 vrc = RTPathAbs(szTmp, aDir, aDirLen);
194 }
195 else
196 {
197 /* compose the config directory (full path) */
198 /** @todo r=bird: RTPathUserHome doesn't necessarily return a full (abs) path
199 * like the comment above seems to indicate. */
200 vrc = RTPathUserHome(aDir, aDirLen);
201 if (RT_SUCCESS(vrc))
202 vrc = RTPathAppend(aDir, aDirLen, VBOX_USER_HOME_SUFFIX);
203 }
204
205 /* ensure the home directory exists */
206 if (RT_SUCCESS(vrc))
207 if (!RTDirExists(aDir))
208 vrc = RTDirCreateFullPath(aDir, 0777);
209 }
210
211 return vrc;
212}
213
214/* static */
215const Guid Guid::Empty; /* default ctor is OK */
216
217#if defined (VBOX_WITH_XPCOM)
218
219/* static */
220const nsID *SafeGUIDArray::nsIDRef::Empty = (const nsID *)Guid::Empty.raw();
221
222#endif /* (VBOX_WITH_XPCOM) */
223
224/**
225 * Used by ComPtr and friends to log details about reference counting.
226 * @param pcszFormat
227 */
228void LogRef(const char *pcszFormat, ...)
229{
230 char *pszNewMsg;
231 va_list args;
232 va_start(args, pcszFormat);
233 RTStrAPrintfV(&pszNewMsg, pcszFormat, args);
234 LogDJ((pszNewMsg));
235 RTStrFree(pszNewMsg);
236 va_end(args);
237}
238
239} /* namespace com */
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