From 18171bc4e8757e5ede5760503d43933b6dbedf74 Mon Sep 17 00:00:00 2001 From: takubokudori Date: Sat, 24 Jul 2021 17:38:01 +0900 Subject: [PATCH] fix --- README.md | 17 +++++- ...er_name.rs => get_environment_variable.rs} | 2 +- examples/message_box.rs | 55 +++++++++++++++++++ src/string.rs | 7 --- 4 files changed, 72 insertions(+), 9 deletions(-) rename examples/{get_user_name.rs => get_environment_variable.rs} (98%) create mode 100644 examples/message_box.rs diff --git a/README.md b/README.md index beda706..040575a 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,23 @@ windy-macros = "0.1.1" use windy::WStr; use windy_macros::wstr; +#[allow(non_snake_case)] +#[link(name = "user32")] +extern "system" { + pub fn MessageBoxW( + hWnd: *mut c_void, + lpText: *const u16, + lpCaption: *const u16, + uType: u32, + ) -> i32; +} + fn main() { - let s: &WStr = wstr!("test"); + let text: &WStr = wstr!("World"); + let caption: &WStr = wstr!("CaptionW"); + unsafe { + MessageBoxW(0 as _, text.as_ptr(), caption.as_ptr(), 0); + } } ``` diff --git a/examples/get_user_name.rs b/examples/get_environment_variable.rs similarity index 98% rename from examples/get_user_name.rs rename to examples/get_environment_variable.rs index b67eb41..4bf8dec 100644 --- a/examples/get_user_name.rs +++ b/examples/get_environment_variable.rs @@ -7,7 +7,7 @@ use windy::*; #[cfg(feature = "std")] extern "system" { fn GetEnvironmentVariableA( - lpName: *const u8, + lpName: *const i8, lpBuffer: *mut u8, nSize: u32, ) -> u32; diff --git a/examples/message_box.rs b/examples/message_box.rs new file mode 100644 index 0000000..cf03326 --- /dev/null +++ b/examples/message_box.rs @@ -0,0 +1,55 @@ +// Copyright takubokudori. +// This source code is licensed under the MIT or Apache-2.0 license. +use core::ffi::c_void; +#[cfg(feature = "std")] +use windy::*; + +#[allow(non_snake_case)] +#[cfg(feature = "std")] +#[link(name = "user32")] +extern "system" { + pub fn MessageBoxA( + hWnd: *mut c_void, + lpText: *const i8, + lpCaption: *const i8, + uType: u32, + ) -> i32; + + pub fn MessageBoxW( + hWnd: *mut c_void, + lpText: *const u16, + lpCaption: *const u16, + uType: u32, + ) -> i32; +} + +#[cfg(feature = "std")] +fn message_box_a() { + let text = AString::from_str("Hello").unwrap(); + let caption = AString::from_str("CaptionA").unwrap(); + unsafe { + MessageBoxA(0 as _, text.as_ptr(), caption.as_ptr(), 0); + } +} + +#[cfg(feature = "std")] +fn message_box_w() { + let text = WString::from_str("World").unwrap(); + let caption = WString::from_str("CaptionW").unwrap(); + unsafe { + MessageBoxW(0 as _, text.as_ptr(), caption.as_ptr(), 0); + } +} + +#[cfg(feature = "std")] +fn main() { + println!("*****message_box_a*****"); + message_box_a(); + println!("*****message_box_w*****"); + message_box_w(); +} + +#[cfg(not(feature = "std"))] +fn main() { + panic!("Use std feature!"); +} diff --git a/src/string.rs b/src/string.rs index 8cd91c9..cd4059a 100644 --- a/src/string.rs +++ b/src/string.rs @@ -95,9 +95,6 @@ impl WString { } } - #[inline] - pub fn as_ptr(&self) -> *const u16 { self.inner.as_ptr() } - /// Returns the length of bytes. #[inline] pub fn len(&self) -> usize { self.inner.len() } @@ -367,10 +364,6 @@ impl AString { } } - /// Returns the `*const u8` pointer. - #[inline] - pub fn as_ptr(&self) -> *const u8 { self.inner.as_ptr() } - /// Returns the length of bytes. #[inline] pub fn len(&self) -> usize { self.inner.len() }