dllhost.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright 2022 Dmitry Timoshkov
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. */
  18. #include <stdarg.h>
  19. #define COBJMACROS
  20. #include <windef.h>
  21. #include <winbase.h>
  22. #include <objbase.h>
  23. #include "wine/debug.h"
  24. WINE_DEFAULT_DEBUG_CHANNEL(dllhost);
  25. struct factory
  26. {
  27. IClassFactory IClassFactory_iface;
  28. IMarshal IMarshal_iface;
  29. CLSID clsid;
  30. LONG ref;
  31. IClassFactory *dll_factory;
  32. };
  33. static inline struct factory *impl_from_IClassFactory(IClassFactory *iface)
  34. {
  35. return CONTAINING_RECORD(iface, struct factory, IClassFactory_iface);
  36. }
  37. static inline struct factory *impl_from_IMarshal(IMarshal *iface)
  38. {
  39. return CONTAINING_RECORD(iface, struct factory, IMarshal_iface);
  40. }
  41. static HRESULT WINAPI factory_QueryInterface(IClassFactory *iface,
  42. REFIID iid, void **ppv)
  43. {
  44. struct factory *factory = impl_from_IClassFactory(iface);
  45. TRACE("(%p,%s,%p)\n", iface, wine_dbgstr_guid(iid), ppv);
  46. if (!ppv) return E_INVALIDARG;
  47. if (IsEqualIID(iid, &IID_IUnknown) ||
  48. IsEqualIID(iid, &IID_IClassFactory))
  49. {
  50. IClassFactory_AddRef(&factory->IClassFactory_iface);
  51. *ppv = &factory->IClassFactory_iface;
  52. return S_OK;
  53. }
  54. else if (IsEqualIID(iid, &IID_IMarshal))
  55. {
  56. IClassFactory_AddRef(&factory->IClassFactory_iface);
  57. *ppv = &factory->IMarshal_iface;
  58. return S_OK;
  59. }
  60. *ppv = NULL;
  61. return E_NOINTERFACE;
  62. }
  63. static ULONG WINAPI factory_AddRef(IClassFactory *iface)
  64. {
  65. struct factory *factory = impl_from_IClassFactory(iface);
  66. ULONG ref = InterlockedIncrement(&factory->ref);
  67. TRACE("(%p)->%lu\n", iface, ref);
  68. return ref;
  69. }
  70. static ULONG WINAPI factory_Release(IClassFactory *iface)
  71. {
  72. struct factory *factory = impl_from_IClassFactory(iface);
  73. ULONG ref = InterlockedDecrement(&factory->ref);
  74. TRACE("(%p)->%lu\n", iface, ref);
  75. if (!ref)
  76. {
  77. if (factory->dll_factory) IClassFactory_Release(factory->dll_factory);
  78. HeapFree(GetProcessHeap(), 0, factory);
  79. }
  80. return ref;
  81. }
  82. static HRESULT WINAPI factory_CreateInstance(IClassFactory *iface,
  83. IUnknown *punkouter, REFIID iid, void **ppv)
  84. {
  85. FIXME("(%p,%p,%s,%p): stub\n", iface, punkouter, wine_dbgstr_guid(iid), ppv);
  86. return E_NOTIMPL;
  87. }
  88. static HRESULT WINAPI factory_LockServer(IClassFactory *iface, BOOL lock)
  89. {
  90. TRACE("(%p,%d)\n", iface, lock);
  91. return S_OK;
  92. }
  93. static const IClassFactoryVtbl ClassFactory_Vtbl =
  94. {
  95. factory_QueryInterface,
  96. factory_AddRef,
  97. factory_Release,
  98. factory_CreateInstance,
  99. factory_LockServer
  100. };
  101. static HRESULT WINAPI marshal_QueryInterface(IMarshal *iface, REFIID iid, LPVOID *ppv)
  102. {
  103. struct factory *factory = impl_from_IMarshal(iface);
  104. TRACE("(%p,%s,%p)\n", iface, wine_dbgstr_guid(iid), ppv);
  105. return IClassFactory_QueryInterface(&factory->IClassFactory_iface, iid, ppv);
  106. }
  107. static ULONG WINAPI marshal_AddRef(IMarshal *iface)
  108. {
  109. struct factory *factory = impl_from_IMarshal(iface);
  110. TRACE("(%p)\n", iface);
  111. return IClassFactory_AddRef(&factory->IClassFactory_iface);
  112. }
  113. static ULONG WINAPI marshal_Release(IMarshal *iface)
  114. {
  115. struct factory *factory = impl_from_IMarshal(iface);
  116. TRACE("(%p)\n", iface);
  117. return IClassFactory_Release(&factory->IClassFactory_iface);
  118. }
  119. static HRESULT WINAPI marshal_GetUnmarshalClass(IMarshal *iface, REFIID iid, void *pv,
  120. DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, CLSID *clsid)
  121. {
  122. TRACE("(%p,%s,%p,%08lx,%p,%08lx,%p)\n", iface, wine_dbgstr_guid(iid), pv,
  123. dwDestContext, pvDestContext, mshlflags, clsid);
  124. *clsid = CLSID_StdMarshal;
  125. return S_OK;
  126. }
  127. static HRESULT WINAPI marshal_GetMarshalSizeMax(IMarshal *iface, REFIID iid, void *pv,
  128. DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, DWORD *size)
  129. {
  130. FIXME("(%p,%s,%p,%08lx,%p,%08lx,%p): stub\n", iface, wine_dbgstr_guid(iid), pv,
  131. dwDestContext, pvDestContext, mshlflags, size);
  132. return E_NOTIMPL;
  133. }
  134. static HRESULT WINAPI marshal_MarshalInterface(IMarshal *iface, IStream *stream, REFIID iid,
  135. void *pv, DWORD dwDestContext, void *pvDestContext, DWORD mshlflags)
  136. {
  137. struct factory *factory = impl_from_IMarshal(iface);
  138. TRACE("(%p,%s,%p,%08lx,%p,%08lx)\n", stream, wine_dbgstr_guid(iid), pv, dwDestContext, pvDestContext, mshlflags);
  139. return CoMarshalInterface(stream, iid, (IUnknown *)factory->dll_factory, dwDestContext, pvDestContext, mshlflags);
  140. }
  141. static HRESULT WINAPI marshal_UnmarshalInterface(IMarshal *iface, IStream *stream,
  142. REFIID iid, void **ppv)
  143. {
  144. FIXME("(%p,%p,%s,%p): stub\n", iface, stream, wine_dbgstr_guid(iid), ppv);
  145. return E_NOTIMPL;
  146. }
  147. static HRESULT WINAPI marshal_ReleaseMarshalData(IMarshal *iface, IStream *stream)
  148. {
  149. TRACE("(%p,%p)\n", iface, stream);
  150. return S_OK;
  151. }
  152. static HRESULT WINAPI marshal_DisconnectObject(IMarshal *iface, DWORD reserved)
  153. {
  154. TRACE("(%p, %08lx)\n", iface, reserved);
  155. return S_OK;
  156. }
  157. static const IMarshalVtbl Marshal_Vtbl =
  158. {
  159. marshal_QueryInterface,
  160. marshal_AddRef,
  161. marshal_Release,
  162. marshal_GetUnmarshalClass,
  163. marshal_GetMarshalSizeMax,
  164. marshal_MarshalInterface,
  165. marshal_UnmarshalInterface,
  166. marshal_ReleaseMarshalData,
  167. marshal_DisconnectObject
  168. };
  169. struct surrogate
  170. {
  171. ISurrogate ISurrogate_iface;
  172. IClassFactory *factory;
  173. DWORD cookie;
  174. HANDLE event;
  175. LONG ref;
  176. };
  177. static inline struct surrogate *impl_from_ISurrogate(ISurrogate *iface)
  178. {
  179. return CONTAINING_RECORD(iface, struct surrogate, ISurrogate_iface);
  180. }
  181. static HRESULT WINAPI surrogate_QueryInterface(ISurrogate *iface,
  182. REFIID iid, void **ppv)
  183. {
  184. struct surrogate *surrogate = impl_from_ISurrogate(iface);
  185. TRACE("(%p,%s,%p)\n", iface, wine_dbgstr_guid(iid), ppv);
  186. if (!ppv) return E_INVALIDARG;
  187. if (IsEqualIID(iid, &IID_IUnknown) ||
  188. IsEqualIID(iid, &IID_ISurrogate))
  189. {
  190. ISurrogate_AddRef(&surrogate->ISurrogate_iface);
  191. *ppv = &surrogate->ISurrogate_iface;
  192. return S_OK;
  193. }
  194. *ppv = NULL;
  195. return E_NOINTERFACE;
  196. }
  197. static ULONG WINAPI surrogate_AddRef(ISurrogate *iface)
  198. {
  199. TRACE("(%p)\n", iface);
  200. return 2;
  201. }
  202. static ULONG WINAPI surrogate_Release(ISurrogate *iface)
  203. {
  204. TRACE("(%p)\n", iface);
  205. return 1;
  206. }
  207. static HRESULT WINAPI surrogate_LoadDllServer(ISurrogate *iface, const CLSID *clsid)
  208. {
  209. struct surrogate *surrogate = impl_from_ISurrogate(iface);
  210. struct factory *factory;
  211. HRESULT hr;
  212. TRACE("(%p,%s)\n", iface, wine_dbgstr_guid(clsid));
  213. factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
  214. if (!factory)
  215. return E_OUTOFMEMORY;
  216. factory->IClassFactory_iface.lpVtbl = &ClassFactory_Vtbl;
  217. factory->IMarshal_iface.lpVtbl = &Marshal_Vtbl;
  218. factory->clsid = *clsid;
  219. factory->ref = 1;
  220. factory->dll_factory = NULL;
  221. hr = CoGetClassObject(clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IClassFactory, (void **)&factory->dll_factory);
  222. if (SUCCEEDED(hr))
  223. hr = CoRegisterClassObject(clsid, (IUnknown *)&factory->IClassFactory_iface,
  224. CLSCTX_LOCAL_SERVER, REGCLS_SURROGATE, &surrogate->cookie);
  225. if (FAILED(hr))
  226. IClassFactory_Release(&factory->IClassFactory_iface);
  227. else
  228. {
  229. surrogate->factory = &factory->IClassFactory_iface;
  230. surrogate->event = CreateEventW(NULL, FALSE, FALSE, NULL);
  231. }
  232. return hr;
  233. }
  234. static HRESULT WINAPI surrogate_FreeSurrogate(ISurrogate *iface)
  235. {
  236. struct surrogate *surrogate = impl_from_ISurrogate(iface);
  237. TRACE("(%p)\n", iface);
  238. if (surrogate->cookie)
  239. {
  240. CoRevokeClassObject(surrogate->cookie);
  241. surrogate->cookie = 0;
  242. }
  243. if (surrogate->factory)
  244. {
  245. IClassFactory_Release(surrogate->factory);
  246. surrogate->factory = NULL;
  247. }
  248. SetEvent(surrogate->event);
  249. return S_OK;
  250. }
  251. static const ISurrogateVtbl Surrogate_Vtbl =
  252. {
  253. surrogate_QueryInterface,
  254. surrogate_AddRef,
  255. surrogate_Release,
  256. surrogate_LoadDllServer,
  257. surrogate_FreeSurrogate
  258. };
  259. int WINAPI wWinMain(HINSTANCE hinst, HINSTANCE previnst, LPWSTR cmdline, int showcmd)
  260. {
  261. HRESULT hr;
  262. CLSID clsid;
  263. struct surrogate surrogate;
  264. TRACE("Running as %u-bit\n", (int)sizeof(void *) * 8);
  265. if (wcsnicmp(cmdline, L"/PROCESSID:", 11))
  266. return 0;
  267. cmdline += 11;
  268. surrogate.ISurrogate_iface.lpVtbl = &Surrogate_Vtbl;
  269. surrogate.factory = NULL;
  270. surrogate.cookie = 0;
  271. surrogate.event = NULL;
  272. surrogate.ref = 1;
  273. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  274. hr = CLSIDFromString(cmdline, &clsid);
  275. if (hr == S_OK)
  276. {
  277. CoRegisterSurrogate(&surrogate.ISurrogate_iface);
  278. hr = ISurrogate_LoadDllServer(&surrogate.ISurrogate_iface, &clsid);
  279. if (hr != S_OK)
  280. {
  281. ERR("Can't create instance of %s\n", wine_dbgstr_guid(&clsid));
  282. goto cleanup;
  283. }
  284. while (WaitForSingleObject(surrogate.event, 30000) != WAIT_OBJECT_0)
  285. CoFreeUnusedLibraries();
  286. }
  287. cleanup:
  288. CoUninitialize();
  289. return 0;
  290. }