Skip to content

Commit

Permalink
samples
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jun 18, 2024
1 parent a455d8b commit 70ef5dd
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 35 deletions.
14 changes: 7 additions & 7 deletions crates/samples/windows-sys/create_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ use windows_sys::{
fn main() {
unsafe {
let instance = GetModuleHandleA(std::ptr::null());
debug_assert!(instance != 0);
debug_assert!(!instance.is_null());

let window_class = s!("window");

let wc = WNDCLASSA {
hCursor: LoadCursorW(0, IDC_ARROW),
hCursor: LoadCursorW(core::ptr::null_mut(), IDC_ARROW),
hInstance: instance,
lpszClassName: window_class,
style: CS_HREDRAW | CS_VREDRAW,
lpfnWndProc: Some(wndproc),
cbClsExtra: 0,
cbWndExtra: 0,
hIcon: 0,
hbrBackground: 0,
hIcon: core::ptr::null_mut(),
hbrBackground: core::ptr::null_mut(),
lpszMenuName: std::ptr::null(),
};

Expand All @@ -35,15 +35,15 @@ fn main() {
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
core::ptr::null_mut(),
core::ptr::null_mut(),
instance,
std::ptr::null(),
);

let mut message = std::mem::zeroed();

while GetMessageA(&mut message, 0, 0, 0) != 0 {
while GetMessageA(&mut message, core::ptr::null_mut(), 0, 0) != 0 {
DispatchMessageA(&message);
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/samples/windows-sys/delay_load/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ use windows_sys::{core::*, Win32::Foundation::*, Win32::System::LibraryLoader::*
///
/// The `PCSTR` parameters need to be valid for reads up until and including the next `\0`.
pub unsafe fn delay_load<T>(library: PCSTR, function: PCSTR) -> Option<T> {
let library = LoadLibraryExA(library, 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
let library = LoadLibraryExA(
library,
core::ptr::null_mut(),
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS,
);

if library == 0 {
if library.is_null() {
return None;
}

Expand Down
11 changes: 9 additions & 2 deletions crates/samples/windows-sys/message_box/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ use windows_sys::{core::*, Win32::UI::Shell::*, Win32::UI::WindowsAndMessaging::

fn main() {
unsafe {
MessageBoxA(0, s!("Ansi"), s!("World"), MB_OK);
ShellMessageBoxW(0, 0, w!("Wide"), w!("World"), MB_ICONERROR);
MessageBoxA(core::ptr::null_mut(), s!("Ansi"), s!("World"), MB_OK);

ShellMessageBoxW(
core::ptr::null_mut(),
core::ptr::null_mut(),
w!("Wide"),
w!("World"),
MB_ICONERROR,
);
}
}
2 changes: 1 addition & 1 deletion crates/samples/windows-sys/privileges/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use windows_sys::{

fn main() {
unsafe {
let mut token = 0;
let mut token = core::ptr::null_mut();
OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token);
let mut bytes_required = 0;

Expand Down
2 changes: 1 addition & 1 deletion crates/samples/windows/consent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() -> Result<()> {
unsafe {
let interop = factory::<UserConsentVerifier, IUserConsentVerifierInterop>()?;

let window = HWND(0); // <== replace with your app's window handle
let window = HWND::default(); // <== replace with your app's window handle

let operation: IAsyncOperation<UserConsentVerificationResult> =
interop.RequestVerificationForWindowAsync(window, h!("Hello from Rust"))?;
Expand Down
4 changes: 1 addition & 3 deletions crates/samples/windows/create_window/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use windows::{
fn main() -> Result<()> {
unsafe {
let instance = GetModuleHandleA(None)?;
debug_assert!(instance.0 != 0);

let window_class = s!("window");

let wc = WNDCLASSA {
Expand Down Expand Up @@ -36,7 +34,7 @@ fn main() -> Result<()> {
None,
instance,
None,
);
)?;

let mut message = MSG::default();

Expand Down
8 changes: 4 additions & 4 deletions crates/samples/windows/dcomp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Window {
CoCreateInstance(&UIAnimationTransitionLibrary2, None, CLSCTX_INPROC_SERVER)?;

Ok(Window {
handle: HWND(0),
handle: Default::default(),
dpi: (0.0, 0.0),
format: create_text_format()?,
image: create_image()?,
Expand Down Expand Up @@ -446,13 +446,13 @@ impl Window {
None,
instance,
Some(self as *mut _ as _),
);
)?;

debug_assert!(handle.0 != 0);
debug_assert!(!handle.is_invalid());
debug_assert!(handle == self.handle);
let mut message = MSG::default();

while GetMessageA(&mut message, HWND(0), 0, 0).into() {
while GetMessageA(&mut message, HWND::default(), 0, 0).into() {
DispatchMessageA(&message);
}

Expand Down
13 changes: 6 additions & 7 deletions crates/samples/windows/direct2d/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Window {
};

Ok(Window {
handle: HWND(0),
handle: Default::default(),
factory,
dxfactory,
style,
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Window {
target.EndDraw(None, None)?;
}

if let Err(error) = self.present(1, 0) {
if let Err(error) = self.present(1, DXGI_PRESENT(0)) {
if error.code() == DXGI_STATUS_OCCLUDED {
self.occlusion = unsafe {
self.dxfactory
Expand All @@ -152,7 +152,7 @@ impl Window {
self.shadow = None;
}

fn present(&self, sync: u32, flags: u32) -> Result<()> {
fn present(&self, sync: u32, flags: DXGI_PRESENT) -> Result<()> {
unsafe { self.swapchain.as_ref().unwrap().Present(sync, flags).ok() }
}

Expand Down Expand Up @@ -320,7 +320,7 @@ impl Window {

if unsafe {
swapchain
.ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0)
.ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, DXGI_SWAP_CHAIN_FLAG(0))
.is_ok()
} {
create_swapchain_bitmap(swapchain, target)?;
Expand Down Expand Up @@ -379,7 +379,6 @@ impl Window {
fn run(&mut self) -> Result<()> {
unsafe {
let instance = GetModuleHandleA(None)?;
debug_assert!(instance.0 != 0);
let window_class = s!("window");

let wc = WNDCLASSA {
Expand Down Expand Up @@ -408,9 +407,9 @@ impl Window {
None,
instance,
Some(self as *mut _ as _),
);
)?;

debug_assert!(handle.0 != 0);
debug_assert!(!handle.is_invalid());
debug_assert!(handle == self.handle);
let mut message = MSG::default();

Expand Down
14 changes: 7 additions & 7 deletions crates/samples/windows/direct3d12/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
instance,
Some(&mut sample as *mut _ as _),
)
};
}?;

sample.bind_to_window(&hwnd)?;
unsafe { _ = ShowWindow(hwnd, SW_SHOW) };
Expand Down Expand Up @@ -179,11 +179,9 @@ extern "system" fn wndproc<S: DXSample>(
fn get_hardware_adapter(factory: &IDXGIFactory4) -> Result<IDXGIAdapter1> {
for i in 0.. {
let adapter = unsafe { factory.EnumAdapters1(i)? };
let desc = unsafe { adapter.GetDesc1()? };

let mut desc = Default::default();
unsafe { adapter.GetDesc1(&mut desc)? };

if (DXGI_ADAPTER_FLAG(desc.Flags as i32) & DXGI_ADAPTER_FLAG_SOFTWARE)
if (DXGI_ADAPTER_FLAG(desc.Flags as _) & DXGI_ADAPTER_FLAG_SOFTWARE)
!= DXGI_ADAPTER_FLAG_NONE
{
// Don't select the Basic Render Driver adapter. If you want a
Expand Down Expand Up @@ -415,7 +413,9 @@ mod d3d12_hello_triangle {
unsafe { resources.command_queue.ExecuteCommandLists(&[command_list]) };

// Present the frame.
unsafe { resources.swap_chain.Present(1, 0) }.ok().unwrap();
unsafe { resources.swap_chain.Present(1, DXGI_PRESENT(0)) }
.ok()
.unwrap();

wait_for_previous_frame(resources);
}
Expand Down Expand Up @@ -515,7 +515,7 @@ mod d3d12_hello_triangle {
let dxgi_factory_flags = if cfg!(debug_assertions) {
DXGI_CREATE_FACTORY_DEBUG
} else {
0
DXGI_CREATE_FACTORY_FLAGS(0)
};

let dxgi_factory: IDXGIFactory4 = unsafe { CreateDXGIFactory2(dxgi_factory_flags) }?;
Expand Down
2 changes: 1 addition & 1 deletion crates/samples/windows/uiautomation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use windows::{
fn main() -> Result<()> {
unsafe {
CoInitializeEx(None, COINIT_MULTITHREADED).ok()?;
let window = FindWindowA(None, s!("Calculator"));
let window = FindWindowA(None, s!("Calculator"))?;

// Start with COM API
let automation: IUIAutomation = CoCreateInstance(&CUIAutomation, None, CLSCTX_ALL)?;
Expand Down

0 comments on commit 70ef5dd

Please sign in to comment.