summaryrefslogtreecommitdiff
path: root/network/ndis/ndisprot_kmdf/notifyob/dllmain.cpp
blob: 2452d95c4abbcdc030aa62f7ef50882c049b2eea (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*++

Copyright (c) Microsoft Corporation. All rights reserved.

Module Name:

    dllmain.cpp

Abstract:

    The module contains the routines to handle the loading and unloading of the
    notify object dll and the Wdf Coinstaller library.

--*/
          
#include "Common.hpp"
#include <DriverSpecs.h>
_Analysis_mode_(_Analysis_code_type_user_code_)  
#include <strsafe.h>
#include "ProtNotify_i.c"


CComModule _Module;

// for example, WDF 1.9 is "01009". the size 6 includes the ending NULL marker
//
#define MAX_VERSION_SIZE 6

WCHAR G_coInstallerVersion[MAX_VERSION_SIZE] = {0};

HMODULE                  CoinstallerLibrary      = NULL;
PFN_WDFPREDEVICEINSTALL  pfnWdfPreDeviceInstall  = NULL;
PFN_WDFPOSTDEVICEINSTALL pfnWdfPostDeviceInstall = NULL;
PFN_WDFPREDEVICEREMOVE   pfnWdfPreDeviceRemove   = NULL;
PFN_WDFPOSTDEVICEREMOVE  pfnWdfPostDeviceRemove  = NULL;

//
// Private methods.
//
HMODULE
LoadWdfCoInstaller(
    );

VOID
UnloadWdfCoInstaller(
    HMODULE Library
    );


extern "C"
BOOL
WINAPI
DllMain(
    _In_ HINSTANCE Instance,
    _In_ DWORD Reason,
    _In_ LPVOID Reserved
    )
{
    UNREFERENCED_PARAMETER(Reserved);

    if (Reason == DLL_PROCESS_ATTACH) {
        //
        // Initialize the COM Server module with the object map.
	// Do this prior to loading the coinstaller as the detach 
        // assumes the Module is initialized.
        //
        _Module.Init(ObjectMap, Instance);
        DisableThreadLibraryCalls(Instance);

        //
        // Load the Wdf Coinstaller library.
        //
        CoinstallerLibrary = LoadWdfCoInstaller();
        if (CoinstallerLibrary == NULL) {
            return FALSE;
        }

    }
    else if (Reason == DLL_PROCESS_DETACH) {

        //
        // Relesae the COM Server.
        //
        _Module.Term();

        //
        // Unload the Wdf Coinstaller library.
        //
        UnloadWdfCoInstaller(CoinstallerLibrary);
    }

    return TRUE;
}

__control_entrypoint(DllExport)
STDAPI DllCanUnloadNow(
    )
{
    //
    // Determine whether the DLL can be unloaded by OLE
    //
    return (_Module.GetLockCount() == 0) ? S_OK : S_FALSE;
}

_Check_return_
STDAPI
DllGetClassObject(
    _In_ REFCLSID rclsid,
    _In_ REFIID riid,
    _Outptr_ LPVOID FAR* ppv
    )
{
    //
    // Return a class factory to create an object of the requested type
    //
    return _Module.GetClassObject(rclsid, riid, ppv);
}


STDAPI DllRegisterServer(
    )
{

    //
    // Register object, typelib and all interfaces in typelib
    //
    return _Module.RegisterServer(TRUE);
}


STDAPI DllUnregisterServer(
    )
{
    //
    // Remove entries from the system registry
    //
    _Module.UnregisterServer();
    return S_OK;
}

PWCHAR
GetCoinstallerVersion(
    VOID
    )
{
    if (FAILED( StringCchPrintf(G_coInstallerVersion,
                                MAX_VERSION_SIZE,
                                L"%02d%03d",    // for example, "01009"
                                KMDF_VERSION_MAJOR,
                                KMDF_VERSION_MINOR)))
    {
        printf("StringCchCopy failed with error \n");
    }

    return (PWCHAR)&G_coInstallerVersion;
}

HMODULE
LoadWdfCoInstaller(
    )
{
   
    #pragma prefast(suppress:6262, "Supprress overflow warnings")        
    HRESULT hr = S_OK;
    HMODULE library = NULL;
    WCHAR coinstaller[MAX_PATH] = {0};
    WCHAR   coinstallerName[MAX_PATH/2];
    PWCHAR  coinstallerVersion;

    //
    // Construct the full file name for the Wdf Coinstaller dll.
    //
    if (GetSystemDirectory(coinstaller, MAX_PATH) == 0) {
        hr = GetLastError();
        goto exit;
    }
    coinstallerVersion = GetCoinstallerVersion();
    if (FAILED( StringCchPrintf(coinstallerName,
                              MAX_PATH/2,
                              L"\\WdfCoInstaller%s.dll",
                              coinstallerVersion) )) {
        goto exit;
    }

    hr = StringCchCat(coinstaller, MAX_PATH, coinstallerName);
    if (FAILED(hr)) {
        goto exit;
    }

    //
    // Load the Wdf Coinstaller library.
    //
#pragma prefast(suppress:28160, "Suppressing false positive from PFD")                
    library = LoadLibrary(coinstaller);
    if (library == NULL) {
        hr = GetLastError();
        goto exit;
    }

    pfnWdfPreDeviceInstall = (PFN_WDFPREDEVICEINSTALL) GetProcAddress(library, "WdfPreDeviceInstall");
    if (pfnWdfPreDeviceInstall == NULL) {
        hr = GetLastError();
        goto exit;
    }

    pfnWdfPostDeviceInstall = (PFN_WDFPOSTDEVICEINSTALL) GetProcAddress(library, "WdfPostDeviceInstall");
    if (pfnWdfPostDeviceInstall == NULL) {
        hr = GetLastError();
        goto exit;
    }

    pfnWdfPreDeviceRemove = (PFN_WDFPREDEVICEREMOVE) GetProcAddress(library, "WdfPreDeviceRemove");
    if (pfnWdfPreDeviceRemove == NULL) {
        hr = GetLastError();
        goto exit;
    }

    pfnWdfPostDeviceRemove = (PFN_WDFPREDEVICEREMOVE) GetProcAddress(library, "WdfPostDeviceRemove");
    if (pfnWdfPostDeviceRemove == NULL) {
        hr = GetLastError();
        goto exit;
    }

exit:

    //
    // Unload the Wdf Coinstaller library in case of an error.
    //
    if (FAILED(hr)) {
        UnloadWdfCoInstaller(library);
        library = NULL;
    }

    SetLastError(hr);
    return library;
}


VOID
UnloadWdfCoInstaller(
    HMODULE Library
    )
{
    if (Library) {
        FreeLibrary( Library );
    }

    pfnWdfPreDeviceInstall  = NULL;
    pfnWdfPostDeviceInstall = NULL;
    pfnWdfPreDeviceRemove   = NULL;
    pfnWdfPostDeviceRemove  = NULL;
}