Last active
February 2, 2017 19:28
-
-
Save billziss-gh/4385c783c56ef1c52e44940099d44d6b to your computer and use it in GitHub Desktop.
How to delay load the WinFsp DLL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Use the following function to delay load the WinFsp DLL | |
* directly from the WinFsp installation directory. | |
* | |
* You will also need to update your project settings: | |
* - Linker > Input > Delay Loaded DLL's: winfsp-$(PlatformTarget).dll | |
* | |
* Written by Bill Zissimopoulos, 2017. Released to the public domain. | |
*/ | |
static inline | |
NTSTATUS FspLoad(PVOID *PModule) | |
{ | |
#if defined(_WIN64) | |
#define FSP_DLLNAME "winfsp-x64.dll" | |
#else | |
#define FSP_DLLNAME "winfsp-x86.dll" | |
#endif | |
#define FSP_DLLPATH "bin\\" FSP_DLLNAME | |
WCHAR PathBuf[MAX_PATH]; | |
DWORD Size; | |
HKEY RegKey; | |
LONG Result; | |
HMODULE Module; | |
if (0 != PModule) | |
*PModule = 0; | |
Module = LoadLibraryW(L"" FSP_DLLNAME); | |
if (0 == Module) | |
{ | |
Result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\WinFsp", | |
0, KEY_READ | KEY_WOW64_32KEY, &RegKey); | |
if (ERROR_SUCCESS == Result) | |
{ | |
Size = sizeof PathBuf - sizeof L"" FSP_DLLPATH + sizeof(WCHAR); | |
Result = RegGetValueW(RegKey, 0, L"InstallDir", | |
RRF_RT_REG_SZ, 0, PathBuf, &Size); | |
RegCloseKey(RegKey); | |
} | |
if (ERROR_SUCCESS != Result) | |
return STATUS_OBJECT_NAME_NOT_FOUND; | |
RtlCopyMemory(PathBuf + (Size / sizeof(WCHAR) - 1), L"" FSP_DLLPATH, sizeof L"" FSP_DLLPATH); | |
Module = LoadLibraryW(PathBuf); | |
if (0 == Module) | |
return STATUS_DLL_NOT_FOUND; | |
} | |
if (0 != PModule) | |
*PModule = Module; | |
return STATUS_SUCCESS; | |
#undef FSP_DLLNAME | |
#undef FSP_DLLPATH | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment