1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_arch_dso.h"
18 #include "apr_strings.h"
19 #include "apr_private.h"
20 #include "apr_arch_file_io.h"
21 #include "apr_arch_utf8.h"
25 APR_DECLARE(apr_status_t) apr_os_dso_handle_put(apr_dso_handle_t **aprdso,
26 apr_os_dso_handle_t osdso,
29 *aprdso = apr_pcalloc(pool, sizeof **aprdso);
30 (*aprdso)->handle = osdso;
31 (*aprdso)->cont = pool;
35 APR_DECLARE(apr_status_t) apr_os_dso_handle_get(apr_os_dso_handle_t *osdso,
36 apr_dso_handle_t *aprdso)
38 *osdso = aprdso->handle;
42 static apr_status_t dso_cleanup(void *thedso)
44 apr_dso_handle_t *dso = thedso;
46 if (dso->handle != NULL && !FreeLibrary(dso->handle)) {
47 return apr_get_os_error();
54 APR_DECLARE(apr_status_t) apr_dso_load(struct apr_dso_handle_t **res_handle,
55 const char *path, apr_pool_t *ctx)
63 #if APR_HAS_UNICODE_FS
66 apr_wchar_t wpath[APR_PATH_MAX];
67 if ((rv = utf8_to_unicode_path(wpath, sizeof(wpath)
68 / sizeof(apr_wchar_t), path))
70 *res_handle = apr_pcalloc(ctx, sizeof(**res_handle));
71 return ((*res_handle)->load_error = rv);
73 /* Prevent ugly popups from killing our app */
75 em = SetErrorMode(SEM_FAILCRITICALERRORS);
77 os_handle = LoadLibraryExW(wpath, NULL, 0);
79 os_handle = LoadLibraryExW(wpath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
81 rv = apr_get_os_error();
86 #endif /* APR_HAS_UNICODE_FS */
90 char fspec[APR_PATH_MAX], *p = fspec;
91 /* Must convert path from / to \ notation.
92 * Per PR2555, the LoadLibraryEx function is very picky about slashes.
93 * Debugging on NT 4 SP 6a reveals First Chance Exception within NTDLL.
94 * LoadLibrary in the MS PSDK also reveals that it -explicitly- states
95 * that backslashes must be used for the LoadLibrary family of calls.
97 apr_cpystrn(fspec, path, sizeof(fspec));
98 while ((p = strchr(p, '/')) != NULL)
101 /* Prevent ugly popups from killing our app */
102 em = SetErrorMode(SEM_FAILCRITICALERRORS);
103 os_handle = LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
105 os_handle = LoadLibraryEx(path, NULL, 0);
107 rv = apr_get_os_error();
114 *res_handle = apr_pcalloc(ctx, sizeof(**res_handle));
115 (*res_handle)->cont = ctx;
118 return ((*res_handle)->load_error = rv);
121 (*res_handle)->handle = (void*)os_handle;
122 (*res_handle)->load_error = APR_SUCCESS;
124 apr_pool_cleanup_register(ctx, *res_handle, dso_cleanup, apr_pool_cleanup_null);
129 APR_DECLARE(apr_status_t) apr_dso_unload(struct apr_dso_handle_t *handle)
131 return apr_pool_cleanup_run(handle->cont, handle, dso_cleanup);
134 APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym,
135 struct apr_dso_handle_t *handle,
139 apr_size_t symlen = strlen(symname) + 1;
140 apr_size_t wsymlen = 256;
141 apr_wchar_t wsymname[256];
144 rv = apr_conv_utf8_to_ucs2(wsymname, &wsymlen, symname, &symlen);
145 if (rv != APR_SUCCESS) {
149 return APR_ENAMETOOLONG;
152 *ressym = (apr_dso_handle_sym_t)GetProcAddressW(handle->handle, wsymname);
154 *ressym = (apr_dso_handle_sym_t)GetProcAddress(handle->handle, symname);
157 return apr_get_os_error();
162 APR_DECLARE(const char *) apr_dso_error(apr_dso_handle_t *dso, char *buf, apr_size_t bufsize)
164 return apr_strerror(dso->load_error, buf, bufsize);