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