From f3aa338b271af88e993fe693fda472c30d8b63a2 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Wed, 28 Feb 2024 15:40:18 -0600 Subject: [PATCH] Allow `PWSTR`/`PSTR` to be passed to `PCWSTR`/`PCSTR` parameters (#2899) --- crates/libs/core/src/param.rs | 12 ++++++++++++ crates/tests/string_param/Cargo.toml | 1 + crates/tests/string_param/tests/pwstr.rs | 17 +++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/crates/libs/core/src/param.rs b/crates/libs/core/src/param.rs index cd16d473f2..5061d41c0b 100644 --- a/crates/libs/core/src/param.rs +++ b/crates/libs/core/src/param.rs @@ -91,3 +91,15 @@ impl IntoParam for &HSTRING { Param::Owned(PCWSTR(self.as_ptr())) } } + +impl IntoParam for PWSTR { + unsafe fn into_param(self) -> Param { + Param::Owned(PCWSTR(self.0)) + } +} + +impl IntoParam for PSTR { + unsafe fn into_param(self) -> Param { + Param::Owned(PCSTR(self.0)) + } +} diff --git a/crates/tests/string_param/Cargo.toml b/crates/tests/string_param/Cargo.toml index 87df5ff40a..929589de05 100644 --- a/crates/tests/string_param/Cargo.toml +++ b/crates/tests/string_param/Cargo.toml @@ -8,4 +8,5 @@ publish = false path = "../../libs/windows" features = [ "Win32_Foundation", + "Win32_UI_Shell", ] diff --git a/crates/tests/string_param/tests/pwstr.rs b/crates/tests/string_param/tests/pwstr.rs index 8d4ad40715..2510c9a889 100644 --- a/crates/tests/string_param/tests/pwstr.rs +++ b/crates/tests/string_param/tests/pwstr.rs @@ -1,7 +1,7 @@ -use windows::{core::*, Win32::Foundation::*}; +use windows::{core::*, Win32::Foundation::*, Win32::UI::Shell::*}; #[test] -fn test() { +fn error() { unsafe { SetLastError(ERROR_BUSY_DRIVE); @@ -15,3 +15,16 @@ fn test() { assert_eq!(GetLastError(), ERROR_BUSY_DRIVE); } } + +#[test] +fn convert() { + unsafe { + let pcwstr: PCWSTR = w!("https://github.com/microsoft"); + let pwstr = PWSTR(pcwstr.0 as _); + let pcstr: PCSTR = s!("https://github.com/microsoft"); + let pstr = PSTR(pcstr.0 as _); + + assert_eq!(0, UrlCompareW(pcwstr, pwstr, true)); + assert_eq!(0, UrlCompareA(pcstr, pstr, true)); + } +}