VirtualBox

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

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

Main: Removed semicolons after the namespace closing curly braces (not required by the standard).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 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
17#if !defined (VBOX_WITH_XPCOM)
18
19#include <objbase.h>
20
21#else /* !defined (VBOX_WITH_XPCOM) */
22
23#include <stdlib.h>
24
25#include <nsCOMPtr.h>
26#include <nsIServiceManagerUtils.h>
27
28#include <nsIInterfaceInfo.h>
29#include <nsIInterfaceInfoManager.h>
30
31#endif /* !defined (VBOX_WITH_XPCOM) */
32
33#include <iprt/param.h>
34#include <iprt/path.h>
35#include <iprt/dir.h>
36#include <iprt/env.h>
37#include <iprt/string.h>
38
39#include <VBox/err.h>
40
41#include "VBox/com/com.h"
42#include "VBox/com/assert.h"
43
44
45#ifdef RT_OS_DARWIN
46#define VBOX_USER_HOME_SUFFIX "Library/VirtualBox"
47#else
48#define VBOX_USER_HOME_SUFFIX ".VirtualBox"
49#endif
50
51
52namespace com
53{
54
55void GetInterfaceNameByIID (const GUID &aIID, BSTR *aName)
56{
57 Assert (aName);
58 if (!aName)
59 return;
60
61 *aName = NULL;
62
63#if !defined (VBOX_WITH_XPCOM)
64
65 LONG rc;
66 LPOLESTR iidStr = NULL;
67 if (StringFromIID (aIID, &iidStr) == S_OK)
68 {
69 HKEY ifaceKey;
70 rc = RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Interface",
71 0, KEY_QUERY_VALUE, &ifaceKey);
72 if (rc == ERROR_SUCCESS)
73 {
74 HKEY iidKey;
75 rc = RegOpenKeyExW (ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
76 if (rc == ERROR_SUCCESS)
77 {
78 /* determine the size and type */
79 DWORD sz, type;
80 rc = RegQueryValueExW (iidKey, NULL, NULL, &type, NULL, &sz);
81 if (rc == ERROR_SUCCESS && type == REG_SZ)
82 {
83 /* query the value to BSTR */
84 *aName = SysAllocStringLen (NULL, (sz + 1) /
85 sizeof (TCHAR) + 1);
86 rc = RegQueryValueExW (iidKey, NULL, NULL, NULL,
87 (LPBYTE) *aName, &sz);
88 if (rc != ERROR_SUCCESS)
89 {
90 SysFreeString (*aName);
91 aName = NULL;
92 }
93 }
94 RegCloseKey (iidKey);
95 }
96 RegCloseKey (ifaceKey);
97 }
98 CoTaskMemFree (iidStr);
99 }
100
101#else /* !defined (VBOX_WITH_XPCOM) */
102
103 nsresult rv;
104 nsCOMPtr <nsIInterfaceInfoManager> iim =
105 do_GetService (NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
106 if (NS_SUCCEEDED (rv))
107 {
108 nsCOMPtr <nsIInterfaceInfo> iinfo;
109 rv = iim->GetInfoForIID (&aIID, getter_AddRefs (iinfo));
110 if (NS_SUCCEEDED (rv))
111 {
112 const char *iname = NULL;
113 iinfo->GetNameShared (&iname);
114 char *utf8IName = NULL;
115 if (VBOX_SUCCESS (RTStrCurrentCPToUtf8 (&utf8IName, iname)))
116 {
117 PRTUCS2 ucs2IName = NULL;
118 if (VBOX_SUCCESS (RTStrUtf8ToUcs2 (&ucs2IName, utf8IName)))
119 {
120 *aName = SysAllocString ((OLECHAR *) ucs2IName);
121 RTStrUcs2Free (ucs2IName);
122 }
123 RTStrFree (utf8IName);
124 }
125 }
126 }
127
128#endif /* !defined (VBOX_WITH_XPCOM) */
129}
130
131int GetVBoxUserHomeDirectory (char *aDir, size_t aDirLen)
132{
133 AssertReturn (aDir, VERR_INVALID_POINTER);
134 AssertReturn (aDirLen > 0, VERR_BUFFER_OVERFLOW);
135
136 /* start with null */
137 *aDir = 0;
138
139 const char *VBoxUserHome = RTEnvGet ("VBOX_USER_HOME");
140
141 char path [RTPATH_MAX];
142 int vrc = VINF_SUCCESS;
143
144 if (VBoxUserHome)
145 {
146 /* get the full path name */
147 char *VBoxUserHomeUtf8 = NULL;
148 vrc = RTStrCurrentCPToUtf8 (&VBoxUserHomeUtf8, VBoxUserHome);
149 if (RT_SUCCESS (vrc))
150 {
151 vrc = RTPathAbs (VBoxUserHomeUtf8, path, sizeof (path));
152 if (RT_SUCCESS (vrc))
153 {
154 if (aDirLen < strlen (path) + 1)
155 vrc = VERR_BUFFER_OVERFLOW;
156 else
157 strcpy (aDir, path);
158 }
159 RTStrFree (VBoxUserHomeUtf8);
160 }
161 }
162 else
163 {
164 /* compose the config directory (full path) */
165 vrc = RTPathUserHome (path, sizeof (path));
166 if (RT_SUCCESS (vrc))
167 {
168 size_t len =
169 RTStrPrintf (aDir, aDirLen, "%s%c%s",
170 path, RTPATH_DELIMITER, VBOX_USER_HOME_SUFFIX);
171 if (len != strlen (path) + 1 + strlen (VBOX_USER_HOME_SUFFIX))
172 vrc = VERR_BUFFER_OVERFLOW;
173 }
174 }
175
176 /* ensure the home directory exists */
177 if (RT_SUCCESS (vrc))
178 if (!RTDirExists (aDir))
179 vrc = RTDirCreateFullPath (aDir, 0777);
180
181 return vrc;
182}
183
184} /* 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