3 Copyright 1998, 2000, 2001 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
19 #include "perprocess.h"
27 set_dl_error (const char *str)
29 __small_sprintf (_my_tls.locals.dl_buffer, "%s: %E", str);
30 _my_tls.locals.dl_error = 1;
33 /* Look for an executable file given the name and the environment
34 variable to use for searching (eg., PATH); returns the full
35 pathname (static buffer) if found or NULL if not. */
36 inline const char * __stdcall
37 check_path_access (const char *mywinenv, const char *name, path_conv& buf)
39 return find_exec (name, buf, mywinenv, FE_NNF | FE_NATIVE | FE_CWD);
42 /* Search LD_LIBRARY_PATH for dll, if it exists.
43 Return Windows version of given path. */
44 static const char * __stdcall
45 get_full_path_of_dll (const char* str, char *name)
47 int len = strlen (str);
49 /* empty string or too long to be legal win32 pathname? */
50 if (len == 0 || len >= CYG_MAX_PATH - 1)
51 return str; /* Yes. Let caller deal with it. */
55 strcpy (name, str); /* Put it somewhere where we can manipulate it. */
57 /* Add extension if necessary */
58 if (str[len - 1] != '.')
60 /* Add .dll only if no extension provided. */
61 const char *p = strrchr (str, '.');
62 if (!p || strpbrk (p, "\\/"))
63 strcat (name, ".dll");
66 path_conv real_filename;
68 if (isabspath (name) ||
69 (ret = check_path_access ("LD_LIBRARY_PATH=", name, real_filename)) == NULL)
70 real_filename.check (name); /* Convert */
72 if (!real_filename.error)
73 ret = strcpy (name, real_filename);
76 set_errno (real_filename.error);
84 dlopen (const char *name, int)
89 ret = (void *) GetModuleHandle (NULL); /* handle for the current module */
92 char buf[CYG_MAX_PATH];
93 /* handle for the named library */
94 const char *fullpath = get_full_path_of_dll (name, buf);
99 ret = (void *) LoadLibrary (fullpath);
106 set_dl_error ("dlopen");
107 debug_printf ("ret %p", ret);
113 dlsym (void *handle, const char *name)
115 void *ret = (void *) GetProcAddress ((HMODULE) handle, name);
117 set_dl_error ("dlsym");
118 debug_printf ("ret %p", ret);
123 dlclose (void *handle)
126 void *temphandle = (void *) GetModuleHandle (NULL);
127 if (temphandle == handle || FreeLibrary ((HMODULE) handle))
130 set_dl_error ("dlclose");
131 CloseHandle ((HMODULE) temphandle);
139 if (!_my_tls.locals.dl_error)
143 _my_tls.locals.dl_error = 0;
144 res = _my_tls.locals.dl_buffer;