VirtualBox

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

Last change on this file since 30257 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/* $Id: com.cpp 28800 2010-04-27 08:22:32Z 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
24# include <stdlib.h>
25
26# include <nsCOMPtr.h>
27# include <nsIServiceManagerUtils.h>
28
29# include <nsIInterfaceInfo.h>
30# include <nsIInterfaceInfoManager.h>
31
32#endif /* !defined (VBOX_WITH_XPCOM) */
33
34#include "VBox/com/com.h"
35#include "VBox/com/assert.h"
36
37#include "VBox/com/Guid.h"
38#include "VBox/com/array.h"
39
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/dir.h>
43#include <iprt/env.h>
44#include <iprt/string.h>
45
46#include <VBox/err.h>
47
48#ifdef RT_OS_DARWIN
49# define VBOX_USER_HOME_SUFFIX "Library/VirtualBox"
50#else
51# define VBOX_USER_HOME_SUFFIX ".VirtualBox"
52#endif
53
54#include "Logging.h"
55
56namespace com
57{
58
59void GetInterfaceNameByIID(const GUID &aIID, BSTR *aName)
60{
61 Assert(aName);
62 if (!aName)
63 return;
64
65 *aName = NULL;
66
67#if !defined(VBOX_WITH_XPCOM)
68
69 LONG rc;
70 LPOLESTR iidStr = NULL;
71 if (StringFromIID(aIID, &iidStr) == S_OK)
72 {
73 HKEY ifaceKey;
74 rc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"Interface",
75 0, KEY_QUERY_VALUE, &ifaceKey);
76 if (rc == ERROR_SUCCESS)
77 {
78 HKEY iidKey;
79 rc = RegOpenKeyExW(ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
80 if (rc == ERROR_SUCCESS)
81 {
82 /* determine the size and type */
83 DWORD sz, type;
84 rc = RegQueryValueExW(iidKey, NULL, NULL, &type, NULL, &sz);
85 if (rc == ERROR_SUCCESS && type == REG_SZ)
86 {
87 /* query the value to BSTR */
88 *aName = SysAllocStringLen(NULL, (sz + 1) / sizeof(TCHAR) + 1);
89 rc = RegQueryValueExW(iidKey, NULL, NULL, NULL, (LPBYTE) *aName, &sz);
90 if (rc != ERROR_SUCCESS)
91 {
92 SysFreeString(*aName);
93 aName = NULL;
94 }
95 }
96 RegCloseKey(iidKey);
97 }
98 RegCloseKey(ifaceKey);
99 }
100 CoTaskMemFree(iidStr);
101 }
102
103#else /* !defined (VBOX_WITH_XPCOM) */
104
105 nsresult rv;
106 nsCOMPtr<nsIInterfaceInfoManager> iim =
107 do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
108 if (NS_SUCCEEDED(rv))
109 {
110 nsCOMPtr<nsIInterfaceInfo> iinfo;
111 rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
112 if (NS_SUCCEEDED(rv))
113 {
114 const char *iname = NULL;
115 iinfo->GetNameShared(&iname);
116 char *utf8IName = NULL;
117 if (RT_SUCCESS(RTStrCurrentCPToUtf8(&utf8IName, iname)))
118 {
119 PRTUTF16 utf16IName = NULL;
120 if (RT_SUCCESS(RTStrToUtf16(utf8IName, &utf16IName)))
121 {
122 *aName = SysAllocString((OLECHAR *) utf16IName);
123 RTUtf16Free(utf16IName);
124 }
125 RTStrFree(utf8IName);
126 }
127 }
128 }
129
130#endif /* !defined (VBOX_WITH_XPCOM) */
131}
132
133int GetVBoxUserHomeDirectory(char *aDir, size_t aDirLen)
134{
135 AssertReturn(aDir, VERR_INVALID_POINTER);
136 AssertReturn(aDirLen > 0, VERR_BUFFER_OVERFLOW);
137
138 /* start with null */
139 *aDir = 0;
140
141 char szTmp[RTPATH_MAX];
142 int vrc = RTEnvGetEx(RTENV_DEFAULT, "VBOX_USER_HOME", szTmp, sizeof(szTmp), NULL);
143 if (RT_SUCCESS(vrc) || vrc == VERR_ENV_VAR_NOT_FOUND)
144 {
145 if (RT_SUCCESS(vrc))
146 {
147 /* get the full path name */
148 vrc = RTPathAbs(szTmp, aDir, aDirLen);
149 }
150 else
151 {
152 /* compose the config directory (full path) */
153 /** @todo r=bird: RTPathUserHome doesn't necessarily return a full (abs) path
154 * like the comment above seems to indicate. */
155 vrc = RTPathUserHome(aDir, aDirLen);
156 if (RT_SUCCESS(vrc))
157 vrc = RTPathAppend(aDir, aDirLen, VBOX_USER_HOME_SUFFIX);
158 }
159
160 /* ensure the home directory exists */
161 if (RT_SUCCESS(vrc))
162 if (!RTDirExists(aDir))
163 vrc = RTDirCreateFullPath(aDir, 0777);
164 }
165
166 return vrc;
167}
168
169/* static */
170const Guid Guid::Empty; /* default ctor is OK */
171
172#if defined (VBOX_WITH_XPCOM)
173
174/* static */
175const nsID *SafeGUIDArray::nsIDRef::Empty = (const nsID *)Guid::Empty.raw();
176
177#endif /* (VBOX_WITH_XPCOM) */
178
179/**
180 * Used by ComPtr and friends to log details about reference counting.
181 * @param pcszFormat
182 */
183void LogRef(const char *pcszFormat, ...)
184{
185 char *pszNewMsg;
186 va_list args;
187 va_start(args, pcszFormat);
188 RTStrAPrintfV(&pszNewMsg, pcszFormat, args);
189 LogDJ((pszNewMsg));
190 RTStrFree(pszNewMsg);
191 va_end(args);
192}
193
194} /* 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