summaryrefslogtreecommitdiff
path: root/src/strongname/api/strongnamecoreclr.cpp
blob: b02cde3dd9a1b0a7c01703ae7301bf4e466573ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//
// Several strong name tools are in a special scenario because they build in the CoreCLR build process, but
// are expected to run against the desktop VM. Because of this, we need to setup the callback structure that
// Utilcode asks for to point to callbacks that shim to the desktop.  These methods provide that shimming functionality.
//

#include "common.h"

CoreClrCallbacks *GetCoreClrCallbacks();

//
// Get a pointer to an API out of the shim
//
// Arguments:
//    szApiName - name of the API to get a pointer to
//
//

template<typename FunctionPointer>
FunctionPointer ApiShim(LPCSTR szApiName)
{
    static FunctionPointer pfnApi = NULL;

    if (pfnApi == NULL)
    {
        CoreClrCallbacks *pCallbacks = GetCoreClrCallbacks();
        pfnApi = reinterpret_cast<FunctionPointer>(GetProcAddress(pCallbacks->m_hmodCoreCLR, szApiName));
        _ASSERTE(pfnApi != NULL);
    }

    return pfnApi;
}

//
// Shim APIs, passing off into the desktop VM
//

IExecutionEngine * __stdcall SnIEE()
{
    typedef IExecutionEngine * ( __stdcall *IEEFn_t)();
    return ApiShim<IEEFn_t>("IEE")();
}

STDAPI SnGetCorSystemDirectory(SString&  pbuffer)
{
    typedef HRESULT (__stdcall *GetCorSystemDirectoryFn_t)(SString&);
    return ApiShim<GetCorSystemDirectoryFn_t>("GetCORSystemDirectory")(pbuffer);
}

//
// Initialize a set of CoreCLR callbacks for utilcode to call into the VM with
//
// Return Value:
//    CoreClrCallbacks for UtilCode
//
// Notes:
//    Will not return NULL
//

CoreClrCallbacks *GetCoreClrCallbacks()
{
    static CoreClrCallbacks coreClrCallbacks = { 0 };
    if (coreClrCallbacks.m_hmodCoreCLR == NULL)
    {
        // Run against the desktop CLR
        coreClrCallbacks.m_hmodCoreCLR = WszLoadLibrary(W("mscoree.dll"));
        coreClrCallbacks.m_pfnIEE = SnIEE;
        coreClrCallbacks.m_pfnGetCORSystemDirectory = SnGetCorSystemDirectory;
        coreClrCallbacks.m_pfnGetCLRFunction = NULL;
    }

    return &coreClrCallbacks;
}

// Initialize Utilcode
//
// Notes:
//    Should only be called once
//

void InitUtilcode()
{
#ifdef _DEBUG
    static bool fAlreadyInitialized = false;
    _ASSERTE(!fAlreadyInitialized);
    fAlreadyInitialized = true;
#endif

    InitUtilcode(*GetCoreClrCallbacks());
}