Skip to content

Commit 1f49a35

Browse files
Added nstd_os_windows_shared_lib_add_dir.
1 parent ae834f9 commit 1f49a35

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

include/nstd/os/windows/shared_lib.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef NSTD_OS_WINDOWS_SHARED_LIB_H
22
#define NSTD_OS_WINDOWS_SHARED_LIB_H
3+
#include "../../core/def.h"
34
#include "../../core/optional.h"
45
#include "../../nstd.h"
56

@@ -12,6 +13,22 @@ typedef struct {
1213
/// An optional (possibly null) shared Windows library handle.
1314
NSTDOptional(NSTDWindowsSharedLib) NSTDWindowsOptionalSharedLib;
1415

16+
/// Adds a directory to the system's search path used to load shared libraries.
17+
///
18+
/// # Parameters:
19+
///
20+
/// - `const NSTDChar *path` - A path to a directory to search when looking for DLLs. Pass null to
21+
/// restore the default search order.
22+
///
23+
/// # Returns
24+
///
25+
/// `NSTDErrorCode errc` - Nonzero on error.
26+
///
27+
/// # Safety
28+
///
29+
/// See <https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setdlldirectorya>.
30+
NSTDAPI NSTDErrorCode nstd_os_windows_shared_lib_add_dir(const NSTDChar *path);
31+
1532
/// Loads a shared library/module by name.
1633
///
1734
/// # Parameters:

src/os/windows/shared_lib.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//! 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};
2+
use crate::{
3+
core::{def::NSTDErrorCode, optional::NSTDOptional},
4+
NSTDAny, NSTDAnyMut, NSTDChar, NSTDInt,
5+
};
6+
use windows_sys::Win32::System::LibraryLoader::{
7+
FreeLibrary, GetProcAddress, LoadLibraryA, SetDllDirectoryA,
8+
};
49

510
/// A handle to a loaded library.
611
#[repr(C)]
@@ -20,6 +25,28 @@ impl Drop for NSTDWindowsSharedLib {
2025
/// An optional (possibly null) shared Windows library handle.
2126
pub type NSTDWindowsOptionalSharedLib = NSTDOptional<NSTDWindowsSharedLib>;
2227

28+
/// Adds a directory to the system's search path used to load shared libraries.
29+
///
30+
/// # Parameters:
31+
///
32+
/// - `const NSTDChar *path` - A path to a directory to search when looking for DLLs. Pass null to
33+
/// restore the default search order.
34+
///
35+
/// # Returns
36+
///
37+
/// `NSTDErrorCode errc` - Nonzero on error.
38+
///
39+
/// # Safety
40+
///
41+
/// See <https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setdlldirectorya>.
42+
#[inline]
43+
#[cfg_attr(feature = "clib", no_mangle)]
44+
pub unsafe extern "C" fn nstd_os_windows_shared_lib_add_dir(
45+
path: *const NSTDChar,
46+
) -> NSTDErrorCode {
47+
(SetDllDirectoryA(path.cast()) == 0).into()
48+
}
49+
2350
/// Loads a shared library/module by name.
2451
///
2552
/// # Parameters:

0 commit comments

Comments
 (0)