VirtualBox

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

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

Main: Ported latest dmik/exp branch changes (r21219:21226) into the trunk.

  • 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 *
3 * MS COM / XPCOM Abstraction Layer
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#if !defined (VBOX_WITH_XPCOM)
23
24#include <objbase.h>
25
26#else
27
28#include <stdlib.h>
29#include <VBox/err.h>
30#include <iprt/path.h>
31
32#include <nsXPCOMGlue.h>
33#include <nsIComponentRegistrar.h>
34#include <nsIServiceManager.h>
35#include <nsCOMPtr.h>
36#include <nsEventQueueUtils.h>
37
38#include <nsIInterfaceInfo.h>
39#include <nsIInterfaceInfoManager.h>
40
41#endif
42
43#include <iprt/string.h>
44#include <VBox/err.h>
45
46#include "VBox/com/com.h"
47#include "VBox/com/assert.h"
48
49namespace com
50{
51
52HRESULT Initialize()
53{
54 HRESULT rc = E_FAIL;
55
56#if !defined (VBOX_WITH_XPCOM)
57
58 rc = CoInitializeEx (NULL, COINIT_MULTITHREADED |
59 COINIT_DISABLE_OLE1DDE |
60 COINIT_SPEED_OVER_MEMORY);
61
62#else
63
64 /* Set VBOX_XPCOM_HOME if not present */
65 if (!getenv("VBOX_XPCOM_HOME"))
66 {
67 /* get the executable path */
68 char szPathProgram[1024];
69 int rcVBox = RTPathProgram(szPathProgram, sizeof(szPathProgram));
70 if (VBOX_SUCCESS(rcVBox))
71 {
72 setenv("VBOX_XPCOM_HOME", szPathProgram, 1);
73 }
74 }
75
76 nsCOMPtr <nsIEventQueue> eventQ;
77 rc = NS_GetMainEventQ (getter_AddRefs (eventQ));
78 if (rc == NS_ERROR_NOT_INITIALIZED)
79 {
80 XPCOMGlueStartup (nsnull);
81 nsCOMPtr <nsIServiceManager> serviceManager;
82 rc = NS_InitXPCOM2 (getter_AddRefs (serviceManager), nsnull, nsnull);
83 if (NS_SUCCEEDED (rc))
84 {
85 nsCOMPtr <nsIComponentRegistrar> registrar =
86 do_QueryInterface (serviceManager, &rc);
87 if (NS_SUCCEEDED (rc))
88 registrar->AutoRegister (nsnull);
89 }
90 }
91
92#endif
93
94 AssertComRC (rc);
95
96 return rc;
97}
98
99void Shutdown()
100{
101#if !defined (VBOX_WITH_XPCOM)
102
103 CoUninitialize();
104
105#else
106
107 nsCOMPtr <nsIEventQueue> eventQ;
108 nsresult rc = NS_GetMainEventQ (getter_AddRefs (eventQ));
109 if (NS_SUCCEEDED (rc))
110 {
111 BOOL isOnMainThread = FALSE;
112 eventQ->IsOnCurrentThread (&isOnMainThread);
113 eventQ = nsnull; /* early release */
114 if (isOnMainThread)
115 {
116 /* only the main thread needs to uninitialize XPCOM */
117 NS_ShutdownXPCOM (nsnull);
118 XPCOMGlueShutdown();
119 }
120 }
121
122#endif
123}
124
125void GetInterfaceNameByIID (const GUID &aIID, BSTR *aName)
126{
127 Assert (aName);
128 if (!aName)
129 return;
130
131 *aName = NULL;
132
133#if !defined (VBOX_WITH_XPCOM)
134
135 LONG rc;
136 LPOLESTR iidStr = NULL;
137 if (StringFromIID (aIID, &iidStr) == S_OK)
138 {
139 HKEY ifaceKey;
140 rc = RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Interface",
141 0, KEY_QUERY_VALUE, &ifaceKey);
142 if (rc == ERROR_SUCCESS)
143 {
144 HKEY iidKey;
145 rc = RegOpenKeyExW (ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
146 if (rc == ERROR_SUCCESS)
147 {
148 /* determine the size and type */
149 DWORD sz, type;
150 rc = RegQueryValueExW (iidKey, NULL, NULL, &type, NULL, &sz);
151 if (rc == ERROR_SUCCESS && type == REG_SZ)
152 {
153 /* query the value to BSTR */
154 *aName = SysAllocStringLen (NULL, (sz + 1) /
155 sizeof (TCHAR) + 1);
156 rc = RegQueryValueExW (iidKey, NULL, NULL, NULL,
157 (LPBYTE) *aName, &sz);
158 if (rc != ERROR_SUCCESS)
159 {
160 SysFreeString (*aName);
161 aName = NULL;
162 }
163 }
164 RegCloseKey (iidKey);
165 }
166 RegCloseKey (ifaceKey);
167 }
168 CoTaskMemFree (iidStr);
169 }
170
171#else
172
173 nsresult rv;
174 nsCOMPtr <nsIInterfaceInfoManager> iim =
175 do_GetService (NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
176 if (NS_SUCCEEDED (rv))
177 {
178 nsCOMPtr <nsIInterfaceInfo> iinfo;
179 rv = iim->GetInfoForIID (&aIID, getter_AddRefs (iinfo));
180 if (NS_SUCCEEDED (rv))
181 {
182 const char *iname = NULL;
183 iinfo->GetNameShared (&iname);
184 char *utf8IName = NULL;
185 if (VBOX_SUCCESS (RTStrCurrentCPToUtf8 (&utf8IName, iname)))
186 {
187 PRTUCS2 ucs2IName = NULL;
188 if (VBOX_SUCCESS (RTStrUtf8ToUcs2 (&ucs2IName, utf8IName)))
189 {
190 *aName = SysAllocString ((OLECHAR *) ucs2IName);
191 RTStrUcs2Free (ucs2IName);
192 }
193 RTStrFree (utf8IName);
194 }
195 }
196 }
197
198#endif
199}
200
201}; // 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