win-auto-utils 0.2.5

Universal Windows automation utilities with memory, window, input, and color operations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# Win Auto Utils

[ไธญๆ–‡ๆ–‡ๆกฃ]docs/zh/README.md | [English Documentation]docs/en/README.md

A universal Windows automation utility library providing atomic modules for memory operations, window management, input simulation, and color processing. Built with Rust for performance and safety.

## ๐Ÿš€ Features

### Core Capabilities

- **Process Management**: Process enumeration, handle aggregation, module snapshot (ToolHelp32)
- **Window Operations**: Window handle queries, enumeration, manipulation, DC management
- **Input Simulation**: Keyboard input (Key Down/Up/Press), mouse click & movement
- **Screen Capture**: High-performance DXGI-based capture, GDI color picking
- **Color Processing**: Pure Rust pixel color finding algorithms (zero dependencies)
- **Memory Operations**: Read/write process memory, address resolution, AOB scanning
- **Hooking System**: Inline hooks, trampoline hooks, register extraction
- **Script Engine**: Pure Rust interpreter with control flow, timing, keyboard/mouse instructions
- **DLL Injection**: Cross-architecture injection (x64โ†’x86/x64, WOW64 compatible)
- **Template Matching**: Image-based UI element detection with parallel processing

### Key Advantages

โœ… **Atomic Design**: Enable only what you need via feature flags
โœ… **Minimal Dependencies**: Core features depend only on `windows` crate
โœ… **Performance**: Release mode with LTO, size optimization, symbol stripping
โœ… **Safety**: Rust's ownership system prevents common memory errors
โœ… **Cross-platform Script Engine**: Pure Rust, no external dependencies

## ๐Ÿ“ฆ Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
win-auto-utils = { version = "0.2.5", features = ["standard"] }
```

For full functionality including template matching:

```toml
[dependencies]
win-auto-utils = { version = "0.2.3", features = ["full"] }
```

## ๐ŸŽฏ Quick Start

### Process Management

**Only three types needed for full functionality!**

```rust
use win_auto_utils::process::{Process, ProcessConfig, ProcessManager};

// Method 1: One-step initialization by name (most convenient)
let mut process = Process::init_by_name("notepad.exe")?;
println!("PID: {}", process.pid_or_default());

// Method 2: One-step initialization by PID (when you know the PID)
let process = Process::init_by_pid(12345)?;
println!("HWND: {:?}", process.hwnd_or_default());

// Method 3: Using Builder with intuitive methods (no enums needed!)
let config = ProcessConfig::builder("target.exe")
    .set_window_client_mode()  // Easy to remember - no DCMode enum!
    .exclude_invisible()
    .include_by_title("Game Window")
    .build();
let mut game = Process::new(config);
game.init()?;

// Method 4: Instance method init_with_pid (for existing Process objects)
// Useful for distinguishing multiple instances of the same process
let mut app = Process::by_name("target.exe");
app.init()?;  // Initialize first instance
// Later, switch to specific PID for second instance
app.init_with_pid(20908)?;

// Manager API (simple and straightforward):
let mut manager = ProcessManager::new();
manager.register("notepad.exe")?;  // process name becomes the key
manager.register_alias("game", "target.exe")?;  // custom alias
manager.init("notepad.exe")?;
manager.init_with_pid("game", 12345)?;  // initialize with specific PID

// Query processes (read-only, no mut needed)
if let Some(proc) = manager.get("notepad.exe") {
    println!("PID: {:?}", proc.pid());
}

// DC Mode Options (intuitive method names):
// - .set_window_mode()        -> Standard window DC (GetWindowDC)
// - .set_window_client_mode() -> Client area DC (GetDC) - best for games
// - .set_desktop_mode()       -> Desktop DC for full-screen capture
```

### Memory Operations

```rust
use win_auto_utils::memory::{read_memory_t, write_memory_t};

// Read a 32-bit integer
let value: i32 = read_memory_t(handle, address)?;

// Write a float value
write_memory_t::<f32>(handle, address, 999.0)?;
```

### Input Simulation

```rust
use win_auto_utils::keyboard::{SendInputKeyboard, PostMessageKeyboard};
use win_auto_utils::mouse::{SendInputMouse, PostMessageMouse};

// SendInput method (system-level input, works with all apps)
let mut kb = SendInputKeyboard::new();
kb.click("a")?;

let mut mouse = SendInputMouse::new();
mouse.move_to(100, 200)?;
mouse.click_left()?;

// PostMessage method (background input, no focus required, needs HWND)
let kb = PostMessageKeyboard::new(hwnd);
kb.click("a")?;

let mouse = PostMessageMouse::new(hwnd);
mouse.move_to(100, 200)?;
mouse.click_left_at(100, 200)?;
```

### Screen Capture (DXGI)

```rust
use win_auto_utils::dxgi::DxgiCapture;

let mut capture = DxgiCapture::new()?;
let image = capture.capture_window(hwnd)?;
```

### Color Finding

Search for colors in screen regions or pixel buffers with automatic AVX2 optimization:

```rust
use win_auto_utils::color_finder::{find_color, find_color_in_buffer};

