VirtualBox

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

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

RTUCS2 -> RTUTF16

  • 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 7430 2008-03-12 10:16:03Z vboxsync $ */
2
3/** @file
4 * MS COM / XPCOM Abstraction Layer
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek GmbH
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
19#if !defined (VBOX_WITH_XPCOM)
20
21#include <objbase.h>
22
23#else /* !defined (VBOX_WITH_XPCOM) */
24
25#include <stdlib.h>
26
27#include <nsCOMPtr.h>
28#include <nsIServiceManagerUtils.h>
29
30#include <nsIInterfaceInfo.h>
31#include <nsIInterfaceInfoManager.h>
32
33#endif /* !defined (VBOX_WITH_XPCOM) */
34
35#include <iprt/param.h>
36#include <iprt/path.h>
37#include <iprt/dir.h>
38#include <iprt/env.h>
39#include <iprt/string.h>
40
41#include <VBox/err.h>
42
43#include "VBox/com/com.h"
44#include "VBox/com/assert.h"
45
46#include <VBox/com/Guid.h>
47
48
49#ifdef RT_OS_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 (RT_SUCCESS (RTStrCurrentCPToUtf8 (&utf8IName, iname)))
120 {
121 PRTUTF16 utf16IName = NULL;
122 if (RT_SUCCESS (RTStrToUtf16 (utf8IName, &utf16IName)))
123 {
124 *aName = SysAllocString ((OLECHAR *) utf16IName);
125 RTUtf16Free (utf16IName);
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/* static */
189const Guid Guid::Empty; /* default ctor is OK */
190
191} /* 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