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
120
121
use crate::co;
use crate::ole::ffi;
use crate::prelude::*;

/// RAII implementation which automatically calls
/// [`CoLockObjectExternal`](https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-colockobjectexternal)
/// to unlock the COM pointer.
pub struct CoLockObjectExternalGuard<'a, T>
	where T: ole_IUnknown,
{
	com_obj: &'a T,
}

impl<'a, T> Drop for CoLockObjectExternalGuard<'a, T>
	where T: ole_IUnknown,
{
	fn drop(&mut self) {
		unsafe {
			ffi::CoLockObjectExternal(self.com_obj.ptr(), 0, 1); // ignore errors
		}
	}
}

impl<'a, T> CoLockObjectExternalGuard<'a, T>
	where T: ole_IUnknown,
{
	/// Constructs the guard by keeping the reference to the COM pointer.
	///
	/// # Safety
	///
	/// Be sure the COM pointer has been locked with a previous call to
	/// [`CoLockObjectExternal`](https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-colockobjectexternal).
	#[must_use]
	pub const unsafe fn new(com_obj: &'a T) -> Self {
		Self { com_obj }
	}
}

//------------------------------------------------------------------------------

/// RAII implementation which automatically calls
/// [`CoTaskMemFree`](https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cotaskmemfree)
/// when the object goes out of scope.
pub struct CoTaskMemFreeGuard {
	pmem: *mut std::ffi::c_void,
	sz: usize,
}

impl Drop for CoTaskMemFreeGuard {
	fn drop(&mut self) {
		if !self.pmem.is_null() {
			unsafe { ffi::CoTaskMemFree(self.pmem); }
		}
	}
}

impl CoTaskMemFreeGuard {
	/// Constructs the guard.
	///
	/// # Safety
	///
	/// Be sure the pointer must be freed with
	/// [`CoTaskMemFree`](https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cotaskmemfree)
	/// at the end of the scope, and the size is correct.
	#[must_use]
	pub const unsafe fn new(pmem: *mut std::ffi::c_void, sz: usize) -> Self {
		Self { pmem, sz }
	}

	/// Ejects the underlying memory pointer and size, leaving null and zero in
	/// their places.
	///
	/// Since the internal memory pointer will be invalidated, the destructor
	/// will not run. It's your responsibility to run it, otherwise you'll cause
	/// a memory leak.
	#[must_use]
	pub fn leak(&mut self) -> (*mut std::ffi::c_void, usize) {
		(
			std::mem::replace(&mut self.pmem, std::ptr::null_mut()),
			std::mem::replace(&mut self.sz, 0),
		)
	}

	pub_fn_mem_block!();
}

//------------------------------------------------------------------------------

/// RAII implementation which automatically calls
/// [`CoUninitialize`](https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-couninitialize)
/// when the object goes out of scope.
pub struct CoUninitializeGuard {
	hr: co::HRESULT,
}

impl Drop for CoUninitializeGuard {
	fn drop(&mut self) {
		unsafe { ffi::CoUninitialize(); }
	}
}

impl CoUninitializeGuard {
	/// Constructs the guard by taking ownership of the code.
	///
	/// # Safety
	///
	/// Be sure you need to call
	/// [`CoUninitialize`](https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-couninitialize)
	/// at the end of scope.
	#[must_use]
	pub const unsafe fn new(hr: co::HRESULT) -> Self {
		Self { hr }
	}

	/// Returns the informational success code returned by
	/// [`CoInitializeEx`](crate::CoInitializeEx).
	#[must_use]
	pub const fn hr(&self) -> co::HRESULT {
		self.hr
	}
}