1 | /* $Id: com.cpp 98297 2023-01-25 01:59:25Z 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 | LPOLESTR iidStr = NULL;
|
---|
81 | if (StringFromIID(aIID, &iidStr) == S_OK)
|
---|
82 | {
|
---|
83 | HKEY ifaceKey;
|
---|
84 | LSTATUS lrc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"Interface", 0, KEY_QUERY_VALUE, &ifaceKey);
|
---|
85 | if (lrc == ERROR_SUCCESS)
|
---|
86 | {
|
---|
87 | HKEY iidKey;
|
---|
88 | lrc = RegOpenKeyExW(ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
|
---|
89 | if (lrc == ERROR_SUCCESS)
|
---|
90 | {
|
---|
91 | /* determine the size and type */
|
---|
92 | DWORD sz, type;
|
---|
93 | lrc = RegQueryValueExW(iidKey, NULL, NULL, &type, NULL, &sz);
|
---|
94 | if (lrc == ERROR_SUCCESS && type == REG_SZ)
|
---|
95 | {
|
---|
96 | /* query the value to BSTR */
|
---|
97 | *aName = SysAllocStringLen(NULL, (sz + 1) / sizeof(TCHAR) + 1);
|
---|
98 | lrc = RegQueryValueExW(iidKey, NULL, NULL, NULL, (LPBYTE) *aName, &sz);
|
---|
99 | if (lrc != ERROR_SUCCESS)
|
---|
100 | {
|
---|
101 | SysFreeString(*aName);
|
---|
102 | *aName = NULL;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | RegCloseKey(iidKey);
|
---|
106 | }
|
---|
107 | RegCloseKey(ifaceKey);
|
---|
108 | }
|
---|
109 | CoTaskMemFree(iidStr);
|
---|
110 | }
|
---|
111 |
|
---|
112 | #else /* !defined (VBOX_WITH_XPCOM) */
|
---|
113 |
|
---|
114 | nsresult rv;
|
---|
115 | nsCOMPtr<nsIInterfaceInfoManager> iim =
|
---|
116 | do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
|
---|
117 | if (NS_SUCCEEDED(rv))
|
---|
118 | {
|
---|
119 | nsCOMPtr<nsIInterfaceInfo> iinfo;
|
---|
120 | rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
|
---|
121 | if (NS_SUCCEEDED(rv))
|
---|
122 | {
|
---|
123 | const char *iname = NULL;
|
---|
124 | iinfo->GetNameShared(&iname);
|
---|
125 | char *utf8IName = NULL;
|
---|
126 | if (RT_SUCCESS(RTStrCurrentCPToUtf8(&utf8IName, iname)))
|
---|
127 | {
|
---|
128 | PRTUTF16 utf16IName = NULL;
|
---|
129 | if (RT_SUCCESS(RTStrToUtf16(utf8IName, &utf16IName)))
|
---|
130 | {
|
---|
131 | *aName = SysAllocString((OLECHAR *) utf16IName);
|
---|
132 | RTUtf16Free(utf16IName);
|
---|
133 | }
|
---|
134 | RTStrFree(utf8IName);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | #endif /* !defined (VBOX_WITH_XPCOM) */
|
---|
140 | }
|
---|
141 |
|
---|
142 | #ifdef VBOX_WITH_XPCOM
|
---|
143 |
|
---|
144 | HRESULT GlueCreateObjectOnServer(const CLSID &clsid,
|
---|
145 | const char *serverName,
|
---|
146 | const nsIID &id,
|
---|
147 | void** ppobj)
|
---|
148 | {
|
---|
149 | HRESULT hrc = E_UNEXPECTED;
|
---|
150 | nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &hrc);
|
---|
151 | if (SUCCEEDED(hrc))
|
---|
152 | {
|
---|
153 | PRUint32 serverID = 0;
|
---|
154 | hrc = ipcServ->ResolveClientName(serverName, &serverID);
|
---|
155 | if (SUCCEEDED (hrc))
|
---|
156 | {
|
---|
157 | nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &hrc);
|
---|
158 | if (SUCCEEDED(hrc))
|
---|
159 | hrc = dconServ->CreateInstance(serverID,
|
---|
160 | clsid,
|
---|
161 | id,
|
---|
162 | ppobj);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | return hrc;
|
---|
166 | }
|
---|
167 |
|
---|
168 | HRESULT GlueCreateInstance(const CLSID &clsid,
|
---|
169 | const nsIID &id,
|
---|
170 | void** ppobj)
|
---|
171 | {
|
---|
172 | nsCOMPtr<nsIComponentManager> manager;
|
---|
173 | HRESULT hrc = NS_GetComponentManager(getter_AddRefs(manager));
|
---|
174 | if (SUCCEEDED(hrc))
|
---|
175 | hrc = manager->CreateInstance(clsid,
|
---|
176 | nsnull,
|
---|
177 | id,
|
---|
178 | ppobj);
|
---|
179 | return hrc;
|
---|
180 | }
|
---|
181 |
|
---|
182 | #endif // VBOX_WITH_XPCOM
|
---|
183 |
|
---|
184 | } /* namespace com */
|
---|
185 |
|
---|