OSDN Git Service

* loadlib.h: New header implementing safe LoadLibrary calls.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / utils / loadlib.h
1 /* loadlib.h
2
3    Copyright 2010 Red Hat, Inc.
4
5    This file is part of Cygwin.
6
7    This software is a copyrighted work licensed under the terms of the
8    Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9    details. */
10
11 #ifndef _LOADLIB_H
12 #define _LOADLIB_H
13
14 #include <windows.h>
15 #include <wchar.h>
16
17 /* Load all system libs from the windows system directory by prepending the
18    full path.  This doesn't work for loadling cygwin1.dll.  For this case,
19    instead of prepending the path, make sure that the CWD is removed from
20    the DLL search path, if possible (XP SP1++, Vista++). */
21 static HMODULE
22 _load_sys_library (const wchar_t *dll)
23 {
24   static BOOL (*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 (*)(LPCWSTR))
41                      GetProcAddress (k32, "SetDllDirectoryW");
42       if (!set_dll_directory)
43         set_dll_directory = (BOOL (*)(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 */