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
119
mod cache;
#[doc(hidden)]
pub mod macro_internal;
mod platform;

#[doc = include_str!("../macro.md")]
pub use windows_dll_codegen::dll;

pub use platform::flags;

use cache::DllCache;
use platform::{LPCSTR, LPCWSTR};
use core::marker::PhantomData;

pub trait WindowsDll: Sized + 'static {
    const LEN: usize;
    const LIB: &'static str;
    const LIB_LPCWSTR: LPCWSTR;
    const FLAGS: flags::LOAD_LIBRARY_FLAGS;

    unsafe fn cache() -> &'static DllCache<Self>;
    unsafe fn exists() -> bool {
        Self::cache().lib_exists()
    }
    unsafe fn free() -> bool {
        let library = Self::cache();
        library.free_lib()
    }
}

pub trait WindowsDllProc: Sized {
    type Dll: WindowsDll;
    type Sig: Copy;
    const CACHE_INDEX: usize;
    const PROC: Proc;
    const PROC_LPCSTR: LPCSTR;

    unsafe fn proc() -> Result<Self::Sig, Error<Self>>;
    unsafe fn exists() -> bool {
        Self::proc().is_ok()
    }
}

#[derive(Debug, Clone)]
pub enum Proc {
    Name(&'static str),
    Ordinal(u16),
}

impl core::fmt::Display for Proc {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Name(name) => name.fmt(f),
            Self::Ordinal(ordinal) => ordinal.fmt(f),
        }
    }
}

#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum ErrorKind {
    Lib,
    Proc,
}

pub struct Error<D> {
    pub kind: ErrorKind,
    _dll: PhantomData<D>,
}
impl<D> Error<D> {
    pub fn lib() -> Self {
        Self {
            kind: ErrorKind::Lib,
            _dll: PhantomData,
        }
    }
    pub fn proc() -> Self {
        Self {
            kind: ErrorKind::Proc,
            _dll: PhantomData,
        }
    }
}

impl<D> Copy for Error<D> {}
impl<D> Clone for Error<D> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<D> From<ErrorKind> for Error<D> {
    fn from(kind: ErrorKind) -> Self {
        Self {
            kind,
            _dll: PhantomData,
        }
    }
}

impl<D: WindowsDllProc> std::error::Error for Error<D> {}

impl<D: WindowsDllProc> core::fmt::Display for Error<D> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match &self.kind {
            ErrorKind::Lib => write!(f, "Could not load {}", D::Dll::LIB),
            ErrorKind::Proc => write!(f, "Could not load {}#{}", D::Dll::LIB, D::PROC),
        }
    }
}
impl<D: WindowsDllProc> core::fmt::Debug for Error<D> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Error")
            .field("kind", &self.kind)
            .field("lib", &D::Dll::LIB)
            .field("proc", &D::PROC)
            .finish()
    }
}