From 04264eeed61c733add984f17ca6cee74dd93628c Mon Sep 17 00:00:00 2001 From: vSylva <58687743+vSylva@users.noreply.github.com> Date: Fri, 9 Jun 2023 02:39:18 +0800 Subject: [PATCH] Simplify dependencies (#105) Removed the "widestring" dependency --- Cargo.toml | 1 - examples/dx11_host.rs | 8 ++------ src/hooks/dx12.rs | 23 +++++++++++------------ src/inject.rs | 20 ++++++-------------- src/renderers/imgui_dx12.rs | 13 +++++-------- 5 files changed, 24 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f9a7fdd2..f7c22dd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,6 @@ crate-type = ["bin"] imgui = "0.11.0" imgui-opengl = "0.1.0" parking_lot = "0.11.2" -widestring = "1.0.1" windows = { version = "0.39.0", features = [ "Win32_Devices_HumanInterfaceDevice", "Win32_Foundation", diff --git a/examples/dx11_host.rs b/examples/dx11_host.rs index 87348f43..f6e2d0ff 100644 --- a/examples/dx11_host.rs +++ b/examples/dx11_host.rs @@ -6,7 +6,7 @@ use std::mem::MaybeUninit; use std::ptr::{null, null_mut}; -use windows::core::{PCSTR, PCWSTR}; +use windows::core::{HSTRING, PCSTR, PCWSTR}; use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM}; use windows::Win32::Graphics::Dxgi::{ DXGIGetDebugInterface1, IDXGIInfoQueue, DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE, @@ -64,11 +64,7 @@ pub fn main(_argc: i32, _argv: *const *const u8) { println!("{:?}", dll_path.canonicalize()); unsafe { LoadLibraryExW( - PCWSTR( - widestring::WideCString::from_os_str(dll_path.canonicalize().unwrap().as_os_str()) - .unwrap() - .as_ptr(), - ), + PCWSTR(HSTRING::from(dll_path.canonicalize().unwrap().as_os_str()).as_ptr()), None, LOAD_LIBRARY_FLAGS(0), ) diff --git a/src/hooks/dx12.rs b/src/hooks/dx12.rs index b9bd95a2..8fe872d9 100644 --- a/src/hooks/dx12.rs +++ b/src/hooks/dx12.rs @@ -10,8 +10,7 @@ use std::time::{Duration, Instant}; use imgui::Context; use parking_lot::Mutex; use tracing::{debug, error, info, trace}; -use widestring::{u16cstr, U16CStr}; -use windows::core::{Interface, HRESULT, PCWSTR}; +use windows::core::{Interface, HRESULT, HSTRING, PCWSTR}; use windows::w; use windows::Win32::Foundation::{ GetLastError, BOOL, HANDLE, HWND, LPARAM, LRESULT, POINT, RECT, WPARAM, @@ -80,15 +79,15 @@ static TRAMPOLINE: OnceLock<( ResizeBuffersType, )> = OnceLock::new(); -const COMMAND_ALLOCATOR_NAMES: [&U16CStr; 8] = [ - u16cstr!("hudhook Command allocator #0"), - u16cstr!("hudhook Command allocator #1"), - u16cstr!("hudhook Command allocator #2"), - u16cstr!("hudhook Command allocator #3"), - u16cstr!("hudhook Command allocator #4"), - u16cstr!("hudhook Command allocator #5"), - u16cstr!("hudhook Command allocator #6"), - u16cstr!("hudhook Command allocator #7"), +const COMMAND_ALLOCATOR_NAMES: [&HSTRING; 8] = [ + w!("hudhook Command allocator #0"), + w!("hudhook Command allocator #1"), + w!("hudhook Command allocator #2"), + w!("hudhook Command allocator #3"), + w!("hudhook Command allocator #4"), + w!("hudhook Command allocator #5"), + w!("hudhook Command allocator #6"), + w!("hudhook Command allocator #7"), ]; //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -317,7 +316,7 @@ impl ImguiRenderer { command_list.Close().unwrap(); command_list - .SetName(PCWSTR(u16cstr!("hudhook Command List").as_ptr())) + .SetName(PCWSTR(w!("hudhook Command List").as_ptr())) .expect("Couldn't set command list name"); let rtv_heap: ID3D12DescriptorHeap = dev diff --git a/src/inject.rs b/src/inject.rs index 971dda0c..033638a1 100644 --- a/src/inject.rs +++ b/src/inject.rs @@ -6,8 +6,7 @@ use std::path::PathBuf; use std::ptr::{null, null_mut}; use tracing::debug; -use widestring::{U16CStr, U16CString}; -use windows::core::{Error, Result, HRESULT, PCSTR, PCWSTR}; +use windows::core::{Error, Result, HRESULT, HSTRING, PCSTR, PCWSTR}; use windows::Win32::Foundation::{CloseHandle, GetLastError, BOOL, HANDLE, MAX_PATH}; use windows::Win32::System::Diagnostics::Debug::WriteProcessMemory; use windows::Win32::System::Diagnostics::ToolHelp::{ @@ -52,9 +51,7 @@ impl Process { ) }; - let dll_path = - widestring::WideCString::from_os_str(dll_path.canonicalize().unwrap().as_os_str()) - .unwrap(); + let dll_path = HSTRING::from(dll_path.canonicalize().unwrap().as_os_str()); let dll_path_buf = unsafe { VirtualAllocEx( self.0, @@ -141,7 +138,7 @@ unsafe fn get_process_by_title32(title: &str) -> Result { // 64-bit implementation. Uses [`widestring::U16CString`] and `FindWindowW`. unsafe fn get_process_by_title64(title: &str) -> Result { - let title = U16CString::from_str_truncate(title); + let title = HSTRING::from(title); let hwnd = FindWindowW(PCWSTR(null()), PCWSTR(title.as_ptr())); if hwnd.0 == 0 { @@ -210,7 +207,7 @@ unsafe fn get_process_by_name32(name: &str) -> Result { // 64-bit implementation. Uses [`PROCESSENTRY32W`] and // [`widestring::U16CString`]. unsafe fn get_process_by_name64(name: &str) -> Result { - let name = U16CString::from_str_truncate(name); + let name = PCWSTR::from_raw(HSTRING::from(name).as_ptr()).display().to_string(); let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)?; let mut pe32 = @@ -225,19 +222,14 @@ unsafe fn get_process_by_name64(name: &str) -> Result { } let pid = loop { - let proc_name = - U16CStr::from_ptr_truncate(pe32.szExeFile.as_ptr(), pe32.szExeFile.len()).unwrap(); - + let proc_name = PCWSTR::from_raw(pe32.szExeFile.as_ptr()).display().to_string(); if proc_name == name { break Ok(pe32.th32ProcessID); } if !Process32NextW(snapshot, &mut pe32).as_bool() { CloseHandle(snapshot); - break Err(Error::new( - HRESULT(0), - format!("Process {} not found", name.to_string_lossy()).into(), - )); + break Err(Error::new(HRESULT(0), format!("Process {} not found", name).into())); } }?; diff --git a/src/renderers/imgui_dx12.rs b/src/renderers/imgui_dx12.rs index 9f20775a..ae87b093 100644 --- a/src/renderers/imgui_dx12.rs +++ b/src/renderers/imgui_dx12.rs @@ -6,8 +6,8 @@ pub use imgui; use imgui::internal::RawWrapper; use imgui::{BackendFlags, DrawCmd, DrawData, DrawIdx, DrawVert, TextureId}; use tracing::{error, trace}; -use widestring::u16cstr; use windows::core::{Result, PCSTR, PCWSTR}; +use windows::w; use windows::Win32::Foundation::{CloseHandle, BOOL, RECT}; use windows::Win32::Graphics::Direct3D::Fxc::D3DCompile; use windows::Win32::Graphics::Direct3D::{ @@ -736,17 +736,14 @@ impl RenderEngine { } .unwrap(); - unsafe { - cmd_queue.SetName(PCWSTR(u16cstr!("hudhook font texture Command Queue").as_ptr())) - } - .unwrap(); + unsafe { cmd_queue.SetName(PCWSTR(w!("hudhook font texture Command Queue").as_ptr())) } + .unwrap(); let cmd_allocator: ID3D12CommandAllocator = unsafe { self.dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT) }.unwrap(); unsafe { - cmd_allocator - .SetName(PCWSTR(u16cstr!("hudhook font texture Command Allocator").as_ptr())) + cmd_allocator.SetName(PCWSTR(w!("hudhook font texture Command Allocator").as_ptr())) } .unwrap(); @@ -755,7 +752,7 @@ impl RenderEngine { } .unwrap(); - unsafe { cmd_list.SetName(PCWSTR(u16cstr!("hudhook font texture Command List").as_ptr())) } + unsafe { cmd_list.SetName(PCWSTR(w!("hudhook font texture Command List").as_ptr())) } .unwrap(); let src_location = D3D12_TEXTURE_COPY_LOCATION {