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