Skip to content

Commit 1a7f65d

Browse files
authored
Feature/update windows crate (#2)
* Upgrade win crate to latest version * fix warnings * Replace K32GetModuleFileNameExA with K32GetModuleFileNameExW. Abstract windows widechar to string conversion. * Split process into files. Remove all ASCII win32 functions and replace them with widechar version * Bump version
1 parent cc0e01d commit 1a7f65d

14 files changed

+499
-407
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
[package]
1818
name = "mem-rs"
19-
version = "0.1.3"
19+
version = "0.1.4"
2020
edition = "2021"
2121
readme = "README.md"
2222
homepage = "https://github.com/FrankvdStam/mem-rs"
@@ -30,7 +30,7 @@ description = "pattern scanning and abstraction for pointers in memory of runnin
3030
[dependencies]
3131

3232
[dependencies.windows]
33-
version = "0.42.0"
33+
version = "0.56.0"
3434
features = [
3535
"Win32_Foundation",
3636
"Win32_System_Memory",

examples/dsr.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use std::thread::sleep;
1818
use std::time::Duration;
19+
use mem_rs::helpers::{get_w32str_from_str, vec_u16_to_u8};
1920
use mem_rs::prelude::*;
2021

2122
struct Ds1
@@ -61,15 +62,34 @@ impl Ds1
6162
}
6263
Ok(())
6364
}
65+
66+
#[allow(dead_code)]
67+
pub fn inject_soulmemory_rs(&self)
68+
{
69+
//self.process.inject_dll(r#"C:\soulmemory\soulmemory_rs.dll"#);
70+
//self.process.inject_dll(r#"C:\projects\soulmemory-rs\target\x86_64-pc-windows-msvc\release\soulmemory_rs.dll"#);
71+
self.process.inject_dll(r#"C:\projects\soulmemory-rs\target\x86_64-pc-windows-msvc\å\soulmemory_rs.dll"#).expect("TODO: panic message");
72+
}
6473
}
6574

6675
fn main()
6776
{
68-
//let processes = Process::get_running_process_names();
69-
//for p in &processes
70-
//{
71-
// println!("{}", p);
72-
//}
77+
let str = r#"C:\soulmemory\soulmemory_rs.dll"#;
78+
let w32_str = get_w32str_from_str(str);
79+
println!("{:?}", w32_str);
80+
println!("{:?}", vec_u16_to_u8(&w32_str));
81+
82+
let alloated_str = String::from(str);
83+
let collected: Vec<u16> = alloated_str.encode_utf16().collect();
84+
println!("{:?}", collected);
85+
unsafe { println!("{:?}", collected.align_to::<u8>()); }
86+
87+
88+
let processes = Process::get_running_process_names();
89+
for p in &processes
90+
{
91+
println!("{}", p);
92+
}
7393

7494
let mut ds1 = Ds1::new();
7595

@@ -81,17 +101,10 @@ fn main()
81101
Err(e) => println!("{}", e)
82102
}
83103

104+
//ds1.inject_soulmemory_rs();
105+
84106
println!("igt: {}", ds1.get_in_game_time_milliseconds());
85107
println!("ai: {}", ds1.get_ai_timer());
86108
sleep(Duration::from_secs(1));
87109
}
88-
}
89-
90-
#[allow(dead_code)]
91-
fn inject()
92-
{
93-
let mut process = Process::new("DarkSoulsRemastered.exe");
94-
process.refresh().unwrap();
95-
process.inject_dll(r#"C:\projects\soulmemory-rs\target\x86_64-pc-windows-msvc\debug\soulmemory_rs.dll"#);
96-
}
97-
110+
}

src/helpers.rs

+33
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17+
use std::path::Path;
18+
use windows::core::{PCSTR, PCWSTR};
19+
1720
pub fn scan(code: &[u8], pattern: &[Option<u8>]) -> Option<usize>
1821
{
1922
if code.len() == 0
@@ -58,4 +61,34 @@ pub fn to_pattern(str: &str) -> Vec<Option<u8>>
5861
}
5962
}
6063
return vec;
64+
}
65+
66+
pub fn vec_u16_to_u8(vec_u16: &Vec<u16>) -> Vec<u8>
67+
{
68+
return unsafe { vec_u16.align_to::<u8>().1.to_vec() };
69+
}
70+
71+
pub fn w32str_to_string(w32str: &Vec<u16>) -> String
72+
{
73+
return w32str.iter().map(|&v| (v & 0xFF) as u8).take_while(|&c| c != 0).map(|c| c as char).collect();
74+
}
75+
76+
pub fn get_file_name_from_string(str: &String) -> String
77+
{
78+
return String::from(Path::new(&str).file_name().unwrap().to_str().unwrap());
79+
}
80+
81+
pub fn get_w32str_from_str(str: &str) -> Vec<u16>
82+
{
83+
return str.encode_utf16().collect();
84+
}
85+
pub fn get_pcwstr_from_str(str: &str) -> PCWSTR
86+
{
87+
let vec: Vec<u16> = str.encode_utf16().collect();
88+
return PCWSTR(vec.as_ptr());
89+
}
90+
91+
pub fn get_pcstr_from_str(str: &str) -> PCSTR
92+
{
93+
return PCSTR(str.as_ptr());
6194
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
mod process_data;
1818
mod process_module;
19-
mod helpers;
19+
pub mod helpers;
2020
pub mod read_write;
2121
pub mod process;
2222
pub mod pointer;

0 commit comments

Comments
 (0)