Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 79e230e78ec5e9f59176321d29a82947d33d8e44 (plain) (blame)
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
// dllmain.cpp : Defines the entry point for the DLL application.
#include <windows.h>
#include <delayimp.h>
#include <assert.h>

static HMODULE getCurrentModule() {
    HMODULE module;
    if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
                           (LPCTSTR)getCurrentModule, &module)) {
        assert(false);
    }
    return module;
}

HMODULE PTYExplicitLoadLibrary(LPCSTR pszModuleName) {
    if (lstrcmpiA(pszModuleName, "winpty.dll") == 0) {
        CHAR szPath[MAX_PATH] = "";
        //_hdllInstance is the HMODULE of *this* module
        DWORD cchPath = GetModuleFileNameA(getCurrentModule(), szPath, MAX_PATH);
        while (cchPath > 0) {
            switch (szPath[cchPath - 1]) {
            case '\\':
            case '/':
            case ':':
                break;
            default:
                --cchPath;
                continue;
            }
            break; // stop searching; found path separator
        }
        lstrcpynA(szPath + cchPath, pszModuleName, MAX_PATH - cchPath);
        return LoadLibraryA(szPath); // call with full path to dependent DLL
    }
    return NULL;
}

FARPROC WINAPI PTYDliNotifyHook(unsigned dliNotify, PDelayLoadInfo pdli) {
    if (dliNotify == dliNotePreLoadLibrary) {
        return (FARPROC)PTYExplicitLoadLibrary(pdli->szDll);
    }
    return NULL;
}

extern "C" {
PfnDliHook __pfnDliNotifyHook2 = PTYDliNotifyHook;
}

Back to the top