// Method 1: Find color in screen region (uses DXGI capture internally)
match find_color(100, 100, 50, 50, (255, 0, 0)) {  // Search red in 50x50 region
    Ok(result) => {
        if result.matched {
            println!("Found at screen coordinates ({}, {})", result.x, result.y);
        }
    }
    Err(e) => eprintln!("Error: {}", e),
}

// Method 2: Find color in pixel buffer (pure algorithm, no screen capture)
use win_auto_utils::color_finder::algorithms::find_color_in_buffer;
let buffer: Vec<u8> = vec![0; 100 * 100 * 4];  // 100x100 BGRA pixels
let result = find_color_in_buffer(&buffer, 100, 100, (0, 255, 0));  // Search green
```

**Features:**
- AVX2 SIMD acceleration (~4-8x faster on supported hardware)
- Automatic fallback to scalar implementation
- Works with any BGRA pixel buffer source

### Memory Hooking (Recommended: Using Memory Manager)

```rust
use win_auto_utils::memory_manager::ModifierManager;
use win_auto_utils::memory_manager::builtin::TrampolineHookHandler;
use win_auto_utils::process::ProcessManager;

// Initialize process
let mut process_mgr = ProcessManager::new();
process_mgr.register("target_app.exe")?;
process_mgr.init("target_app.exe")?;

let proc = process_mgr.get("target_app.exe").unwrap();
let handle = proc.handle().unwrap();
let pid = proc.pid().unwrap();

// Create manager and bind context
let mut manager = ModifierManager::new();
manager.set_context(handle, pid);

// Register hook with shellcode (architecture auto-detected)
let shellcode = vec![0x90, 0x90]; // NOP instruction
let hook_handler = TrampolineHookHandler::new_hook_aob_with_offset(
    "func_hook",
    "48 8B 05 ?? ?? ?? ??",  // AOB pattern
    shellcode,
    2,                        // bytes_to_overwrite
    0x10,                     // offset
)?;
manager.register("func_hook", hook_handler);

// Activate hook
manager.activate("func_hook")?;

// ... trigger hook ...

// Deactivate (auto-frees memory)
manager.deactivate("func_hook")?;
```

**Legacy Direct API** (still supported but not recommended):

```rust
use win_auto_utils::memory_hook::TrampolineHook;

let shellcode = vec![0x01, 0xD2]; // add edx, edx
let mut hook = TrampolineHook::new_x86(handle, target_addr, shellcode);
hook.install()?;
// ... trigger hook ...
hook.uninstall()?; // Auto-frees memory
```

### Script Engine

```rust
use win_auto_utils::script_engine::ScriptEngine;

let script = r#"
    loop 10
        key VK_SPACE
        sleep 100
    end
"#;

let mut engine = ScriptEngine::new();
engine.execute(script)?;
```

## ๐Ÿ“š Documentation

Comprehensive documentation is available in both English and Chinese:

### Quick Links
- **[Documentation Index (EN)]docs/en/INDEX.md** - Complete navigation guide
- **[ๆ–‡ๆกฃ็ดขๅผ• (ไธญๆ–‡)]docs/zh/INDEX.md** - ๅฎŒๆ•ดๅฏผ่ˆชๆŒ‡ๅ—

### Module Documentation

#### English
- [Modules Overview]docs/en/modules/overview.md - High-level view of all modules
- [Memory Operations]docs/en/modules/memory.md
- [Memory Manager]docs/en/modules/memory_manager.md - Unified memory modification manager
- [Memory Hooking]docs/en/modules/memory_hook.md
- [Address Resolution]docs/en/modules/memory_resolver.md
- [AOB Scanning]docs/en/modules/memory_aobscan.md
- [Script Engine]docs/en/modules/script_engine.md
- [Input Control]docs/en/modules/input.md
- [Process & Window]docs/en/modules/process_window.md
- [Screen Capture]docs/en/modules/dxgi.md
- [Template Matching]docs/en/modules/template_matcher.md
- [DLL Injection]docs/en/modules/dll_injector.md

#### Chinese (ไธญๆ–‡)
- [ๆจกๅ—ๆฆ‚่งˆ]docs/zh/modules/overview.md - ๆ‰€ๆœ‰ๆจกๅ—็š„้ซ˜ๅฑ‚่ง†ๅ›พ
- [ๅ†…ๅญ˜ๆ“ไฝœ]docs/zh/modules/memory.md
- [ๅ†…ๅญ˜็ฎก็†ๅ™จ]docs/zh/modules/memory_manager.md - ็ปŸไธ€็š„ๅ†…ๅญ˜ไฟฎๆ”น็ฎก็†ๅ™จ
- [ๅ†…ๅญ˜้’ฉๅญ]docs/zh/modules/memory_hook.md
- [ๅœฐๅ€่งฃๆž]docs/zh/modules/memory_resolver.md
- [ๅญ—่Š‚ๆ‰ซๆ]docs/zh/modules/memory_aobscan.md
- [่„šๆœฌๅผ•ๆ“Ž]docs/zh/modules/script_engine.md
- [่พ“ๅ…ฅๆŽงๅˆถ]docs/zh/modules/input.md
- [่ฟ›็จ‹ไธŽ็ช—ๅฃ]docs/zh/modules/process_window.md
- [ๅฑๅน•ๆ•่Žท]docs/zh/modules/dxgi.md
- [ๆจกๆฟๅŒน้…]docs/zh/modules/template_matcher.md
- [DLLๆณจๅ…ฅ]docs/zh/modules/dll_injector.md

## ๐Ÿ—๏ธ Architecture

The library follows a modular architecture with feature-gated components:

```
win-auto-utils/
โ”œโ”€โ”€ Process & Window Layer
โ”‚   โ”œโ”€โ”€ process      - Process management
โ”‚   โ”œโ”€โ”€ hwnd         - Handle queries
โ”‚   โ”œโ”€โ”€ window       - Window manipulation
โ”‚   โ””โ”€โ”€ snapshot     - ToolHelp32 enumeration
โ”‚
โ”œโ”€โ”€ Input Layer
โ”‚   โ”œโ”€โ”€ keyboard     - Keyboard simulation
โ”‚   โ””โ”€โ”€ mouse        - Mouse control
โ”‚
โ”œโ”€โ”€ Graphics Layer
โ”‚   โ”œโ”€โ”€ dxgi         - Screen capture
โ”‚   โ”œโ”€โ”€ color_picker - GDI color picking
โ”‚   โ””โ”€โ”€ color_finder - Pixel color search
โ”‚
โ”œโ”€โ”€ Memory Layer
โ”‚   โ”œโ”€โ”€ memory       - Basic read/write
โ”‚   โ”œโ”€โ”€ memory_resolver - Symbolic addresses
โ”‚   โ”œโ”€โ”€ memory_aobscan  - Pattern scanning
โ”‚   โ””โ”€โ”€ memory_hook     - Inline/trampoline hooks
โ”‚
โ”œโ”€โ”€ Advanced Features
โ”‚   โ”œโ”€โ”€ dll_injector    - DLL injection
โ”‚   โ””โ”€โ”€ template_matcher - Image matching
โ”‚
โ””โ”€โ”€ Script Engine
    โ”œโ”€โ”€ script_engine      - Core interpreter
    โ””โ”€โ”€ scripts_builtin    - Built-in instructions
