Skip to content

Commit 7a365ec

Browse files
Merge branch 'nstd.os.windows.shared_lib'
2 parents e5c79eb + 2515951 commit 7a365ec

File tree

7 files changed

+201
-2
lines changed

7 files changed

+201
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ nstd_os_unix_shared_lib = ["libc", "nstd_core", "nstd_os"]
3737
nstd_os_windows_alloc = [
3838
"nstd_core", "nstd_os", "windows-sys/Win32_Foundation", "windows-sys/Win32_System_Memory"
3939
]
40+
nstd_os_windows_shared_lib = [
41+
"nstd_core", "nstd_os", "windows-sys/Win32_Foundation", "windows-sys/Win32_System_LibraryLoader"
42+
]
4043
nstd_shared_lib = ["libloading", "nstd_core", "std"]
4144
nstd_shared_ptr = ["nstd_alloc", "nstd_core"]
4245
nstd_string = ["nstd_alloc", "nstd_core", "nstd_vec"]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ int main()
5454
- `windows` - OS support for Windows.
5555
- `alloc` - Low level memory allocation for Windows.
5656
- `heap` - Process heap management for Windows.
57+
- `shared_lib` - Shared library/module access for Windows.
5758
- `shared_lib` - Access symbols from loaded shared libraries.
5859
- `shared_ptr` - A reference counting smart pointer.
5960
- `string` - Dynamically sized UTF-8 encoded byte string.

include/nstd/os/windows.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#ifndef NSTD_OS_WINDOWS_H
22
#define NSTD_OS_WINDOWS_H
33
#include "windows/alloc.h"
4+
#include "windows/shared_lib.h"
45
#endif

include/nstd/os/windows/shared_lib.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#ifndef NSTD_OS_WINDOWS_SHARED_LIB_H
2+
#define NSTD_OS_WINDOWS_SHARED_LIB_H
3+
#include "../../core/optional.h"
4+
#include "../../nstd.h"
5+
6+
/// A handle to a loaded library.
7+
typedef struct {
8+
/// A raw handle to the module.
9+
NSTDInt handle;
10+
} NSTDWindowsSharedLib;
11+
12+
/// An optional (possibly null) shared Windows library handle.
13+
NSTDOptional(NSTDWindowsSharedLib) NSTDWindowsOptionalSharedLib;
14+
15+
/// Loads a shared library/module by name.
16+
///
17+
/// # Parameters:
18+
///
19+
/// - `const NSTDChar *name` - The name of the module to load.
20+
///
21+
/// # Returns
22+
///
23+
/// `NSTDWindowsOptionalSharedLib lib` - A handle to the shared library.
24+
///
25+
/// # Safety
26+
///
27+
/// See
28+
/// <https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya>.
29+
NSTDAPI NSTDWindowsOptionalSharedLib nstd_os_windows_shared_lib_load(const NSTDChar *name);
30+
31+
/// Gets a pointer to a function or static variable in a dynamically loaded library by symbol name.
32+
///
33+
/// # Parameters
34+
///
35+
/// - `const NSTDWindowsSharedLib *lib` - The loaded library.
36+
///
37+
/// - `const NSTDChar *symbol` - The name of the function or variable to get a pointer to.
38+
///
39+
/// # Returns
40+
///
41+
/// `NSTDAny ptr` - A pointer to the function or variable.
42+
///
43+
/// # Safety
44+
///
45+
/// See <https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress>.
46+
NSTDAPI NSTDAny nstd_os_windows_shared_lib_get(const NSTDWindowsSharedLib *lib,
47+
const NSTDChar *symbol);
48+
49+
/// Gets a mutable pointer to a function or static variable in a dynamically loaded library by
50+
/// symbol name.
51+
///
52+
/// # Parameters
53+
///
54+
/// - `NSTDWindowsSharedLib *lib` - The loaded library.
55+
///
56+
/// - `const NSTDChar *symbol` - The name of the function or variable to get a pointer to.
57+
///
58+
/// # Returns
59+
///
60+
/// `NSTDAnyMut ptr` - A pointer to the function or variable.
61+
///
62+
/// # Safety
63+
///
64+
/// See <https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress>.
65+
NSTDAPI NSTDAnyMut nstd_os_windows_shared_lib_get_mut(NSTDWindowsSharedLib *lib,
66+
const NSTDChar *symbol);
67+
68+
/// Unloads and frees a dynamically loaded shared library.
69+
///
70+
/// # Parameters:
71+
///
72+
/// - `NSTDWindowsSharedLib lib` - The library handle.
73+
///
74+
/// # Safety
75+
///
76+
/// See
77+
/// <https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-freelibrary>.
78+
NSTDAPI void nstd_os_windows_shared_lib_free(NSTDWindowsSharedLib lib);
79+
80+
#endif

src/os/windows.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
#[cfg(feature = "nstd_os_windows_alloc")]
33
#[cfg_attr(doc_cfg, doc(cfg(feature = "nstd_os_windows_alloc")))]
44
pub mod alloc;
5+
#[cfg(feature = "nstd_os_windows_shared_lib")]
6+
#[cfg_attr(doc_cfg, doc(cfg(feature = "nstd_os_windows_shared_lib")))]
7+
pub mod shared_lib;

