com/
sys.rs

1//! Types for interacting with COM related system APIs
2use core::ffi::c_void;
3
4/// A Windows result code
5pub type HRESULT = i32;
6
7/// Equivalent of the [FAILED macro](https://docs.microsoft.com/en-us/windows/win32/api/winerror/nf-winerror-failed)
8#[allow(non_snake_case)]
9pub fn FAILED(result: HRESULT) -> bool {
10    result < 0
11}
12
13/// BOOL type
14pub type BOOL = i32;
15/// LSTATUS type
16pub type LSTATUS = i32;
17/// HKEY type
18pub type HKEY = *mut c_void;
19
20/// No error
21pub const S_OK: HRESULT = 0;
22/// No error
23pub const NOERROR: HRESULT = 0;
24/// False
25pub const S_FALSE: HRESULT = 1;
26
27/// Argument was invalid
28pub const E_INVALIDARG: HRESULT = -0x7FF8_FFA9;
29/// No interface found
30pub const E_NOINTERFACE: HRESULT = -0x7FFF_BFFE;
31/// Invalid pointer
32pub const E_POINTER: HRESULT = -0x7FFF_BFFD;
33
34/// No aggregation for class
35pub const CLASS_E_NOAGGREGATION: HRESULT = -0x7FFB_FEF0;
36/// Class is not available
37pub const CLASS_E_CLASSNOTAVAILABLE: HRESULT = -0x7FFB_FEEF;
38
39/// No error
40pub const ERROR_SUCCESS: u32 = 0;
41/// Registration error
42pub const SELFREG_E_CLASS: HRESULT = -0x7FFB_FDFF;
43/// A in process server
44pub const CLSCTX_INPROC_SERVER: u32 = 0x1;
45
46/// An single threaded apartment (STA)
47pub const COINIT_APARTMENTTHREADED: u32 = 0x2;
48/// An multi threaded apartment (STA)
49pub const COINIT_MULTITHREADED: u32 = 0x0;
50
51/// A globally unique identifier
52#[allow(missing_docs)]
53#[repr(C)]
54#[derive(Copy, Clone, PartialEq)]
55pub struct GUID {
56    pub data1: u32,
57    pub data2: u16,
58    pub data3: u16,
59    pub data4: [u8; 8],
60}
61
62/// An interface ID
63pub type IID = GUID;
64/// A class ID
65pub type CLSID = GUID;
66
67impl core::fmt::Debug for GUID {
68    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
69        write!(
70            f,
71            "{:08X}-{:04X}-{:04X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
72            self.data1,
73            self.data2,
74            self.data3,
75            self.data4[0],
76            self.data4[1],
77            self.data4[2],
78            self.data4[3],
79            self.data4[4],
80            self.data4[5],
81            self.data4[6],
82            self.data4[7]
83        )
84    }
85}
86impl core::fmt::Display for GUID {
87    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
88        write!(f, "{:?}", self)
89    }
90}
91
92#[cfg(windows)]
93#[link(name = "ole32")]
94#[allow(missing_docs)]
95extern "system" {
96    pub fn CoIncrementMTAUsage(cookie: *mut c_void) -> HRESULT;
97    pub fn RegCreateKeyExA(
98        hKey: HKEY,
99        lpSubKey: *const i8,
100        Reserved: u32,
101        lpClass: *mut u8,
102        dwOptions: u32,
103        samDesired: u32,
104        lpSecurityAttributes: *mut c_void,
105        phkResult: *mut HKEY,
106        lpdwDisposition: *mut u32,
107    ) -> LSTATUS;
108    pub fn GetModuleFileNameA(hModule: *mut c_void, lpFilename: *mut i8, nSize: u32) -> u32;
109    pub fn RegCloseKey(hKey: HKEY) -> LSTATUS;
110    pub fn RegSetValueExA(
111        hKey: HKEY,
112        lpValueName: *const i8,
113        Reserved: u32,
114        dwType: u32,
115        lpData: *const u8,
116        cbData: u32,
117    ) -> LSTATUS;
118    pub fn RegDeleteKeyA(hKey: HKEY, lpSubKey: *const i8) -> LSTATUS;
119    pub fn GetModuleHandleA(lpModuleName: *const i8) -> *mut c_void;
120    pub fn CoInitializeEx(pvReserved: *mut c_void, dwCoInit: u32) -> HRESULT;
121    pub fn CoGetClassObject(
122        rclsid: *const IID,
123        dwClsContext: u32,
124        pvReserved: *mut c_void,
125        riid: *const IID,
126        ppv: *mut *mut c_void,
127    ) -> HRESULT;
128    pub fn CoCreateInstance(
129        rclsid: *const IID,
130        pUnkOuter: *mut c_void,
131        dwClsContext: u32,
132        riid: *const IID,
133        ppv: *mut *mut c_void,
134    ) -> HRESULT;
135    pub fn CoUninitialize();
136}