1 /* DllSecur.c -- DLL loading security
2 2018-02-21 : Igor Pavlov : Public domain */
14 typedef BOOL (WINAPI
*Func_SetDefaultDllDirectories
)(DWORD DirectoryFlags
);
16 #define MY_LOAD_LIBRARY_SEARCH_USER_DIRS 0x400
17 #define MY_LOAD_LIBRARY_SEARCH_SYSTEM32 0x800
19 static const char * const g_Dlls
=
36 void My_SetDefaultDllDirectories()
41 vi
.dwOSVersionInfoSize
= sizeof(vi
);
43 if (!GetVersionEx(&vi
) || vi
.dwMajorVersion
!= 6 || vi
.dwMinorVersion
!= 0)
45 Func_SetDefaultDllDirectories setDllDirs
= (Func_SetDefaultDllDirectories
)
46 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "SetDefaultDllDirectories");
48 if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32
| MY_LOAD_LIBRARY_SEARCH_USER_DIRS
))
56 void LoadSecurityDlls()
60 wchar_t buf
[MAX_PATH
+ 100];
63 // at Vista (ver 6.0) : CoCreateInstance(CLSID_ShellLink, ...) doesn't work after SetDefaultDllDirectories() : Check it ???
65 vi
.dwOSVersionInfoSize
= sizeof(vi
);
66 if (!GetVersionEx(&vi
) || vi
.dwMajorVersion
!= 6 || vi
.dwMinorVersion
!= 0)
68 Func_SetDefaultDllDirectories setDllDirs
= (Func_SetDefaultDllDirectories
)
69 GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "SetDefaultDllDirectories");
71 if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32
| MY_LOAD_LIBRARY_SEARCH_USER_DIRS
))
77 unsigned len
= GetSystemDirectoryW(buf
, MAX_PATH
+ 2);
78 if (len
== 0 || len
> MAX_PATH
)
83 unsigned pos
= (unsigned)lstrlenW(buf
);
85 if (buf
[pos
- 1] != '\\')
88 for (dll
= g_Dlls
; dll
[0] != 0;)
94 buf
[pos
+ k
] = (Byte
)c
;
100 lstrcatW(buf
, L
".dll");
101 LoadLibraryExW(buf
, NULL
, LOAD_WITH_ALTERED_SEARCH_PATH
);