diff options
author | Ron Yorston <rmy@pobox.com> | 2020-06-02 07:53:10 +0100 |
---|---|---|
committer | Ron Yorston <rmy@pobox.com> | 2020-06-02 08:27:46 +0100 |
commit | 27c718aa1a4674587925adb543362cb8e60814c4 (patch) | |
tree | db22579586cede7cb0f3351ce66c36f98a9a5a7a /win32/mingw.c | |
parent | 5ea460a32a9882906c7ee3656b8fb0dcdbce2abc (diff) | |
download | busybox-w32-27c718aa1a4674587925adb543362cb8e60814c4.tar.gz busybox-w32-27c718aa1a4674587925adb543362cb8e60814c4.tar.bz2 busybox-w32-27c718aa1a4674587925adb543362cb8e60814c4.zip |
win32: use lazy loading for certain DLLs
Only a handful of functions are used from shell32.dll, userenv.dll
and psapi.dll. Mostly these functions are in out of the way places.
By loading the functions only when required we can avoid the startup
cost of linking the three DLLs in the common case that they aren't
needed.
Diffstat (limited to 'win32/mingw.c')
-rw-r--r-- | win32/mingw.c | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/win32/mingw.c b/win32/mingw.c index 5c4c39b9d..105b7864d 100644 --- a/win32/mingw.c +++ b/win32/mingw.c | |||
@@ -837,17 +837,17 @@ static char *gethomedir(void) | |||
837 | static char *buf = NULL; | 837 | static char *buf = NULL; |
838 | DWORD len = PATH_MAX; | 838 | DWORD len = PATH_MAX; |
839 | HANDLE h; | 839 | HANDLE h; |
840 | DECLARE_PROC_ADDR(BOOL, GetUserProfileDirectoryA, HANDLE, LPSTR, LPDWORD); | ||
840 | 841 | ||
841 | if (!buf) | 842 | if (buf) |
842 | buf = xzalloc(PATH_MAX); | ||
843 | |||
844 | if (buf[0]) | ||
845 | return buf; | 843 | return buf; |
846 | 844 | ||
845 | buf = xzalloc(PATH_MAX); | ||
847 | if ( !OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &h) ) | 846 | if ( !OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &h) ) |
848 | return buf; | 847 | return buf; |
849 | 848 | ||
850 | GetUserProfileDirectory(h, buf, &len); | 849 | if (INIT_PROC_ADDR(userenv.dll, GetUserProfileDirectoryA)) |
850 | GetUserProfileDirectoryA(h, buf, &len); | ||
851 | CloseHandle(h); | 851 | CloseHandle(h); |
852 | return bs_to_slash(buf); | 852 | return bs_to_slash(buf); |
853 | } | 853 | } |
@@ -1027,7 +1027,6 @@ int link(const char *oldpath, const char *newpath) | |||
1027 | LPSECURITY_ATTRIBUTES); | 1027 | LPSECURITY_ATTRIBUTES); |
1028 | 1028 | ||
1029 | if (!INIT_PROC_ADDR(kernel32.dll, CreateHardLinkA)) { | 1029 | if (!INIT_PROC_ADDR(kernel32.dll, CreateHardLinkA)) { |
1030 | errno = ENOSYS; | ||
1031 | return -1; | 1030 | return -1; |
1032 | } | 1031 | } |
1033 | if (!CreateHardLinkA(newpath, oldpath, NULL)) { | 1032 | if (!CreateHardLinkA(newpath, oldpath, NULL)) { |
@@ -1767,3 +1766,19 @@ void make_sparse(int fd, off_t start, off_t end) | |||
1767 | DeviceIoControl(fh, FSCTL_SET_ZERO_DATA, &fzdi, sizeof(fzdi), | 1766 | DeviceIoControl(fh, FSCTL_SET_ZERO_DATA, &fzdi, sizeof(fzdi), |
1768 | NULL, 0, &dwTemp, NULL); | 1767 | NULL, 0, &dwTemp, NULL); |
1769 | } | 1768 | } |
1769 | |||
1770 | void *get_proc_addr(const char *dll, const char *function, | ||
1771 | struct proc_addr *proc) | ||
1772 | { | ||
1773 | /* only do this once */ | ||
1774 | if (!proc->initialized) { | ||
1775 | HANDLE hnd = LoadLibraryExA(dll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); | ||
1776 | if (hnd) | ||
1777 | proc->pfunction = GetProcAddress(hnd, function); | ||
1778 | proc->initialized = 1; | ||
1779 | } | ||
1780 | /* set ENOSYS if DLL or function was not found */ | ||
1781 | if (!proc->pfunction) | ||
1782 | errno = ENOSYS; | ||
1783 | return proc->pfunction; | ||
1784 | } | ||