-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_object.c
118 lines (100 loc) · 2.04 KB
/
shared_object.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 - 2022 TheVice
*
*/
#if !defined(_WIN32)
#define _POSIX_SOURCE 1
#endif
#include "shared_object.h"
/*#ifndef NDEBUG
#include "common.h"
#include "echo.h"
#include "text_encoding.h"
#endif*/
#include <stddef.h>
#if defined(_WIN32)
#include "buffer.h"
#include "file_system.h"
#include <wchar.h>
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#if defined(_WIN32)
void* shared_object_load_wchar_t(const wchar_t* path)
{
return (void*)LoadLibraryExW(path, NULL, 0);
}
#endif
void* shared_object_load(const uint8_t* path)
{
if (NULL == path)
{
return NULL;
}
#if defined(_WIN32)
uint8_t pathW_buffer[BUFFER_SIZE_OF];
void* pathW = (void*)pathW_buffer;
if (!buffer_init(pathW, BUFFER_SIZE_OF))
{
return NULL;
}
if (!file_system_path_to_pathW(path, pathW))
{
buffer_release(pathW);
return NULL;
}
void* object = shared_object_load_wchar_t(buffer_wchar_t_data(pathW, 0));
buffer_release(pathW);
/**/
return object;
#else
/*
#ifndef NDEBUG
uint8_t verbose = 1;
void* object = dlopen((const char*)path, RTLD_NOW);
if (verbose && NULL == object)
{
const uint8_t* error_str = (const uint8_t*)dlerror();
const uint8_t message_length = (uint8_t)common_count_bytes_until(error_str, 0);
echo(0, UTF8, NULL, Error, error_str, message_length, 1, 0);
}
return object;
#else*/
return dlopen((const char*)path, RTLD_NOW);
/*
#endif
*/
#endif
}
void* shared_object_get_procedure_address(void* object, const uint8_t* procedure_name)
{
if (NULL == object ||
NULL == procedure_name)
{
return NULL;
}
#if defined(_WIN32)
#if defined(_MSC_VER) && (_MSC_VER < 1910)
#pragma warning(suppress: 4054)
return (void*)GetProcAddress((HMODULE)object, (const char*)procedure_name);
#else
return (void*)GetProcAddress((HMODULE)object, (const char*)procedure_name);
#endif
#else
return dlsym(object, (const char*)procedure_name);
#endif
}
void shared_object_unload(void* object)
{
if (NULL != object)
{
#if defined(_WIN32)
/*TODO:*/FreeLibrary((HMODULE)object);
#else
dlclose(object);
#endif
}
}