1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5#include <xplatform.h>
6
7#ifdef WINDOWS
8#include "mscoree.h"
9
10typedef HRESULT (STDAPICALLTYPE *FnGetCLRRuntimeHost)(REFIID riid, IUnknown **pUnk);
11
12// Returns the ICLRRuntimeHost instance or nullptr on failure.
13ICLRRuntimeHost* GetCLRRuntimeHost()
14{
15 HMODULE coreCLRModule = ::GetModuleHandle(L"coreclr.dll");
16 if (!coreCLRModule)
17 {
18 coreCLRModule = ::GetModuleHandle(L"coreclr.so");
19 }
20 if (!coreCLRModule)
21 {
22 coreCLRModule = ::GetModuleHandle(L"coreclr.dynlib");
23 }
24 if (!coreCLRModule)
25 {
26 return nullptr;
27 }
28
29 FnGetCLRRuntimeHost pfnGetCLRRuntimeHost = (FnGetCLRRuntimeHost)::GetProcAddress(coreCLRModule, "GetCLRRuntimeHost");
30 if (!pfnGetCLRRuntimeHost)
31 {
32 return nullptr;
33 }
34
35 ICLRRuntimeHost* clrRuntimeHost = nullptr;
36 HRESULT hr = pfnGetCLRRuntimeHost(IID_ICLRRuntimeHost, (IUnknown**)&clrRuntimeHost);
37 if (FAILED(hr)) {
38 return nullptr;
39 }
40
41 return clrRuntimeHost;
42}
43
44extern "C" DLL_EXPORT int STDMETHODCALLTYPE
45CallExecuteInDefaultAppDomain(LPCWSTR pwzAssemblyPath,
46 LPCWSTR pwzTypeName,
47 LPCWSTR pwzMethodName,
48 LPCWSTR pwzArgument,
49 DWORD *pReturnValue)
50{
51 ICLRRuntimeHost* host = GetCLRRuntimeHost();
52
53 if (!host)
54 return E_FAIL;
55
56 auto result = host->ExecuteInDefaultAppDomain(pwzAssemblyPath, pwzTypeName, pwzMethodName, pwzArgument, pReturnValue);
57
58 host->Release();
59
60 return result;
61}
62
63#else // WINDOWS
64
65#include <cstdint>
66
67typedef uint32_t DWORD;
68
69extern "C" DLL_EXPORT int STDMETHODCALLTYPE
70CallExecuteInDefaultAppDomain(LPCWSTR pwzAssemblyPath,
71 LPCWSTR pwzTypeName,
72 LPCWSTR pwzMethodName,
73 LPCWSTR pwzArgument,
74 DWORD *pReturnValue)
75{
76 const int E_FAIL = 0x80004005;
77
78 return E_FAIL;
79}
80
81#endif // WINDOWS