diff --git a/crates/libs/windows/src/extensions/Win32/System.rs b/crates/libs/windows/src/extensions/Win32/System.rs index 099876e9ae..330942f0a3 100644 --- a/crates/libs/windows/src/extensions/Win32/System.rs +++ b/crates/libs/windows/src/extensions/Win32/System.rs @@ -1,4 +1,6 @@ #[cfg(feature = "Win32_System_Rpc")] mod Rpc; +#[cfg(all(feature = "Win32_System_Com_StructuredStorage", feature = "Win32_System_Variant"))] +mod StructuredStorage; #[cfg(all(feature = "Win32_System_Variant", feature = "Win32_System_Com", feature = "Win32_System_Ole"))] mod Variant; diff --git a/crates/libs/windows/src/extensions/Win32/System/Com.rs b/crates/libs/windows/src/extensions/Win32/System/Com.rs deleted file mode 100644 index 20ce5d02f4..0000000000 --- a/crates/libs/windows/src/extensions/Win32/System/Com.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod IDispatch; diff --git a/crates/libs/windows/src/extensions/Win32/System/StructuredStorage.rs b/crates/libs/windows/src/extensions/Win32/System/StructuredStorage.rs new file mode 100644 index 0000000000..9c6c3711a6 --- /dev/null +++ b/crates/libs/windows/src/extensions/Win32/System/StructuredStorage.rs @@ -0,0 +1,265 @@ +use crate::core::*; +use crate::Win32::Foundation::*; +use crate::Win32::System::Com::StructuredStorage::*; +use crate::Win32::System::Com::*; +use crate::Win32::System::Variant::*; +use core::mem::*; + +macro_rules! variant_from_value { + ($from:ident, $vt:ident, $field:ident, $value:expr) => { + impl From<$from> for PROPVARIANT { + fn from(value: $from) -> Self { + Self { + Anonymous: PROPVARIANT_0 { + Anonymous: ManuallyDrop::new(PROPVARIANT_0_0 { vt: $vt, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: PROPVARIANT_0_0_0 { $field: $value(value) } }), + }, + } + } + } + }; +} + +impl Clone for PROPVARIANT { + fn clone(&self) -> Self { + unsafe { + let mut value = Self::default(); + _ = PropVariantCopy(&mut value, self); + value + } + } +} + +impl Drop for PROPVARIANT { + fn drop(&mut self) { + unsafe { _ = PropVariantClear(self) }; + } +} + +impl PROPVARIANT { + pub fn vt(&self) -> VARENUM { + unsafe { self.Anonymous.Anonymous.vt } + } + + pub fn is_empty(&self) -> bool { + self.vt() == VT_EMPTY + } +} + +impl core::fmt::Debug for PROPVARIANT { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + let mut debug = f.debug_struct("PROPVARIANT"); + debug.field("type", &unsafe { self.Anonymous.Anonymous.vt }); + + if let Ok(value) = BSTR::try_from(self) { + debug.field("value", &value); + } + + debug.finish() + } +} + +impl core::fmt::Display for PROPVARIANT { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + core::write!(f, "{}", BSTR::try_from(self).unwrap_or_default()) + } +} + +impl PartialEq for PROPVARIANT { + fn eq(&self, other: &Self) -> bool { + unsafe { + if self.Anonymous.Anonymous.vt != other.Anonymous.Anonymous.vt { + return false; + } + + PropVariantCompareEx(self, other, PVCU_DEFAULT, PVCF_DEFAULT) == 0 + } + } +} + +impl Eq for PROPVARIANT {} + +impl TryFrom<&VARIANT> for PROPVARIANT { + type Error = Error; + fn try_from(from: &VARIANT) -> Result { + unsafe { VariantToPropVariant(from) } + } +} + +// VT_UNKNOWN + +variant_from_value!(IUnknown, VT_UNKNOWN, punkVal, |v: IUnknown| ManuallyDrop::new(Some(v))); + +impl TryFrom<&PROPVARIANT> for IUnknown { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { + if from.Anonymous.Anonymous.vt == VT_UNKNOWN && !from.Anonymous.Anonymous.Anonymous.punkVal.is_none() { + let unknown: &IUnknown = transmute(&from.Anonymous.Anonymous.Anonymous.punkVal); + Ok(unknown.clone()) + } else { + Err(Error::from_hresult(TYPE_E_TYPEMISMATCH)) + } + } + } +} + +// VT_BSTR + +variant_from_value!(BSTR, VT_BSTR, bstrVal, |v: BSTR| ManuallyDrop::new(v)); + +impl From<&str> for PROPVARIANT { + fn from(value: &str) -> Self { + BSTR::from(value).into() + } +} + +impl TryFrom<&PROPVARIANT> for BSTR { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { PropVariantToBSTR(from) } + } +} + +// VT_BOOL + +variant_from_value!(bool, VT_BOOL, boolVal, |v: bool| VARIANT_BOOL(if v { -1 } else { 0 })); + +impl TryFrom<&PROPVARIANT> for bool { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { PropVariantToBoolean(from) }.map(|ok| ok.0 != 0) + } +} + +// VT_UI1 + +variant_from_value!(u8, VT_UI1, bVal, |v: u8| v); + +// VT_I1 + +variant_from_value!(i8, VT_I1, cVal, |v: i8| v); + +// VT_UI2 + +variant_from_value!(u16, VT_UI2, uiVal, |v: u16| v); + +impl TryFrom<&PROPVARIANT> for u16 { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { PropVariantToUInt16(from) } + } +} + +// VT_I2 + +variant_from_value!(i16, VT_I2, iVal, |v: i16| v); + +impl TryFrom<&PROPVARIANT> for i16 { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { PropVariantToInt16(from) } + } +} + +// VT_UI4 + +variant_from_value!(u32, VT_UI4, ulVal, |v: u32| v); + +impl TryFrom<&PROPVARIANT> for u32 { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { PropVariantToUInt32(from) } + } +} + +// VT_I4 + +variant_from_value!(i32, VT_I4, lVal, |v: i32| v); + +impl TryFrom<&PROPVARIANT> for i32 { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { PropVariantToInt32(from) } + } +} + +// VT_UI8 + +impl From for PROPVARIANT { + fn from(value: u64) -> Self { + Self { + Anonymous: PROPVARIANT_0 { + Anonymous: ManuallyDrop::new(PROPVARIANT_0_0 { vt: VT_UI8, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: PROPVARIANT_0_0_0 { uhVal: value } }), + }, + } + } +} + +impl TryFrom<&PROPVARIANT> for u64 { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { PropVariantToUInt64(from) } + } +} + +// VT_I8 + +impl From for PROPVARIANT { + fn from(value: i64) -> Self { + Self { + Anonymous: PROPVARIANT_0 { + Anonymous: ManuallyDrop::new(PROPVARIANT_0_0 { vt: VT_I8, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: PROPVARIANT_0_0_0 { hVal: value } }), + }, + } + } +} + +impl TryFrom<&PROPVARIANT> for i64 { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { PropVariantToInt64(from) } + } +} + +// VT_R4 + +variant_from_value!(f32, VT_R4, fltVal, |v: f32| v); + +// VT_R8 + +variant_from_value!(f64, VT_R8, dblVal, |v: f64| v); + +impl TryFrom<&PROPVARIANT> for f64 { + type Error = Error; + fn try_from(from: &PROPVARIANT) -> Result { + unsafe { PropVariantToDouble(from) } + } +} + +// VT_DISPATCH + +impl From for PROPVARIANT { + fn from(value: IDispatch) -> Self { + unsafe { + Self { + Anonymous: PROPVARIANT_0 { + Anonymous: ManuallyDrop::new(PROPVARIANT_0_0 { vt: VT_DISPATCH, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: PROPVARIANT_0_0_0 { pdispVal: transmute(value) } }), + }, + } + } + } +} + +impl TryFrom<&PROPVARIANT> for IDispatch { + type Error = windows_core::Error; + fn try_from(from: &PROPVARIANT) -> windows_core::Result { + unsafe { + if from.Anonymous.Anonymous.vt == VT_DISPATCH && !from.Anonymous.Anonymous.Anonymous.pdispVal.is_none() { + let dispatch: &IDispatch = transmute(&from.Anonymous.Anonymous.Anonymous.pdispVal); + Ok(dispatch.clone()) + } else { + Err(windows_core::Error::from_hresult(TYPE_E_TYPEMISMATCH)) + } + } + } +} diff --git a/crates/libs/windows/src/extensions/Win32/System/Variant.rs b/crates/libs/windows/src/extensions/Win32/System/Variant.rs index ce3ebfa9ac..d8000fc1d8 100644 --- a/crates/libs/windows/src/extensions/Win32/System/Variant.rs +++ b/crates/libs/windows/src/extensions/Win32/System/Variant.rs @@ -17,16 +17,6 @@ macro_rules! variant_from_value { } } } - #[cfg(feature = "Win32_System_Com_StructuredStorage")] - impl From<$from> for PROPVARIANT { - fn from(value: $from) -> Self { - Self { - Anonymous: PROPVARIANT_0 { - Anonymous: ManuallyDrop::new(PROPVARIANT_0_0 { vt: $vt, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: PROPVARIANT_0_0_0 { $field: $value(value) } }), - }, - } - } - } }; } @@ -40,30 +30,11 @@ impl Clone for VARIANT { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl Clone for PROPVARIANT { - fn clone(&self) -> Self { - unsafe { - let mut value = Self::default(); - _ = PropVariantCopy(&mut value, self); - value - } - } -} - impl Drop for VARIANT { fn drop(&mut self) { unsafe { _ = VariantClear(self) }; } } - -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl Drop for PROPVARIANT { - fn drop(&mut self) { - unsafe { _ = PropVariantClear(self) }; - } -} - impl VARIANT { pub fn vt(&self) -> VARENUM { unsafe { self.Anonymous.Anonymous.vt } @@ -74,17 +45,6 @@ impl VARIANT { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl PROPVARIANT { - pub fn vt(&self) -> VARENUM { - unsafe { self.Anonymous.Anonymous.vt } - } - - pub fn is_empty(&self) -> bool { - self.vt() == VT_EMPTY - } -} - #[cfg(feature = "Win32_System_Com_StructuredStorage")] impl core::fmt::Debug for VARIANT { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { @@ -99,20 +59,6 @@ impl core::fmt::Debug for VARIANT { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl core::fmt::Debug for PROPVARIANT { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - let mut debug = f.debug_struct("PROPVARIANT"); - debug.field("type", &unsafe { self.Anonymous.Anonymous.vt }); - - if let Ok(value) = BSTR::try_from(self) { - debug.field("value", &value); - } - - debug.finish() - } -} - #[cfg(feature = "Win32_System_Com_StructuredStorage")] impl core::fmt::Display for VARIANT { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { @@ -120,13 +66,6 @@ impl core::fmt::Display for VARIANT { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl core::fmt::Display for PROPVARIANT { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - core::write!(f, "{}", BSTR::try_from(self).unwrap_or_default()) - } -} - #[cfg(feature = "Win32_System_Com_StructuredStorage")] impl PartialEq for VARIANT { fn eq(&self, other: &Self) -> bool { @@ -148,31 +87,8 @@ impl PartialEq for VARIANT { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl PartialEq for PROPVARIANT { - fn eq(&self, other: &Self) -> bool { - unsafe { - if self.Anonymous.Anonymous.vt != other.Anonymous.Anonymous.vt { - return false; - } - - PropVariantCompareEx(self, other, PVCU_DEFAULT, PVCF_DEFAULT) == 0 - } - } -} - #[cfg(feature = "Win32_System_Com_StructuredStorage")] impl Eq for VARIANT {} -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl Eq for PROPVARIANT {} - -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&VARIANT> for PROPVARIANT { - type Error = Error; - fn try_from(from: &VARIANT) -> Result { - unsafe { VariantToPropVariant(from) } - } -} #[cfg(feature = "Win32_System_Com_StructuredStorage")] impl TryFrom<&PROPVARIANT> for VARIANT { @@ -200,21 +116,6 @@ impl TryFrom<&VARIANT> for IUnknown { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for IUnknown { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { - if from.Anonymous.Anonymous.vt == VT_UNKNOWN && !from.Anonymous.Anonymous.Anonymous.punkVal.is_none() { - let unknown: &IUnknown = transmute(&from.Anonymous.Anonymous.Anonymous.punkVal); - Ok(unknown.clone()) - } else { - Err(Error::from_hresult(TYPE_E_TYPEMISMATCH)) - } - } - } -} - // VT_BSTR variant_from_value!(BSTR, VT_BSTR, bstrVal, |v: BSTR| ManuallyDrop::new(v)); @@ -225,13 +126,6 @@ impl From<&str> for VARIANT { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl From<&str> for PROPVARIANT { - fn from(value: &str) -> Self { - BSTR::from(value).into() - } -} - #[cfg(feature = "Win32_System_Com_StructuredStorage")] impl TryFrom<&VARIANT> for BSTR { type Error = Error; @@ -241,14 +135,6 @@ impl TryFrom<&VARIANT> for BSTR { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for BSTR { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { PropVariantToBSTR(from) } - } -} - // VT_BOOL variant_from_value!(bool, VT_BOOL, boolVal, |v: bool| VARIANT_BOOL(if v { -1 } else { 0 })); @@ -260,14 +146,6 @@ impl TryFrom<&VARIANT> for bool { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for bool { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { PropVariantToBoolean(from) }.map(|ok| ok.0 != 0) - } -} - // VT_UI1 variant_from_value!(u8, VT_UI1, bVal, |v: u8| v); @@ -287,14 +165,6 @@ impl TryFrom<&VARIANT> for u16 { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for u16 { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { PropVariantToUInt16(from) } - } -} - // VT_I2 variant_from_value!(i16, VT_I2, iVal, |v: i16| v); @@ -306,14 +176,6 @@ impl TryFrom<&VARIANT> for i16 { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for i16 { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { PropVariantToInt16(from) } - } -} - // VT_UI4 variant_from_value!(u32, VT_UI4, ulVal, |v: u32| v); @@ -325,14 +187,6 @@ impl TryFrom<&VARIANT> for u32 { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for u32 { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { PropVariantToUInt32(from) } - } -} - // VT_I4 variant_from_value!(i32, VT_I4, lVal, |v: i32| v); @@ -344,14 +198,6 @@ impl TryFrom<&VARIANT> for i32 { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for i32 { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { PropVariantToInt32(from) } - } -} - // VT_UI8 impl From for VARIANT { @@ -364,17 +210,6 @@ impl From for VARIANT { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl From for PROPVARIANT { - fn from(value: u64) -> Self { - Self { - Anonymous: PROPVARIANT_0 { - Anonymous: ManuallyDrop::new(PROPVARIANT_0_0 { vt: VT_UI8, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: PROPVARIANT_0_0_0 { uhVal: value } }), - }, - } - } -} - impl TryFrom<&VARIANT> for u64 { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -382,14 +217,6 @@ impl TryFrom<&VARIANT> for u64 { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for u64 { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { PropVariantToUInt64(from) } - } -} - // VT_I8 impl From for VARIANT { @@ -400,17 +227,6 @@ impl From for VARIANT { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl From for PROPVARIANT { - fn from(value: i64) -> Self { - Self { - Anonymous: PROPVARIANT_0 { - Anonymous: ManuallyDrop::new(PROPVARIANT_0_0 { vt: VT_I8, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: PROPVARIANT_0_0_0 { hVal: value } }), - }, - } - } -} - impl TryFrom<&VARIANT> for i64 { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -418,14 +234,6 @@ impl TryFrom<&VARIANT> for i64 { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for i64 { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { PropVariantToInt64(from) } - } -} - // VT_R4 variant_from_value!(f32, VT_R4, fltVal, |v: f32| v); @@ -441,14 +249,6 @@ impl TryFrom<&VARIANT> for f64 { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for f64 { - type Error = Error; - fn try_from(from: &PROPVARIANT) -> Result { - unsafe { PropVariantToDouble(from) } - } -} - // VT_DISPATCH impl From for VARIANT { @@ -463,19 +263,6 @@ impl From for VARIANT { } } -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl From for PROPVARIANT { - fn from(value: IDispatch) -> Self { - unsafe { - Self { - Anonymous: PROPVARIANT_0 { - Anonymous: ManuallyDrop::new(PROPVARIANT_0_0 { vt: VT_DISPATCH, wReserved1: 0, wReserved2: 0, wReserved3: 0, Anonymous: PROPVARIANT_0_0_0 { pdispVal: transmute(value) } }), - }, - } - } - } -} - impl TryFrom<&VARIANT> for IDispatch { type Error = windows_core::Error; fn try_from(from: &VARIANT) -> windows_core::Result { @@ -489,18 +276,3 @@ impl TryFrom<&VARIANT> for IDispatch { } } } - -#[cfg(feature = "Win32_System_Com_StructuredStorage")] -impl TryFrom<&PROPVARIANT> for IDispatch { - type Error = windows_core::Error; - fn try_from(from: &PROPVARIANT) -> windows_core::Result { - unsafe { - if from.Anonymous.Anonymous.vt == VT_DISPATCH && !from.Anonymous.Anonymous.Anonymous.pdispVal.is_none() { - let dispatch: &IDispatch = transmute(&from.Anonymous.Anonymous.Anonymous.pdispVal); - Ok(dispatch.clone()) - } else { - Err(windows_core::Error::from_hresult(TYPE_E_TYPEMISMATCH)) - } - } - } -}