Debian Codegraph @

crt/aws-c-io/source/windows/shared_library.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 // clang-format off
7 #include <windows.h>
8 #include <libloaderapi.h>
9 // clang-format on
10
11 #include <aws/io/shared_library.h>
12
13 #include <aws/io/logging.h>
14
15 static const char *s_null = "<NULL>";
16 static const char *s_unknown_error = "<Unknown>";
17
18 int aws_shared_library_init(struct aws_shared_library *library, const char *library_path) {
19 AWS_ZERO_STRUCT(*library);
20
21 library->library_handle = LoadLibrary(library_path);
22 if (library->library_handle == NULL) {
23 DWORD ec = GetLastError();
24
25 AWS_LOGF_ERROR(
26 AWS_LS_IO_SHARED_LIBRARY,
27 "id=%p: Failed to load shared library with path \"%s\" with Windows error code: %ul",
28 (void *)library,
29 library_path ? library_path : s_null,
30 ec);
31 return aws_raise_error(AWS_IO_SHARED_LIBRARY_LOAD_FAILURE);
32 }
33
34 return AWS_OP_SUCCESS;
35 }
36
37 void aws_shared_library_clean_up(struct aws_shared_library *library) {
38 if (library && library->library_handle) {
39 FreeLibrary((HMODULE)library->library_handle);
40 library->library_handle = NULL;
41 }
42 }
43
44 int aws_shared_library_find_function(
45 struct aws_shared_library *library,
46 const char *symbol_name,
47 aws_generic_function *function_address) {
48 if (library == NULL || library->library_handle == NULL) {
49 return aws_raise_error(AWS_IO_SHARED_LIBRARY_FIND_SYMBOL_FAILURE);
50 }
51
52 *function_address = (aws_generic_function)GetProcAddress((HMODULE)library->library_handle, symbol_name);
53 if (*function_address == NULL) {
54 DWORD ec = GetLastError();
55 AWS_LOGF_ERROR(
56 AWS_LS_IO_SHARED_LIBRARY,
57 "id=%p: Failed to find shared library symbol \"%s\" with error code: %ul",
58 (void *)library,
59 symbol_name ? symbol_name : s_null,
60 ec);
61 return aws_raise_error(AWS_IO_SHARED_LIBRARY_FIND_SYMBOL_FAILURE);
62 }
63
64 return AWS_OP_SUCCESS;
65 }