Skip to content

Commit

Permalink
Simplify dependencies (#105)
Browse files Browse the repository at this point in the history
Removed the "widestring" dependency
  • Loading branch information
vsylva authored Jun 8, 2023
1 parent 2a8d547 commit 04264ee
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 41 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 2 additions & 6 deletions examples/dx11_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
)
Expand Down
23 changes: 11 additions & 12 deletions src/hooks/dx12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
];

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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
Expand Down
20 changes: 6 additions & 14 deletions src/inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -141,7 +138,7 @@ unsafe fn get_process_by_title32(title: &str) -> Result<HANDLE> {

// 64-bit implementation. Uses [`widestring::U16CString`] and `FindWindowW`.
unsafe fn get_process_by_title64(title: &str) -> Result<HANDLE> {
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 {
Expand Down Expand Up @@ -210,7 +207,7 @@ unsafe fn get_process_by_name32(name: &str) -> Result<HANDLE> {
// 64-bit implementation. Uses [`PROCESSENTRY32W`] and
// [`widestring::U16CString`].
unsafe fn get_process_by_name64(name: &str) -> Result<HANDLE> {
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 =
Expand All @@ -225,19 +222,14 @@ unsafe fn get_process_by_name64(name: &str) -> Result<HANDLE> {
}

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()));
}
}?;

Expand Down
13 changes: 5 additions & 8 deletions src/renderers/imgui_dx12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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();

Expand All @@ -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 {
Expand Down

0 comments on commit 04264ee

Please sign in to comment.