1 | /* $Id: com.cpp 22700 2009-09-02 10:05:23Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | * MS COM / XPCOM Abstraction Layer
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #if !defined (VBOX_WITH_XPCOM)
|
---|
24 |
|
---|
25 | #include <objbase.h>
|
---|
26 |
|
---|
27 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
28 |
|
---|
29 | #include <stdlib.h>
|
---|
30 |
|
---|
31 | #include <nsCOMPtr.h>
|
---|
32 | #include <nsIServiceManagerUtils.h>
|
---|
33 |
|
---|
34 | #include <nsIInterfaceInfo.h>
|
---|
35 | #include <nsIInterfaceInfoManager.h>
|
---|
36 |
|
---|
37 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
38 |
|
---|
39 | #include "VBox/com/com.h"
|
---|
40 | #include "VBox/com/assert.h"
|
---|
41 |
|
---|
42 | #include "VBox/com/Guid.h"
|
---|
43 | #include "VBox/com/array.h"
|
---|
44 |
|
---|
45 | #include <iprt/param.h>
|
---|
46 | #include <iprt/path.h>
|
---|
47 | #include <iprt/dir.h>
|
---|
48 | #include <iprt/env.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 | #include <iprt/log.h>
|
---|
51 |
|
---|
52 | #include <VBox/err.h>
|
---|
53 |
|
---|
54 |
|
---|
55 | #ifdef RT_OS_DARWIN
|
---|
56 | #define VBOX_USER_HOME_SUFFIX "Library/VirtualBox"
|
---|
57 | #else
|
---|
58 | #define VBOX_USER_HOME_SUFFIX ".VirtualBox"
|
---|
59 | #endif
|
---|
60 |
|
---|
61 |
|
---|
62 | namespace com
|
---|
63 | {
|
---|
64 |
|
---|
65 | void GetInterfaceNameByIID (const GUID &aIID, BSTR *aName)
|
---|
66 | {
|
---|
67 | Assert (aName);
|
---|
68 | if (!aName)
|
---|
69 | return;
|
---|
70 |
|
---|
71 | *aName = NULL;
|
---|
72 |
|
---|
73 | #if !defined (VBOX_WITH_XPCOM)
|
---|
74 |
|
---|
75 | LONG rc;
|
---|
76 | LPOLESTR iidStr = NULL;
|
---|
77 | if (StringFromIID (aIID, &iidStr) == S_OK)
|
---|
78 | {
|
---|
79 | HKEY ifaceKey;
|
---|
80 | rc = RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Interface",
|
---|
81 | 0, KEY_QUERY_VALUE, &ifaceKey);
|
---|
82 | if (rc == ERROR_SUCCESS)
|
---|
83 | {
|
---|
84 | HKEY iidKey;
|
---|
85 | rc = RegOpenKeyExW (ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
|
---|
86 | if (rc == ERROR_SUCCESS)
|
---|
87 | {
|
---|
88 | /* determine the size and type */
|
---|
89 | DWORD sz, type;
|
---|
90 | rc = RegQueryValueExW (iidKey, NULL, NULL, &type, NULL, &sz);
|
---|
91 | if (rc == ERROR_SUCCESS && type == REG_SZ)
|
---|
92 | {
|
---|
93 | /* query the value to BSTR */
|
---|
94 | *aName = SysAllocStringLen (NULL, (sz + 1) /
|
---|
95 | sizeof (TCHAR) + 1);
|
---|
96 | rc = RegQueryValueExW (iidKey, NULL, NULL, NULL,
|
---|
97 | (LPBYTE) *aName, &sz);
|
---|
98 | if (rc != ERROR_SUCCESS)
|
---|
99 | {
|
---|
100 | SysFreeString (*aName);
|
---|
101 | aName = NULL;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | RegCloseKey (iidKey);
|
---|
105 | }
|
---|
106 | RegCloseKey (ifaceKey);
|
---|
107 | }
|
---|
108 | CoTaskMemFree (iidStr);
|
---|
109 | }
|
---|
110 |
|
---|
111 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
112 |
|
---|
113 | nsresult rv;
|
---|
114 | nsCOMPtr <nsIInterfaceInfoManager> iim =
|
---|
115 | do_GetService (NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
|
---|
116 | if (NS_SUCCEEDED(rv))
|
---|
117 | {
|
---|
118 | nsCOMPtr <nsIInterfaceInfo> iinfo;
|
---|
119 | rv = iim->GetInfoForIID (&aIID, getter_AddRefs (iinfo));
|
---|
120 | if (NS_SUCCEEDED(rv))
|
---|
121 | {
|
---|
122 | const char *iname = NULL;
|
---|
123 | iinfo->GetNameShared (&iname);
|
---|
124 | char *utf8IName = NULL;
|
---|
125 | if (RT_SUCCESS(RTStrCurrentCPToUtf8 (&utf8IName, iname)))
|
---|
126 | {
|
---|
127 | PRTUTF16 utf16IName = NULL;
|
---|
128 | if (RT_SUCCESS(RTStrToUtf16 (utf8IName, &utf16IName)))
|
---|
129 | {
|
---|
130 | *aName = SysAllocString ((OLECHAR *) utf16IName);
|
---|
131 | RTUtf16Free (utf16IName);
|
---|
132 | }
|
---|
133 | RTStrFree (utf8IName);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
139 | }
|
---|
140 |
|
---|
141 | int GetVBoxUserHomeDirectory (char *aDir, size_t aDirLen)
|
---|
142 | {
|
---|
143 | AssertReturn(aDir, VERR_INVALID_POINTER);
|
---|
144 | AssertReturn(aDirLen > 0, VERR_BUFFER_OVERFLOW);
|
---|
145 |
|
---|
146 | /* start with null */
|
---|
147 | *aDir = 0;
|
---|
148 |
|
---|
149 | const char *VBoxUserHome = RTEnvGet ("VBOX_USER_HOME");
|
---|
150 |
|
---|
151 | char path [RTPATH_MAX];
|
---|
152 | int vrc = VINF_SUCCESS;
|
---|
153 |
|
---|
154 | if (VBoxUserHome)
|
---|
155 | {
|
---|
156 | /* get the full path name */
|
---|
157 | char *VBoxUserHomeUtf8 = NULL;
|
---|
158 | vrc = RTStrCurrentCPToUtf8 (&VBoxUserHomeUtf8, VBoxUserHome);
|
---|
159 | if (RT_SUCCESS(vrc))
|
---|
160 | {
|
---|
161 | vrc = RTPathAbs (VBoxUserHomeUtf8, path, sizeof (path));
|
---|
162 | if (RT_SUCCESS(vrc))
|
---|
163 | {
|
---|
164 | if (aDirLen < strlen (path) + 1)
|
---|
165 | vrc = VERR_BUFFER_OVERFLOW;
|
---|
166 | else
|
---|
167 | strcpy (aDir, path);
|
---|
168 | }
|
---|
169 | RTStrFree (VBoxUserHomeUtf8);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | else
|
---|
173 | {
|
---|
174 | /* compose the config directory (full path) */
|
---|
175 | vrc = RTPathUserHome (path, sizeof (path));
|
---|
176 | if (RT_SUCCESS(vrc))
|
---|
177 | {
|
---|
178 | size_t len =
|
---|
179 | RTStrPrintf (aDir, aDirLen, "%s%c%s",
|
---|
180 | path, RTPATH_DELIMITER, VBOX_USER_HOME_SUFFIX);
|
---|
181 | if (len != strlen (path) + 1 + strlen (VBOX_USER_HOME_SUFFIX))
|
---|
182 | vrc = VERR_BUFFER_OVERFLOW;
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* ensure the home directory exists */
|
---|
187 | if (RT_SUCCESS(vrc))
|
---|
188 | if (!RTDirExists (aDir))
|
---|
189 | vrc = RTDirCreateFullPath (aDir, 0777);
|
---|
190 |
|
---|
191 | return vrc;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /* static */
|
---|
195 | const Guid Guid::Empty; /* default ctor is OK */
|
---|
196 |
|
---|
197 | #if defined (VBOX_WITH_XPCOM)
|
---|
198 |
|
---|
199 | /* static */
|
---|
200 | const nsID *SafeGUIDArray::nsIDRef::Empty = (const nsID *) Guid::Empty.raw();
|
---|
201 |
|
---|
202 | #endif /* (VBOX_WITH_XPCOM) */
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * Used by ComPtr and friends to log details about reference counting.
|
---|
206 | * @param pcszFormat
|
---|
207 | */
|
---|
208 | void LogRef(const char *pcszFormat, ...)
|
---|
209 | {
|
---|
210 | va_list va;
|
---|
211 | va_start(va, pcszFormat);
|
---|
212 | va_end(va);
|
---|
213 |
|
---|
214 | char *psz = NULL;
|
---|
215 | RTStrAPrintfV(&psz, pcszFormat, va);
|
---|
216 | LogDJ((psz));
|
---|
217 | RTStrFree(psz);
|
---|
218 | }
|
---|
219 |
|
---|
220 | } /* namespace com */
|
---|