From a2f0a532a8349883c00a97dc443b6b428b989306 Mon Sep 17 00:00:00 2001 From: BTMuli Date: Fri, 16 Jan 2026 20:38:54 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20=E8=B0=83=E6=95=B4dll=E5=AF=BB?= =?UTF-8?q?=E6=89=BE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/yae/inject.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/yae/inject.rs b/src-tauri/src/yae/inject.rs index 7566196f..c6de7f27 100644 --- a/src-tauri/src/yae/inject.rs +++ b/src-tauri/src/yae/inject.rs @@ -1,7 +1,8 @@ //! DLL 注入相关功能 -//! @since Beta v0.9.1 +//! @since Beta v0.9.2 #![cfg(target_os = "windows")] +use std::path::Path; use std::ptr; use widestring::U16CString; use windows_sys::Win32::Foundation::{CloseHandle, FreeLibrary, HANDLE, INVALID_HANDLE_VALUE}; @@ -133,11 +134,18 @@ pub fn inject_dll(pi: &PROCESS_INFORMATION, dll_path: &str) { } } +/// 将 UTF-16 缓冲区转成 String fn utf16_to_string(buf: &[u16]) -> String { let nul_pos = buf.iter().position(|&c| c == 0).unwrap_or(buf.len()); String::from_utf16_lossy(&buf[..nul_pos]) } +/// 统一 DLL 名称:转小写并补全 .dll 后缀 +fn normalize_module_name(name: &str) -> String { + let lower = name.to_ascii_lowercase(); + if lower.ends_with(".dll") { lower } else { format!("{}.dll", lower) } +} + /// 枚举模块,找到 DLL 基址 pub fn find_module_base(pid: u32, dll_name: &str) -> Option { unsafe { @@ -149,13 +157,22 @@ pub fn find_module_base(pid: u32, dll_name: &str) -> Option { let mut me32 = MODULEENTRY32W { dwSize: std::mem::size_of::() as u32, ..Default::default() }; + let target_name = normalize_module_name(dll_name); + if Module32FirstW(snapshot, &mut me32) != 0 { loop { - let module_name = utf16_to_string(&me32.szModule); - if module_name.eq_ignore_ascii_case(dll_name) { + // 取文件名部分,避免路径干扰 + let module_name = Path::new(&utf16_to_string(&me32.szModule)) + .file_name() + .and_then(|s| s.to_str()) + .unwrap_or("") + .to_ascii_lowercase(); + + if module_name == target_name { CloseHandle(snapshot); return Some(me32.modBaseAddr as usize); } + if Module32NextW(snapshot, &mut me32) == 0 { break; }