winsafe\oleaut/
funcs.rs

1#![allow(non_snake_case)]
2
3use crate::co;
4use crate::decl::*;
5use crate::guard::*;
6use crate::kernel::privs::*;
7use crate::ole::privs::*;
8use crate::oleaut::ffi;
9use crate::prelude::*;
10
11/// [`OleLoadPicture`](https://learn.microsoft.com/en-us/windows/win32/api/olectl/nf-olectl-oleloadpicture)
12/// function.
13///
14/// # Examples
15///
16/// Parsing an image from raw data:
17///
18/// ```no_run
19/// use winsafe::{self as w, prelude::*};
20///
21/// let stream: w::IStream; // initialized somewhere
22/// # let stream = unsafe { w::IStream::null() };
23///
24/// let picture = w::OleLoadPicture(&stream, None, true)?;
25/// # w::HrResult::Ok(())
26/// ```
27///
28/// # Related functions
29///
30/// * [`OleLoadPicturePath`](crate::OleLoadPicturePath)
31#[must_use]
32pub fn OleLoadPicture(
33	stream: &impl ole_IStream,
34	size: Option<u32>,
35	keep_original_format: bool,
36) -> HrResult<IPicture> {
37	let mut queried = unsafe { IPicture::null() };
38	ok_to_hrresult(unsafe {
39		ffi::OleLoadPicture(
40			stream.ptr() as _,
41			size.unwrap_or_default() as _,
42			!keep_original_format as _, // note: reversed
43			pcvoid(&IPicture::IID),
44			queried.as_mut(),
45		)
46	})
47	.map(|_| queried)
48}
49
50/// [`OleLoadPicturePath`](https://learn.microsoft.com/en-us/windows/win32/api/olectl/nf-olectl-oleloadpicturepath)
51/// function.
52///
53/// The picture must be in BMP (bitmap), JPEG, WMF (metafile), ICO (icon), or
54/// GIF format.
55///
56/// # Related functions
57///
58/// * [`OleLoadPicture`](crate::OleLoadPicture)
59#[must_use]
60pub fn OleLoadPicturePath(path: &str, transparent_color: Option<COLORREF>) -> HrResult<IPicture> {
61	let mut queried = unsafe { IPicture::null() };
62	ok_to_hrresult(unsafe {
63		ffi::OleLoadPicturePath(
64			WString::from_str(path).as_ptr(),
65			std::ptr::null_mut(),
66			0,
67			transparent_color.map_or(0, |c| c.into()),
68			pcvoid(&IPicture::IID),
69			queried.as_mut(),
70		)
71	})
72	.map(|_| queried)
73}
74
75/// [`PSGetNameFromPropertyKey`](https://learn.microsoft.com/en-us/windows/win32/api/propsys/nf-propsys-psgetnamefrompropertykey)
76/// function.
77#[must_use]
78pub fn PSGetNameFromPropertyKey(prop_key: &co::PKEY) -> HrResult<String> {
79	let mut pstr = std::ptr::null_mut::<u16>();
80	ok_to_hrresult(unsafe { ffi::PSGetNameFromPropertyKey(pcvoid(prop_key), &mut pstr) }).map(
81		|_| {
82			let name = unsafe { WString::from_wchars_nullt(pstr) };
83			let _ = unsafe { CoTaskMemFreeGuard::new(pstr as _, 0) };
84			name.to_string()
85		},
86	)
87}
88
89/// [`SystemTimeToVariantTime`](https://learn.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-systemtimetovarianttime)
90/// function.
91///
92/// Note that this function resolves the time to one second; milliseconds are
93/// ignored.
94///
95/// # Related functions
96///
97/// * [`VariantTimeToSystemTime`](crate::VariantTimeToSystemTime)
98#[must_use]
99pub fn SystemTimeToVariantTime(st: &SYSTEMTIME) -> SysResult<f64> {
100	let mut double = f64::default();
101	bool_to_invalidparm(unsafe { ffi::SystemTimeToVariantTime(pcvoid(st), &mut double) })
102		.map(|_| double)
103}
104
105/// [`VariantTimeToSystemTime`](https://learn.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-varianttimetosystemtime)
106/// function.
107///
108/// # Related functions
109///
110/// * [`SystemTimeToVariantTime`](crate::SystemTimeToVariantTime)
111#[must_use]
112pub fn VariantTimeToSystemTime(var_time: f64) -> SysResult<SYSTEMTIME> {
113	let mut st = SYSTEMTIME::default();
114	bool_to_invalidparm(unsafe { ffi::VariantTimeToSystemTime(var_time, pvoid(&mut st)) })
115		.map(|_| st)
116}