src/os/windows/shared_lib.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//! Shared library/module access for Windows.
2+
use crate::{core::optional::NSTDOptional, NSTDAny, NSTDAnyMut, NSTDChar, NSTDInt};
3+
use windows_sys::Win32::System::LibraryLoader::{FreeLibrary, GetProcAddress, LoadLibraryA};
4+
5+
/// A handle to a loaded library.
6+
#[repr(C)]
7+
pub struct NSTDWindowsSharedLib {
8+
/// A raw handle to the module.
9+
handle: NSTDInt,
10+
}
11+
impl Drop for NSTDWindowsSharedLib {
12+
/// [NSTDWindowsSharedLib]'s destructor.
13+
#[inline]
14+
fn drop(&mut self) {
15+
// SAFETY: `handle` is non-null.
16+
unsafe { FreeLibrary(self.handle) };
17+
}
18+
}
19+
20+
/// An optional (possibly null) shared Windows library handle.
21+
pub type NSTDWindowsOptionalSharedLib = NSTDOptional<NSTDWindowsSharedLib>;
22+
23+
/// Loads a shared library/module by name.
24+
///
25+
/// # Parameters:
26+
///
27+
/// - `const NSTDChar *name` - The name of the module to load.
28+
///
29+
/// # Returns
30+
///
31+
/// `NSTDWindowsOptionalSharedLib lib` - A handle to the shared library.
32+
///
33+
/// # Safety
34+
///
35+
/// See
36+
/// <https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya>.
37+
#[inline]
38+
#[cfg_attr(feature = "clib", no_mangle)]
39+
pub unsafe extern "C" fn nstd_os_windows_shared_lib_load(
40+
name: *const NSTDChar,
41+
) -> NSTDWindowsOptionalSharedLib {
42+
match LoadLibraryA(name.cast()) {
43+
0 => NSTDOptional::None,
44+
handle => NSTDOptional::Some(NSTDWindowsSharedLib { handle }),
45+
}
46+
}
47+
48+
/// Gets a pointer to a function or static variable in a dynamically loaded library by symbol name.
49+
///
50+
/// # Parameters
51+
///
52+
/// - `const NSTDWindowsSharedLib *lib` - The loaded library.
53+
///
54+
/// - `const NSTDChar *symbol` - The name of the function or variable to get a pointer to.
55+
///
56+
/// # Returns
57+
///
58+
/// `NSTDAny ptr` - A pointer to the function or variable.
59+
///
60+
/// # Safety
61+
///
62+
/// See <https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress>.
63+
#[inline]
64+
#[cfg_attr(feature = "clib", no_mangle)]
65+
pub unsafe extern "C" fn nstd_os_windows_shared_lib_get(
66+
lib: &NSTDWindowsSharedLib,
67+
symbol: *const NSTDChar,
68+
) -> NSTDAny {
69+
core::mem::transmute(GetProcAddress(lib.handle, symbol.cast()))
70+
}
71+
72+
/// Gets a mutable pointer to a function or static variable in a dynamically loaded library by
73+
/// symbol name.
74+
///
75+
/// # Parameters
76+
///
77+
/// - `NSTDWindowsSharedLib *lib` - The loaded library.
78+
///
79+
/// - `const NSTDChar *symbol` - The name of the function or variable to get a pointer to.
80+
///
81+
/// # Returns
82+
///
83+
/// `NSTDAnyMut ptr` - A pointer to the function or variable.
84+
///
85+
/// # Safety
86+
///
87+
/// See <https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress>.
88+
#[inline]
89+
#[cfg_attr(feature = "clib", no_mangle)]
90+
pub unsafe extern "C" fn nstd_os_windows_shared_lib_get_mut(
91+
lib: &mut NSTDWindowsSharedLib,
92+
symbol: *const NSTDChar,
93+
) -> NSTDAnyMut {
94+
core::mem::transmute(GetProcAddress(lib.handle, symbol.cast()))
95+
}
96+
97+
/// Unloads and frees a dynamically loaded shared library.
98+
///
99+
/// # Parameters:
100+
///
101+
/// - `NSTDWindowsSharedLib lib` - The library handle.
102+
///
103+
/// # Safety
104+
///
105+
/// See
106+
/// <https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-freelibrary>.
107+
#[inline]
108+
#[cfg_attr(feature = "clib", no_mangle)]
109+
#[allow(unused_variables)]
110+
pub unsafe extern "C" fn nstd_os_windows_shared_lib_free(lib: NSTDWindowsSharedLib) {}

test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
22

33
FEATURES = ("nstd_alloc", "nstd_core", "nstd_cstring", "nstd_fs", "nstd_heap_ptr", "nstd_io",
4-
"nstd_math", "nstd_os_unix_shared_lib", "nstd_os_windows_alloc", "nstd_shared_lib",
5-
"nstd_shared_ptr", "nstd_string", "nstd_vec")
4+
"nstd_math", "nstd_os_unix_shared_lib", "nstd_os_windows_alloc",
5+
"nstd_os_windows_shared_lib", "nstd_shared_lib", "nstd_shared_ptr", "nstd_string",
6+
"nstd_vec")
67

78
TARGETS = ("x86_64-pc-windows-msvc", "x86_64-apple-darwin",
89
"x86_64-unknown-linux-gnu", "x86_64-apple-ios", "x86_64-linux-android")

0 commit comments

Comments
 (0)