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
//! DLL Injection Module
//!
//! Provides functionality to inject and unload DLLs into remote processes,
//! and call exported functions from injected DLLs.
//! Supports cross-architecture injection (x64 injector → x86/x64 target).
//!
//! # Architecture Compatibility
//! | Injector | Target Process | DLL Architecture | Status |
//! |----------|---------------|------------------|--------|
//! | x64 | x64 | x64 | ✅ Supported |
//! | x86 | x86 | x86 | ✅ Supported |
//! | x64 | x86 (WOW64) | x86 | ✅ Supported |
//! | x86 | x64 | x64 | ❌ Not possible |
//!
//! # Quick Start
//!
//! ## Basic DLL Injection
//! ```no_run
//! use win_auto_utils::dll_injector::inject_dll;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Inject a DLL into a process by PID
//! inject_dll(12345, "C:\\path\\to\\library.dll")?;
//! Ok(())
//! }
//! ```
//!
//! ## Call Exported Functions
//! ```no_run
//! use win_auto_utils::dll_injector::{
//! get_exported_function_address,
//! call_function_with_raw_bytes
//! };
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Get function address
//! let func_addr = get_exported_function_address(12345, "game_mod.dll", "set_health")?;
//!
//! // Call with i32 parameter (999 health)
//! let health: i32 = 999;
//! let param_bytes = health.to_ne_bytes();
//! let result = call_function_with_raw_bytes(12345, func_addr, Some(¶m_bytes))?;
//!
//! println!("Function returned: {}", result);
//! Ok(())
//! }
//! ```
//!
//! # Module Structure
//!
//! This module is organized into specialized submodules for clarity and maintainability:
//!
//! - **`core`**: Core injection/unloading logic ([`inject_dll`], [`unload_dll`])
//! - **`remote_call`**: Remote function calling APIs ([`get_exported_function_address`], [`call_function_with_raw_bytes`], [`call_function_no_params`])
//! - **`diagnostic`**: Diagnostic tools ([`diagnose_injection`])
//! - **`shellcode`**: Architecture-specific shellcode generators (x86/x64)
//!
//! # Public API Overview
//!
//! ## Core Functions
//!
//! ### `inject_dll(pid, dll_path)`
//! Injects a DLL into the target process using LoadLibraryW + CreateRemoteThread.
//! This is the primary entry point for DLL injection workflows.
//!
//! **Common Use Cases**:
//! - Loading game mods
//! - Process instrumentation
//! - Debugging and profiling
//!
//! ### `unload_dll(pid, module_name)`
//! Unloads a DLL from the target process using FreeLibrary + CreateRemoteThread.
//! Allows cleanup and dynamic unloading of injected modules.
//!
//! **Note**: May not work for system proxy DLLs (version.dll, etc.) due to reference counting.
//! For proxy DLLs, prefer configuration switches over unloading.
//!
//! ### `get_exported_function_address(pid, module_name, function_name)`
//! Resolves the virtual address of an exported function in a remote DLL.
//! Enables calling arbitrary functions without hardcoding addresses.
//!
//! **Implementation**: Uses shellcode to call GetProcAddress in target process.
//!
//! ### `call_function_with_raw_bytes(pid, function_address, param_data)`
//! Calls a remote function by passing raw byte parameters.
//! Most flexible API - supports any parameter type via serialization.
//!
//! **Parameter Types**: i8/i16/i32/i64/u8/u16/u32/u64/f32/f64/structs (via .to_ne_bytes())
//!
//! ### `call_function_no_params(pid, function_address)`
//! Calls a remote function that takes NO parameters.
//! Generates simpler and more efficient shellcode than the generic version.
//!
//! **Use Case**: For parameterless functions like `void reset_stats()`.
//!
//! ## Diagnostic Functions
//!
//! ### `diagnose_injection(pid)`
//! Performs feasibility checks before attempting injection.
//! Returns detailed report with process accessibility, memory allocation, and write permissions.
//!
//! **Usage Frequency**: Low - Only used during development/debugging.
//! **Returns**: `DiagnosticReport` with fields: process_accessible, kernel32_loaded, memory_allocatable, memory_writable.
//!
//! # Performance Characteristics
//! - Typical injection time: 50-200ms (depends on target process)
//! - Function call time: 5-30ms per call
//! - Memory overhead: ~4KB per injection (freed after completion)
//! - Shellcode size: 40-120 bytes (architecture dependent)
//!
//! # FAQ
//!
//! ## Q: Injection fails with "Access Denied"
//! A: Run your program as Administrator. Right-click → "Run as administrator"
//!
//! ## Q: How do I know if my DLL is x86 or x64?
//! A: Use the `dumpbin /headers your.dll` command or check compilation target
//!
//! ## Q: Can I inject into protected processes (e.g., games with anti-cheat)?
//! A: Generally no. Anti-cheat systems block CreateRemoteThread and memory writes.
//!
//! ## Q: Module detection shows "not loaded" after successful injection
//! A: Possible causes:
//! - DllMain returned FALSE (initialization failed)
//! - Missing dependencies prevented loading
//! - Anti-injection protection blocked loading
//! - Debug version called AllocConsole (crashes protected processes)
//!
//! Check Windows Event Viewer for application errors.
//!
//! # Feature Flag
//! Enable with: `--features "dll_injector"`
// Module declarations
// Re-export public API
pub use ;
pub use diagnose_injection;
pub use ;
use fmt;
/// DLL injection error types