]>
Commit | Line | Data |
---|---|---|
1 | /* loadlib.h | |
2 | ||
3 | This file is part of Cygwin. | |
4 | ||
5 | This software is a copyrighted work licensed under the terms of the | |
6 | Cygwin license. Please consult the file "CYGWIN_LICENSE" for | |
7 | details. */ | |
8 | ||
9 | #ifndef _LOADLIB_H | |
10 | #define _LOADLIB_H | |
11 | ||
12 | #include <windows.h> | |
13 | #include <wchar.h> | |
14 | ||
15 | /* Load all system libs from the windows system directory by prepending the | |
16 | full path. This doesn't work for loadling cygwin1.dll. For this case, | |
17 | instead of prepending the path, make sure that the CWD is removed from | |
18 | the DLL search path, if possible (XP SP1++, Vista++). */ | |
19 | static HMODULE _load_sys_library (const wchar_t *dll) __attribute__ ((used)); | |
20 | ||
21 | static HMODULE | |
22 | _load_sys_library (const wchar_t *dll) | |
23 | { | |
24 | static BOOL WINAPI (*set_dll_directory)(LPCWSTR); | |
25 | static WCHAR sysdir[MAX_PATH]; | |
26 | static UINT sysdir_len; | |
27 | ||
28 | WCHAR dllpath[MAX_PATH]; | |
29 | ||
30 | if (!sysdir_len) | |
31 | { | |
32 | sysdir_len = GetSystemDirectoryW (sysdir, MAX_PATH); | |
33 | sysdir[sysdir_len++] = L'\\'; | |
34 | sysdir[sysdir_len] = L'\0'; | |
35 | } | |
36 | if (!set_dll_directory) | |
37 | { | |
38 | HMODULE k32 = GetModuleHandleW (L"kernel32.dll"); | |
39 | if (k32) | |
40 | set_dll_directory = (BOOL WINAPI (*)(LPCWSTR)) | |
41 | GetProcAddress (k32, "SetDllDirectoryW"); | |
42 | if (!set_dll_directory) | |
43 | set_dll_directory = (BOOL WINAPI (*)(LPCWSTR)) -1; | |
44 | else | |
45 | set_dll_directory (L""); | |
46 | } | |
47 | ||
48 | if (wcscmp (dll, L"cygwin1.dll") == 0) | |
49 | return LoadLibraryExW (L"cygwin1.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | |
50 | ||
51 | wcscpy (dllpath, sysdir); | |
52 | wcscpy (dllpath + sysdir_len, dll); | |
53 | return LoadLibraryExW (dllpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | |
54 | } | |
55 | ||
56 | #define LoadLibraryW(d) _load_sys_library(d) | |
57 | #define LoadLibraryA(d) _load_sys_library(L##d) | |
58 | ||
59 | #endif /* _LOADLIB_H */ |