VirtualBox

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

Last change on this file since 3387 was 3387, checked in by vboxsync, 17 years ago

Main: com::GetVBoxUserHomeDirectory() now takes char * instead of Utf8Str because on XPCOM platforms, this function may be called before XPCOM has been initialized, when Utf8Str functionality is not yet available (because it uses XPCOM memory management).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21#if !defined (VBOX_WITH_XPCOM)
22
23#include <objbase.h>
24
25#else /* !defined (VBOX_WITH_XPCOM) */
26
27#include <stdlib.h>
28
29#include <nsCOMPtr.h>
30#include <nsIServiceManagerUtils.h>
31
32#include <nsIInterfaceInfo.h>
33#include <nsIInterfaceInfoManager.h>
34
35#endif /* !defined (VBOX_WITH_XPCOM) */
36
37#include <iprt/param.h>
38#include <iprt/path.h>
39#include <iprt/dir.h>
40#include <iprt/env.h>
41#include <iprt/string.h>
42
43#include <VBox/err.h>
44
45#include "VBox/com/com.h"
46#include "VBox/com/assert.h"
47
48
49#ifdef __DARWIN__
50#define VBOX_USER_HOME_SUFFIX "Library/VirtualBox"
51#else
52#define VBOX_USER_HOME_SUFFIX ".VirtualBox"
53#endif
54
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) /
89 sizeof (TCHAR) + 1);
90 rc = RegQueryValueExW (iidKey, NULL, NULL, NULL,
91 (LPBYTE) *aName, &sz);
92 if (rc != ERROR_SUCCESS)
93 {
94 SysFreeString (*aName);
95 aName = NULL;
96 }
97 }
98 RegCloseKey (iidKey);
99 }
100 RegCloseKey (ifaceKey);
101 }
102 CoTaskMemFree (iidStr);
103 }
104
105#else /* !defined (VBOX_WITH_XPCOM) */
106
107 nsresult rv;
108 nsCOMPtr <nsIInterfaceInfoManager> iim =
109 do_GetService (NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
110 if (NS_SUCCEEDED (rv))
111 {
112 nsCOMPtr <nsIInterfaceInfo> iinfo;
113 rv = iim->GetInfoForIID (&aIID, getter_AddRefs (iinfo));
114 if (NS_SUCCEEDED (rv))
115 {
116 const char *iname = NULL;
117 iinfo->GetNameShared (&iname);
118 char *utf8IName = NULL;
119 if (VBOX_SUCCESS (RTStrCurrentCPToUtf8 (&utf8IName, iname)))
120 {
121 PRTUCS2 ucs2IName = NULL;
122 if (VBOX_SUCCESS (RTStrUtf8ToUcs2 (&ucs2IName, utf8IName)))
123 {
124 *aName = SysAllocString ((OLECHAR *) ucs2IName);
125 RTStrUcs2Free (ucs2IName);
126 }
127 RTStrFree (utf8IName);
128 }
129 }
130 }
131
132#endif /* !defined (VBOX_WITH_XPCOM) */
133}
134
135int GetVBoxUserHomeDirectory (char *aDir, size_t aDirLen)
136{
137 AssertReturn (aDir, VERR_INVALID_POINTER);
138 AssertReturn (aDirLen > 0, VERR_BUFFER_OVERFLOW);
139
140 /* start with null */
141 *aDir = 0;
142
143 const char *VBoxUserHome = RTEnvGet ("VBOX_USER_HOME");
144
145 char path [RTPATH_MAX];
146 int vrc = VINF_SUCCESS;
147
148 if (VBoxUserHome)
149 {
150 /* get the full path name */
151 char *VBoxUserHomeUtf8 = NULL;
152 vrc = RTStrCurrentCPToUtf8 (&VBoxUserHomeUtf8, VBoxUserHome);
153 if (RT_SUCCESS (vrc))
154 {
155 vrc = RTPathAbs (VBoxUserHomeUtf8, path, sizeof (path));
156 if (RT_SUCCESS (vrc))
157 {
158 if (aDirLen < strlen (path) + 1)
159 vrc = VERR_BUFFER_OVERFLOW;
160 else
161 strcpy (aDir, path);
162 }
163 RTStrFree (VBoxUserHomeUtf8);
164 }
165 }
166 else
167 {
168 /* compose the config directory (full path) */
169 vrc = RTPathUserHome (path, sizeof (path));
170 if (RT_SUCCESS (vrc))
171 {
172 size_t len =
173 RTStrPrintf (aDir, aDirLen, "%s%c%s",
174 path, RTPATH_DELIMITER, VBOX_USER_HOME_SUFFIX);
175 if (len != strlen (path) + 1 + strlen (VBOX_USER_HOME_SUFFIX))
176 vrc = VERR_BUFFER_OVERFLOW;
177 }
178 }
179
180 /* ensure the home directory exists */
181 if (RT_SUCCESS (vrc))
182 if (!RTDirExists (aDir))
183 vrc = RTDirCreateFullPath (aDir, 0777);
184
185 return vrc;
186}
187
188}; // 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