Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace unmaintained winapi with windows-sys #18

Merged
merged 4 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
hard_tabs = true

# Requires nightly
# imports_granularity = "Module"
59 changes: 58 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 31 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
[package]
name = "inno_updater"
version = "0.9.0"
authors = ["Microsoft <[email protected]>"]
build = "build.rs"

[dependencies]
byteorder = "1"
crc = "^1.0.0"
slog = "2.1.1"
slog-async = "2.2.0"
slog-term = "2.3.0"

[target.'cfg(windows)'.dependencies]
winapi = { version = "^0.3.9", features = ["winuser", "libloaderapi", "commctrl", "processthreadsapi", "tlhelp32", "handleapi", "psapi", "errhandlingapi", "winbase", "shellapi"] }

[profile.release]
lto = true
panic = 'abort'
[package]
name = "inno_updater"
version = "0.9.0"
authors = ["Microsoft <[email protected]>"]
build = "build.rs"

[dependencies]
byteorder = "1"
crc = "^1.0.0"
slog = "2.1.1"
slog-async = "2.2.0"
slog-term = "2.3.0"

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.42"
features = [
"Win32_Foundation",
"Win32_System_Shutdown",
"Win32_UI_WindowsAndMessaging",
"Win32_System_Threading",
"Win32_System_LibraryLoader",
"Win32_System_Diagnostics_Debug",
"Win32_Storage_FileSystem",
"Win32_Security",
"Win32_System_ProcessStatus",
"Win32_System_Diagnostics_ToolHelp"
]

[profile.release]
lto = true
panic = 'abort'
10 changes: 8 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ fn main() {

assert!(ecode.success(), "Resource compiler failed");

println!("cargo:rustc-link-search=native={}", resources.parent().unwrap().to_str().unwrap());
println!("cargo:rustc-link-lib={}", resources.file_stem().unwrap().to_str().unwrap());
println!(
"cargo:rustc-link-search=native={}",
resources.parent().unwrap().to_str().unwrap()
);
println!(
"cargo:rustc-link-lib={}",
resources.file_stem().unwrap().to_str().unwrap()
);
}
7 changes: 3 additions & 4 deletions src/blockio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use crc::{crc32, Hasher32};
use std::cmp;
use std::io;
use std::io::prelude::*;
use std::{cmp, io};

const BLOCK_MAX_SIZE: usize = 4096;

Expand Down Expand Up @@ -48,8 +47,8 @@ impl<'a> BlockRead<'a> {
}

let size = size as usize;
let mut buffer = &mut self.buffer[..size];
self.reader.read_exact(&mut buffer)?;
let buffer = &mut self.buffer[..size];
self.reader.read_exact(buffer)?;

let mut digest = crc32::Digest::new(crc32::IEEE);
digest.write(buffer);
Expand Down
55 changes: 28 additions & 27 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
use std::sync::mpsc::Sender;
use std::{mem, ptr};
use strings::to_utf16;
use winapi::shared::basetsd::INT_PTR;
use winapi::shared::minwindef::{BOOL, DWORD, LPARAM, UINT, WPARAM};
use winapi::shared::ntdef::LPCWSTR;
use winapi::shared::windef::HWND;
use windows_sys::core::PCWSTR;
use windows_sys::Win32::Foundation::{BOOL, HWND, LPARAM, WPARAM};

extern "system" {
pub fn ShutdownBlockReasonCreate(hWnd: HWND, pwszReason: LPCWSTR) -> BOOL;
pub fn ShutdownBlockReasonCreate(hWnd: HWND, pwszReason: PCWSTR) -> BOOL;
pub fn ShutdownBlockReasonDestroy(hWnd: HWND) -> BOOL;
}

Expand All @@ -21,23 +19,27 @@ struct DialogData {
tx: Sender<ProgressWindow>,
}

