summaryrefslogtreecommitdiff
path: root/src/dlls/mscorpe/mscorpe/wrapper.cpp
blob: d2f1701ec47f9ba4b5692f5fc54ab908c4c40b24 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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.
// 
// File: wrapper.cpp
// 

// 
// This file implements a simple wrapper DLL (mscorpe.dll) which calls properly into mscorpehost.dll.
// It exists because of compatibility with 1.x/2.0 apps running on CLR 4.0+. Such older apps could pass 
// full path to LoadLibrary() Windows API and get this DLL.
// 
// Noone in CLR should ever try to load this DLL directly (using LoadLibrary API). Note that hosting APIs 
// and PInvoke redirect mscorpe.dll to mscorpehost.dll automatically.
// 

#include <MscorpeSxSWrapper.h>

#include <mscoree.h>
#include <metahost.h>

// Globals
HINSTANCE g_hThisInst;  // This library.

//*****************************************************************************
// Handle lifetime of loaded library.
//*****************************************************************************
extern "C"
BOOL WINAPI 
DllMain(
    HINSTANCE hInstance, 
    DWORD     dwReason, 
    LPVOID    lpReserved)
{
    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
        {   // Save the module handle.
            g_hThisInst = hInstance;
            DisableThreadLibraryCalls((HMODULE)hInstance);
        }
        break;
    case DLL_PROCESS_DETACH:
        break;
    }

    return TRUE;
} // DllMain

// Implementation for utilcode
HINSTANCE 
GetModuleInst()
{
    return g_hThisInst;
} // GetModuleInst

// Load correct SxS version of mscorpe.dll and initialize it (uses shim).
HRESULT 
LoadMscorpe(HMODULE * phModule)
{
    HRESULT hr = S_OK;
    ICLRMetaHost *    pMetaHost = NULL;
    ICLRRuntimeInfo * pCLRRuntimeInfo = NULL;
    
    // Get full DLL path
    WCHAR wszPath[_MAX_PATH];
    DWORD dwLength = GetModuleFileName((HMODULE)g_hThisInst, wszPath, NumItems(wszPath));
    
    if ((dwLength == 0) || 
        ((dwLength == NumItems(wszPath)) && 
         (GetLastError() == ERROR_INSUFFICIENT_BUFFER)))
    {
        IfFailGo(CLR_E_SHIM_RUNTIMELOAD);
    }
    
    // Find start of '\mscorpe.dll'
    LPWSTR wszSeparator = wcsrchr(wszPath, L'\\');
    if (wszSeparator == NULL)
    {
        IfFailGo(CLR_E_SHIM_RUNTIMELOAD);
    }
    // Check the name of this DLL
    _ASSERTE(_wcsicmp(wszSeparator, L"\\mscorpe.dll") == 0);
    // Remove the DLL name
    *wszSeparator = 0;

    // Find start of last directory name (\<version>),
    // C:\Windows\Microsoft.NET\Framework\[[v4.0.12345]]\mscorpe.dll
    LPWSTR wszLastDirectoryName = wcsrchr(wszPath, L'\\');
    if (wszLastDirectoryName == NULL)
    {
        IfFailGo(CLR_E_SHIM_RUNTIMELOAD);
    }
    LPWSTR wszVersion = wszLastDirectoryName + 1;
    
    IfFailGo(CLRCreateInstance(
        CLSID_CLRMetaHost, 
        IID_ICLRMetaHost, 
        reinterpret_cast<LPVOID *>(&pMetaHost)));
    
    IfFailGo(pMetaHost->GetRuntime(
        wszVersion, 
        IID_ICLRRuntimeInfo, 
        reinterpret_cast<LPVOID *>(&pCLRRuntimeInfo)));
    
    // Shim will load correct SxS version of mscorpe.dll and will initialize it
    IfFailGo(pCLRRuntimeInfo->LoadLibrary(
        L"mscorpe.dll", 
        phModule));
    
ErrExit:
    if (pMetaHost != NULL)
    {
        pMetaHost->Release();
        pMetaHost = NULL;
    }
    if (pCLRRuntimeInfo != NULL)
    {
        pCLRRuntimeInfo->Release();
        pCLRRuntimeInfo = NULL;
    }
    
    if (FAILED(hr))
    {
        *phModule = NULL;
    }
    
    return hr;
} // LoadMscorpe

// SxS wrapper of mscorpe.dll entrypoints
typedef MscorpeSxSWrapper<LoadMscorpe> MscorpeSxS;

// Export of 'original' 1.x/2.0 mscorpe.dll
EXTERN_C 
HRESULT __stdcall 
CreateICeeFileGen(
    ICeeFileGen ** ppCeeFileGen)
{
    return MscorpeSxS::CreateICeeFileGen(ppCeeFileGen);
}

// Export of 'original' 1.x/2.0 mscorpe.dll
EXTERN_C 
HRESULT __stdcall 
DestroyICeeFileGen(ICeeFileGen ** ppCeeFileGen)
{
    return MscorpeSxS::DestroyICeeFileGen(ppCeeFileGen);
}