test_application_dll/
test_application_dll.rs

1use maple_rs::{Maple, MemoryModuleBuilder};
2use std::env;
3use std::fs;
4
5fn main() -> maple_rs::Result<()> {
6    println!("Testing focus.dll as an application DLL...");
7
8    // Change to the test/focus directory so dependencies are found
9    let current_dir = env::current_dir().expect("Failed to get current directory");
10    let focus_dir = current_dir.join("test").join("focus");
11    env::set_current_dir(&focus_dir).expect("Failed to change to focus directory");
12    println!("Changed working directory to: {:?}", focus_dir);
13
14    // Read the focus.dll file
15    let dll_data = fs::read("focus.dll").expect("Failed to read focus.dll");
16    println!("Loaded {} bytes from focus.dll", dll_data.len());
17
18    // Test 1: Load as application DLL using the new convenient method
19    println!("\nTest 1: Loading using Maple::load_application_dll_from_memory()...");
20    match Maple::load_application_dll_from_memory(&dll_data) {
21        Ok(module) => {
22            println!("Successfully loaded focus.dll as application DLL!");
23            println!("Base address: {:?}", module.base_address());
24            println!("Size: {} bytes", module.size());
25
26            println!("Executing DLL application...");
27            match module.execute_dll_application() {
28                Ok(()) => {
29                    println!("DLL application started successfully!");
30                    println!("Waiting 5 seconds to let the application run...");
31                    std::thread::sleep(std::time::Duration::from_secs(5));
32                }
33                Err(e) => {
34                    println!("Failed to execute DLL application: {}", e);
35                }
36            }
37        }
38        Err(e) => {
39            println!("Failed to load focus.dll as application DLL: {}", e);
40        }
41    }
42
43    // Test 2: Load using the builder with application DLL settings
44    println!("\nTest 2: Loading using MemoryModuleBuilder with application DLL settings...");
45    match MemoryModuleBuilder::new()
46        .resolve_imports(true)
47        .process_relocations(true)
48        .call_dll_main(false) // Don't auto-call DllMain
49        .is_application_dll(true)
50        .load_from_memory(&dll_data)
51    {
52        Ok(module) => {
53            println!("Successfully loaded focus.dll with builder!");
54            println!("Base address: {:?}", module.base_address());
55            println!("Size: {} bytes", module.size());
56
57            println!("Manually executing DLL application...");
58            match module.execute_dll_application() {
59                Ok(()) => {
60                    println!("DLL application started successfully!");
61                    println!("Waiting 5 seconds to let the application run...");
62                    std::thread::sleep(std::time::Duration::from_secs(5));
63                }
64                Err(e) => {
65                    println!("Failed to execute DLL application: {}", e);
66                }
67            }
68        }
69        Err(e) => {
70            println!("Failed to load focus.dll with builder: {}", e);
71        }
72    }
73
74    // Restore working directory
75    env::set_current_dir(&current_dir).expect("Failed to restore working directory");
76
77    println!("\nTest completed!");
78    Ok(())
79}