```

## ๐Ÿ”ง Feature Flags

Choose only what you need:

### Minimal Setup (~15s compilation)
```bash
cargo build --no-default-features --features "keyboard,mouse"
```

### Core Features (default)
```bash
cargo build  # Includes all stable features except template_matcher
```

### Full Features (~45-60s compilation)
```bash
cargo build --no-default-features --features "full"
```

### Available Features

| Feature | Description | Dependencies |
|---------|-------------|--------------|
| `process` | Process management | windows, hwnd, hdc, snapshot, handle |
| `keyboard` | Keyboard input | windows |
| `mouse` | Mouse control | windows |
| `color_picker` | GDI color picking | windows |
| `color_finder` | Pixel color search (AVX2/SIMD) | dxgi |
| `memory` | Memory read/write | windows |
| `memory_hook` | Hooking system | memory, windows |
| `memory_aobscan` | Pattern scanning | memory, memchr, rayon |
| `dxgi` | Screen capture | windows |
| `template_matcher` | Image matching | image, imageproc, rayon |
| `script_engine` | Script interpreter | (none, pure Rust) |
| `dll_injector` | DLL injection | snapshot, windows |

See [Cargo.toml](Cargo.toml) for complete feature list.

## ๐Ÿ“– Examples

Explore the `examples/` directory for usage demonstrations:

### Memory Manager Examples

```bash
# General memory manager usage
cargo run --example memory_manager_example --features "memory_manager"
```

### Process & Window Management

```bash
# Process manager example
cargo run --example process_manager_example --features "process"

# Custom initialization flags
cargo run --example custom_init_flags --features "process"

# Window activation instruction
cargo run --example active_instruction --features "scripts_window"
```

### Screen Capture

```bash
# DXGI capture example
cargo run --example dxgi_capture --features "dxgi"

# Performance comparison
cargo run --example dxgi_performance_comparison --features "dxgi"
```

### Script Engine

```bash
# Script engine with built-in instructions
cargo run --example script_engine --features "script_engine,scripts_builtin"
```

### Color Operations

```bash
# Color conversion utilities
cargo run --example color_conversion --features "color_picker"

# Color finder re-exports
cargo run --example color_reexports --features "color_finder"
```

### Clipboard

```bash
# Clipboard usage example
cargo run --example clipboard_usage --features "clipboard"
```

### AOB Scanning

```bash
# 64-bit bytecode AOB scanning
cargo run --example aobscan_64bit_bytecode --features "memory_aobscan"
```

### DLL Injection

```bash
# DLL injection example
cargo run --example dll_injection --features "dll_injector"
```

## ๐Ÿงช Testing

Run tests for specific modules:

```bash
# Test script engine
cargo test --features "script_engine"

# Test built-in instructions
cargo test --features "scripts_builtin"

# Test memory operations
cargo test --features "memory"
```