1 | /* $Id: com.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2005-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_MAIN
|
---|
33 | #if !defined(VBOX_WITH_XPCOM)
|
---|
34 |
|
---|
35 | # include <iprt/win/objbase.h>
|
---|
36 |
|
---|
37 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
38 | # include <stdlib.h>
|
---|
39 | # include <nsCOMPtr.h>
|
---|
40 | # include <nsIServiceManagerUtils.h>
|
---|
41 | # include <nsIComponentManager.h>
|
---|
42 | # include <ipcIService.h>
|
---|
43 | # include <ipcCID.h>
|
---|
44 | # include <ipcIDConnectService.h>
|
---|
45 | # include <nsIInterfaceInfo.h>
|
---|
46 | # include <nsIInterfaceInfoManager.h>
|
---|
47 | # define IPC_DCONNECTSERVICE_CONTRACTID "@mozilla.org/ipc/dconnect-service;1" // official XPCOM headers don't define it yet
|
---|
48 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
49 |
|
---|
50 | #include "VBox/com/com.h"
|
---|
51 | #include "VBox/com/assert.h"
|
---|
52 |
|
---|
53 | #include "VBox/com/Guid.h"
|
---|
54 | #include "VBox/com/array.h"
|
---|
55 |
|
---|
56 | #include <iprt/string.h>
|
---|
57 |
|
---|
58 | #include <iprt/errcore.h>
|
---|
59 | #include <VBox/log.h>
|
---|
60 |
|
---|
61 |
|
---|
62 | /*********************************************************************************************************************************
|
---|
63 | * Global Variables *
|
---|
64 | *********************************************************************************************************************************/
|
---|
65 | namespace com
|
---|
66 | {
|
---|
67 | /* static */
|
---|
68 | const Guid Guid::Empty; /* default ctor is OK */
|
---|
69 |
|
---|
70 | const char Zeroes[16] = {0, };
|
---|
71 |
|
---|
72 |
|
---|
73 | void GetInterfaceNameByIID(const GUID &aIID, BSTR *aName)
|
---|
74 | {
|
---|
75 | AssertPtrReturnVoid(aName);
|
---|
76 | *aName = NULL;
|
---|
77 |
|
---|
78 | #if !defined(VBOX_WITH_XPCOM)
|
---|
79 |
|
---|
80 | LONG rc;
|
---|
81 | LPOLESTR iidStr = NULL;
|
---|
82 | if (StringFromIID(aIID, &iidStr) == S_OK)
|
---|
83 | {
|
---|
84 | HKEY ifaceKey;
|
---|
85 | rc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"Interface",
|
---|
86 | 0, KEY_QUERY_VALUE, &ifaceKey);
|
---|
87 | if (rc == ERROR_SUCCESS)
|
---|
88 | {
|
---|
89 | HKEY iidKey;
|
---|
90 | rc = RegOpenKeyExW(ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
|
---|
91 | if (rc == ERROR_SUCCESS)
|
---|
92 | {
|
---|
93 | /* determine the size and type */
|
---|
94 | DWORD sz, type;
|
---|
95 | rc = RegQueryValueExW(iidKey, NULL, NULL, &type, NULL, &sz);
|
---|
96 | if (rc == ERROR_SUCCESS && type == REG_SZ)
|
---|
97 | {
|
---|
98 | /* query the value to BSTR */
|
---|
99 | *aName = SysAllocStringLen(NULL, (sz + 1) / sizeof(TCHAR) + 1);
|
---|
100 | rc = RegQueryValueExW(iidKey, NULL, NULL, NULL, (LPBYTE) *aName, &sz);
|
---|
101 | if (rc != ERROR_SUCCESS)
|
---|
102 | {
|
---|
103 | SysFreeString(*aName);
|
---|
104 | *aName = NULL;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | RegCloseKey(iidKey);
|
---|
108 | }
|
---|
109 | RegCloseKey(ifaceKey);
|
---|
110 | }
|
---|
111 | CoTaskMemFree(iidStr);
|
---|
112 | }
|
---|
113 |
|
---|
114 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
115 |
|
---|
116 | nsresult rv;
|
---|
117 | nsCOMPtr<nsIInterfaceInfoManager> iim =
|
---|
118 | do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
|
---|
119 | if (NS_SUCCEEDED(rv))
|
---|
120 | {
|
---|
121 | nsCOMPtr<nsIInterfaceInfo> iinfo;
|
---|
122 | rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
|
---|
123 | if (NS_SUCCEEDED(rv))
|
---|
124 | {
|
---|
125 | const char *iname = NULL;
|
---|
126 | iinfo->GetNameShared(&iname);
|
---|
127 | char *utf8IName = NULL;
|
---|
128 | if (RT_SUCCESS(RTStrCurrentCPToUtf8(&utf8IName, iname)))
|
---|
129 | {
|
---|
130 | PRTUTF16 utf16IName = NULL;
|
---|
131 | if (RT_SUCCESS(RTStrToUtf16(utf8IName, &utf16IName)))
|
---|
132 | {
|
---|
133 | *aName = SysAllocString((OLECHAR *) utf16IName);
|
---|
134 | RTUtf16Free(utf16IName);
|
---|
135 | }
|
---|
136 | RTStrFree(utf8IName);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
142 | }
|
---|
143 |
|
---|
144 | #ifdef VBOX_WITH_XPCOM
|
---|
145 |
|
---|
146 | HRESULT GlueCreateObjectOnServer(const CLSID &clsid,
|
---|
147 | const char *serverName,
|
---|
148 | const nsIID &id,
|
---|
149 | void** ppobj)
|
---|
150 | {
|
---|
151 | HRESULT rc;
|
---|
152 | nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);
|
---|
153 | if (SUCCEEDED(rc))
|
---|
154 | {
|
---|
155 | PRUint32 serverID = 0;
|
---|
156 | rc = ipcServ->ResolveClientName(serverName, &serverID);
|
---|
157 | if (SUCCEEDED (rc))
|
---|
158 | {
|
---|
159 | nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc);
|
---|
160 | if (SUCCEEDED(rc))
|
---|
161 | rc = dconServ->CreateInstance(serverID,
|
---|
162 | clsid,
|
---|
163 | id,
|
---|
164 | ppobj);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | return rc;
|
---|
168 | }
|
---|
169 |
|
---|
170 | HRESULT GlueCreateInstance(const CLSID &clsid,
|
---|
171 | const nsIID &id,
|
---|
172 | void** ppobj)
|
---|
173 | {
|
---|
174 | nsCOMPtr<nsIComponentManager> manager;
|
---|
175 | HRESULT rc = NS_GetComponentManager(getter_AddRefs(manager));
|
---|
176 | if (SUCCEEDED(rc))
|
---|
177 | rc = manager->CreateInstance(clsid,
|
---|
178 | nsnull,
|
---|
179 | id,
|
---|
180 | ppobj);
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 |
|
---|
184 | #endif // VBOX_WITH_XPCOM
|
---|
185 |
|
---|
186 | } /* namespace com */
|
---|
187 |
|
---|