VirtualBox

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

Last change on this file since 93483 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/* $Id: com.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer
4 */
5
6/*
7 * Copyright (C) 2005-2022 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 <iprt/errcore.h>
49#include <VBox/log.h>
50
51
52/*********************************************************************************************************************************
53* Global Variables *
54*********************************************************************************************************************************/
55namespace com
56{
57/* static */
58const Guid Guid::Empty; /* default ctor is OK */
59
60const char Zeroes[16] = {0, };
61
62
63void GetInterfaceNameByIID(const GUID &aIID, BSTR *aName)
64{
65 AssertPtrReturnVoid(aName);
66 *aName = NULL;
67
68#if !defined(VBOX_WITH_XPCOM)
69
70 LONG rc;
71 LPOLESTR iidStr = NULL;
72 if (StringFromIID(aIID, &iidStr) == S_OK)
73 {
74 HKEY ifaceKey;
75 rc = RegOpenKeyExW(HKEY_CLASSES_ROOT, L"Interface",
76 0, KEY_QUERY_VALUE, &ifaceKey);
77 if (rc == ERROR_SUCCESS)
78 {
79 HKEY iidKey;
80 rc = RegOpenKeyExW(ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);
81 if (rc == ERROR_SUCCESS)
82 {
83 /* determine the size and type */
84 DWORD sz, type;
85 rc = RegQueryValueExW(iidKey, NULL, NULL, &type, NULL, &sz);
86 if (rc == ERROR_SUCCESS && type == REG_SZ)
87 {
88 /* query the value to BSTR */
89 *aName = SysAllocStringLen(NULL, (sz + 1) / sizeof(TCHAR) + 1);
90 rc = RegQueryValueExW(iidKey, NULL, NULL, NULL, (LPBYTE) *aName, &sz);
91 if (rc != ERROR_SUCCESS)
92 {
93 SysFreeString(*aName);
94 *aName = NULL;
95 }
96 }
97 RegCloseKey(iidKey);
98 }
99 RegCloseKey(ifaceKey);
100 }
101 CoTaskMemFree(iidStr);
102 }
103
104#else /* !defined (VBOX_WITH_XPCOM) */
105
106 nsresult rv;
107 nsCOMPtr<nsIInterfaceInfoManager> iim =
108 do_GetService(NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);
109 if (NS_SUCCEEDED(rv))
110 {
111 nsCOMPtr<nsIInterfaceInfo> iinfo;
112 rv = iim->GetInfoForIID(&aIID, getter_AddRefs(iinfo));
113 if (NS_SUCCEEDED(rv))
114 {
115 const char *iname = NULL;
116 iinfo->GetNameShared(&iname);
117 char *utf8IName = NULL;
118 if (RT_SUCCESS(RTStrCurrentCPToUtf8(&utf8IName, iname)))
119 {
120 PRTUTF16 utf16IName = NULL;
121 if (RT_SUCCESS(RTStrToUtf16(utf8IName, &utf16IName)))
122 {
123 *aName = SysAllocString((OLECHAR *) utf16IName);
124 RTUtf16Free(utf16IName);
125 }
126 RTStrFree(utf8IName);
127 }
128 }
129 }
130
131#endif /* !defined (VBOX_WITH_XPCOM) */
132}
133
134#ifdef VBOX_WITH_XPCOM
135
136HRESULT GlueCreateObjectOnServer(const CLSID &clsid,
137 const char *serverName,
138 const nsIID &id,
139 void** ppobj)
140{
141 HRESULT rc;
142 nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);
143 if (SUCCEEDED(rc))
144 {
145 PRUint32 serverID = 0;
146 rc = ipcServ->ResolveClientName(serverName, &serverID);
147 if (SUCCEEDED (rc))
148 {
149 nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc);
150 if (SUCCEEDED(rc))
151 rc = dconServ->CreateInstance(serverID,
152 clsid,
153 id,
154 ppobj);
155 }
156 }
157 return rc;
158}
159
160HRESULT GlueCreateInstance(const CLSID &clsid,
161 const nsIID &id,
162 void** ppobj)
163{
164 nsCOMPtr<nsIComponentManager> manager;
165 HRESULT rc = NS_GetComponentManager(getter_AddRefs(manager));
166 if (SUCCEEDED(rc))
167 rc = manager->CreateInstance(clsid,
168 nsnull,
169 id,
170 ppobj);
171 return rc;
172}
173
174#endif // VBOX_WITH_XPCOM
175
176} /* namespace com */
177
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