unsafe extern "system" fn dlgproc(hwnd: HWND, msg: UINT, _: WPARAM, l: LPARAM) -> INT_PTR {
unsafe extern "system" fn dlgproc(hwnd: HWND, msg: u32, _: WPARAM, l: LPARAM) -> isize {
use resources;
use winapi::shared::windef::RECT;
use winapi::um::commctrl::PBM_SETMARQUEE;
use winapi::um::processthreadsapi::GetCurrentThreadId;
use winapi::um::winuser::{
GetDesktopWindow, GetWindowRect, SendDlgItemMessageW, SetWindowPos, ShowWindow, HWND_TOPMOST,
SW_HIDE, WM_DESTROY, WM_INITDIALOG,
use windows_sys::Win32::Foundation::RECT;
use windows_sys::Win32::System::Threading::GetCurrentThreadId;
use windows_sys::Win32::UI::WindowsAndMessaging::{
GetDesktopWindow, GetWindowRect, SendDlgItemMessageW, SetWindowPos, ShowWindow,
HWND_TOPMOST, SW_HIDE, WM_DESTROY, WM_INITDIALOG, WM_USER,
};

match msg {
WM_INITDIALOG => {
let data = &*(l as *const DialogData);
if !data.silent {
SendDlgItemMessageW(hwnd, resources::PROGRESS_SLIDER, PBM_SETMARQUEE, 1, 0);

let mut rect = mem::MaybeUninit::<RECT>::uninit().assume_init();
SendDlgItemMessageW(hwnd, resources::PROGRESS_SLIDER, WM_USER + 10, 1, 0);

let mut rect = RECT {
top: 0,
left: 0,
bottom: 0,
right: 0,
};
GetWindowRect(hwnd, &mut rect);

let width = rect.right - rect.left;
Expand All @@ -58,8 +60,7 @@ unsafe extern "system" fn dlgproc(hwnd: HWND, msg: UINT, _: WPARAM, l: LPARAM) -
ShowWindow(hwnd, SW_HIDE);
}

data
.tx
data.tx
.send(ProgressWindow {
ui_thread_id: GetCurrentThreadId(),
})
Expand All @@ -77,14 +78,14 @@ unsafe extern "system" fn dlgproc(hwnd: HWND, msg: UINT, _: WPARAM, l: LPARAM) -
}

pub struct ProgressWindow {
ui_thread_id: DWORD,
ui_thread_id: u32,
}

unsafe impl Send for ProgressWindow {}

impl ProgressWindow {
pub fn exit(&self) {
use winapi::um::winuser::{PostThreadMessageW, WM_QUIT};
use windows_sys::Win32::UI::WindowsAndMessaging::{PostThreadMessageW, WM_QUIT};

unsafe {
PostThreadMessageW(self.ui_thread_id, WM_QUIT, 0, 0);
Expand All @@ -94,16 +95,16 @@ impl ProgressWindow {

pub fn run_progress_window(silent: bool, tx: Sender<ProgressWindow>) {
use resources;
use winapi::um::libloaderapi::GetModuleHandleW;
use winapi::um::winuser::{DialogBoxParamW, MAKEINTRESOURCEW};
use windows_sys::Win32::System::LibraryLoader::GetModuleHandleW;
use windows_sys::Win32::UI::WindowsAndMessaging::DialogBoxParamW;

let data = DialogData { silent, tx };

unsafe {
DialogBoxParamW(
GetModuleHandleW(ptr::null_mut()),
MAKEINTRESOURCEW(resources::PROGRESS_DIALOG),
ptr::null_mut(),
resources::PROGRESS_DIALOG as PCWSTR,
mem::zeroed(),
Some(dlgproc),
(&data as *const DialogData) as LPARAM,
);
Expand All @@ -130,16 +131,16 @@ pub enum MessageBoxResult {
}

pub fn message_box(text: &str, caption: &str, mbtype: MessageBoxType) -> MessageBoxResult {
use winapi::um::winuser::{
MessageBoxW, IDABORT, IDCANCEL, IDCONTINUE, IDIGNORE, IDNO, IDOK, IDRETRY, IDTRYAGAIN, IDYES,
MB_ICONERROR, MB_RETRYCANCEL, MB_SYSTEMMODAL,
use windows_sys::Win32::UI::WindowsAndMessaging::{
MessageBoxW, IDABORT, IDCANCEL, IDCONTINUE, IDIGNORE, IDNO, IDOK, IDRETRY, IDTRYAGAIN,
IDYES, MB_ICONERROR, MB_RETRYCANCEL, MB_SYSTEMMODAL,
};

let result: i32;

unsafe {
result = MessageBoxW(
ptr::null_mut(),
mem::zeroed(),
to_utf16(text).as_ptr(),
to_utf16(caption).as_ptr(),
match mbtype {
Expand Down
Loading
Loading