Maple

Struct Maple 

Source
pub struct Maple;
Expand description

Main entry point for the maple-rs library.

Provides convenient static methods for loading executables and libraries from memory buffers.

Implementations§

Source§

impl Maple

Source

pub fn load_library_from_memory(data: &[u8]) -> Result<Box<dyn MemoryModule>>

Loads a DLL/library from a memory buffer.

§Arguments
  • data - Raw bytes of the PE file to load
§Returns

Returns a MemoryModule trait object that can be used to interact with the loaded library, including getting function addresses.

§Example
use maple_rs::Maple;
use std::fs;

let dll_data = fs::read("library.dll").unwrap();
let module = Maple::load_library_from_memory(&dll_data).unwrap();
let func = module.get_proc_address("MyFunction").unwrap();
§Errors

Returns MapleError if:

  • The PE format is invalid
  • Memory allocation fails
  • Import resolution fails
  • Platform is not supported
Examples found in repository?
examples/load_demo.rs (line 50)
5fn main() -> Result<()> {
6    let demo_path = Path::new("test/demo.exe");
7    let dll_path = Path::new("test/makcu-cpp.dll");
8    let focus_dll_path = Path::new("test/focus/focus.dll");
9
10    if !demo_path.exists() {
11        eprintln!("demo.exe not found in test directory");
12        return Ok(());
13    }
14
15    if !dll_path.exists() {
16        eprintln!("makcu-cpp.dll not found in test directory");
17        return Ok(());
18    }
19
20    println!("First, copy makcu-cpp.dll to current directory so demo.exe can find it...");
21    if dll_path.exists() {
22        let _ = fs::copy(dll_path, "makcu-cpp.dll");
23    }
24
25    println!("Loading demo.exe from memory...");
26    let demo_data = fs::read(demo_path)?;
27
28    println!("Creating memory module...");
29    match Maple::load_executable_from_memory(&demo_data) {
30        Ok(module) => {
31            println!("Successfully loaded demo.exe into memory");
32            println!("Base address: {:p}", module.base_address());
33            println!("Size: {} bytes", module.size());
34            println!("Is loaded: {}", module.is_loaded());
35
36            println!("Executing entry point...");
37            match module.execute_entry_point() {
38                Ok(_) => println!("Entry point executed successfully"),
39                Err(e) => eprintln!("Failed to execute entry point: {}", e),
40            }
41        }
42        Err(e) => {
43            eprintln!("Failed to load demo.exe: {}", e);
44        }
45    }
46
47    println!("\nLoading makcu-cpp.dll from memory...");
48    let dll_data = fs::read(dll_path)?;
49
50    match Maple::load_library_from_memory(&dll_data) {
51        Ok(module) => {
52            println!("Successfully loaded makcu-cpp.dll into memory");
53            println!("Base address: {:p}", module.base_address());
54            println!("Size: {} bytes", module.size());
55            println!("Is loaded: {}", module.is_loaded());
56        }
57        Err(e) => {
58            eprintln!("Failed to load makcu-cpp.dll: {}", e);
59        }
60    }
61
62    // Demo application DLL loading if focus.dll exists
63    if focus_dll_path.exists() {
64        use std::env;
65
66        println!("\nTesting application DLL loading with focus.dll...");
67
68        // Change to focus directory temporarily for dependency resolution
69        let current_dir = env::current_dir().expect("Failed to get current directory");
70        let focus_dir = focus_dll_path.parent().unwrap();
71        env::set_current_dir(focus_dir).expect("Failed to change to focus directory");
72
73        let focus_dll_data = fs::read(focus_dll_path)?;
74        match Maple::load_application_dll_from_memory(&focus_dll_data) {
75            Ok(module) => {
76                println!("Successfully loaded focus.dll as application DLL");
77                println!("Base address: {:p}", module.base_address());
78                println!("Size: {} bytes", module.size());
79
80                println!("Executing application DLL (will run for 3 seconds)...");
81                match module.execute_dll_application() {
82                    Ok(_) => {
83                        println!("Application DLL started successfully!");
84                        std::thread::sleep(std::time::Duration::from_secs(3));
85                    }
86                    Err(e) => eprintln!("Failed to execute application DLL: {}", e),
87                }
88            }
89            Err(e) => {
90                eprintln!("Failed to load focus.dll as application DLL: {}", e);
91            }
92        }
93
94        // Restore working directory
95        env::set_current_dir(&current_dir).expect("Failed to restore working directory");
96    }
97
98    // Clean up the copied DLL
99    if Path::new("makcu-cpp.dll").exists() {
100        println!("\nCleaning up copied DLL...");
101        if let Err(e) = fs::remove_file("makcu-cpp.dll") {
102            eprintln!("Warning: Failed to clean up makcu-cpp.dll: {}", e);
103        } else {
104            println!("Cleaned up makcu-cpp.dll");
105        }
106    }
107
108    Ok(())
109}
Source

pub fn load_executable_from_memory(data: &[u8]) -> Result<Box<dyn MemoryModule>>

Loads an executable from a memory buffer.

§Arguments
  • data - Raw bytes of the PE executable to load
§Returns

Returns a MemoryModule trait object that can be used to execute the program’s entry point.

§Example
use maple_rs::Maple;
use std::fs;

let exe_data = fs::read("program.exe").unwrap();
let module = Maple::load_executable_from_memory(&exe_data).unwrap();
module.execute_entry_point().unwrap();
§Errors

Returns MapleError if:

  • The PE format is invalid
  • Memory allocation fails
  • Import resolution fails
  • Platform is not supported
Examples found in repository?
examples/load_focus.rs (line 54)
5fn main() -> Result<()> {
6    let focus_path = Path::new("test/focus/focus.exe");
7
8    if !focus_path.exists() {
9        eprintln!("focus.exe not found in test/focus directory");
10        return Ok(());
11    }
12
13    // Check if all the required DLLs are present in the focus directory
14    let dll_files = [
15        "abseil_dll.dll",
16        "jpeg62.dll",
17        "libcrypto-3-x64.dll",
18        "liblzma.dll",
19        "libpng16.dll",
20        "libprotobuf.dll",
21        "libsharpyuv.dll",
22        "libwebp.dll",
23        "libwebpdecoder.dll",
24        "opencv_core4.dll",
25        "opencv_dnn4.dll",
26        "opencv_highgui4.dll",
27        "opencv_imgcodecs4.dll",
28        "opencv_imgproc4.dll",
29        "opencv_videoio4.dll",
30        "tiff.dll",
31        "zlib1.dll",
32    ];
33
34    println!("Copying required DLLs to current directory...");
35    for dll in &dll_files {
36        let dll_path = Path::new("test/focus").join(dll);
37        if dll_path.exists() {
38            if let Err(e) = fs::copy(&dll_path, dll) {
39                eprintln!("Warning: Failed to copy {}: {}", dll, e);
40            } else {
41                println!("Copied {}", dll);
42            }
43        } else {
44            eprintln!("Warning: {} not found", dll);
45        }
46    }
47
48    println!("\nLoading focus.exe from memory...");
49    let focus_data = fs::read(focus_path)?;
50
51    println!("Creating memory module...");
52    println!("Focus.exe size: {} bytes", focus_data.len());
53
54    match Maple::load_executable_from_memory(&focus_data) {
55        Ok(module) => {
56            println!("Successfully loaded focus.exe into memory");
57            println!("Base address: {:p}", module.base_address());
58            println!("Size: {} bytes", module.size());
59            println!("Is loaded: {}", module.is_loaded());
60
61            println!("\nExecuting GUI application entry point...");
62            println!("Note: This will launch the focus.exe GUI application from memory!");
63
64            match module.execute_entry_point() {
65                Ok(_) => println!("Entry point executed successfully (GUI should have appeared)"),
66                Err(e) => eprintln!("Failed to execute entry point: {}", e),
67            }
68        }
69        Err(e) => {
70            eprintln!("Failed to load focus.exe: {}", e);
71        }
72    }
73
74    // Clean up copied DLLs
75    println!("\nCleaning up copied DLLs...");
76    for dll in &dll_files {
77        if Path::new(dll).exists() {
78            let _ = fs::remove_file(dll);
79        }
80    }
81
82    Ok(())
83}
More examples
Hide additional examples
examples/load_demo.rs (line 29)
5fn main() -> Result<()> {
6    let demo_path = Path::new("test/demo.exe");
7    let dll_path = Path::new("test/makcu-cpp.dll");
8    let focus_dll_path = Path::new("test/focus/focus.dll");
9
10    if !demo_path.exists() {
11        eprintln!("demo.exe not found in test directory");
12        return Ok(());
13    }
14
15    if !dll_path.exists() {
16        eprintln!("makcu-cpp.dll not found in test directory");
17        return Ok(());
18    }
19
20    println!("First, copy makcu-cpp.dll to current directory so demo.exe can find it...");
21    if dll_path.exists() {
22        let _ = fs::copy(dll_path, "makcu-cpp.dll");
23    }
24
25    println!("Loading demo.exe from memory...");
26    let demo_data = fs::read(demo_path)?;
27
28    println!("Creating memory module...");
29    match Maple::load_executable_from_memory(&demo_data) {
30        Ok(module) => {
31            println!("Successfully loaded demo.exe into memory");
32            println!("Base address: {:p}", module.base_address());
33            println!("Size: {} bytes", module.size());
34            println!("Is loaded: {}", module.is_loaded());
35
36            println!("Executing entry point...");
37            match module.execute_entry_point() {
38                Ok(_) => println!("Entry point executed successfully"),
39                Err(e) => eprintln!("Failed to execute entry point: {}", e),
40            }
41        }
42        Err(e) => {
43            eprintln!("Failed to load demo.exe: {}", e);
44        }
45    }
46
47    println!("\nLoading makcu-cpp.dll from memory...");
48    let dll_data = fs::read(dll_path)?;
49
50    match Maple::load_library_from_memory(&dll_data) {
51        Ok(module) => {
52            println!("Successfully loaded makcu-cpp.dll into memory");
53            println!("Base address: {:p}", module.base_address());
54            println!("Size: {} bytes", module.size());
55            println!("Is loaded: {}", module.is_loaded());
56        }
57        Err(e) => {
58            eprintln!("Failed to load makcu-cpp.dll: {}", e);
59        }
60    }
61
62    // Demo application DLL loading if focus.dll exists
63    if focus_dll_path.exists() {
64        use std::env;
65
66        println!("\nTesting application DLL loading with focus.dll...");
67
68        // Change to focus directory temporarily for dependency resolution
69        let current_dir = env::current_dir().expect("Failed to get current directory");
70        let focus_dir = focus_dll_path.parent().unwrap();
71        env::set_current_dir(focus_dir).expect("Failed to change to focus directory");
72
73        let focus_dll_data = fs::read(focus_dll_path)?;
74        match Maple::load_application_dll_from_memory(&focus_dll_data) {
75            Ok(module) => {
76                println!("Successfully loaded focus.dll as application DLL");
77                println!("Base address: {:p}", module.base_address());
78                println!("Size: {} bytes", module.size());
79
80                println!("Executing application DLL (will run for 3 seconds)...");
81                match module.execute_dll_application() {
82                    Ok(_) => {
83                        println!("Application DLL started successfully!");
84                        std::thread::sleep(std::time::Duration::from_secs(3));
85                    }
86                    Err(e) => eprintln!("Failed to execute application DLL: {}", e),
87                }
88            }
89            Err(e) => {
90                eprintln!("Failed to load focus.dll as application DLL: {}", e);
91            }
92        }
93
94        // Restore working directory
95        env::set_current_dir(&current_dir).expect("Failed to restore working directory");
96    }
97
98    // Clean up the copied DLL
99    if Path::new("makcu-cpp.dll").exists() {
100        println!("\nCleaning up copied DLL...");
101        if let Err(e) = fs::remove_file("makcu-cpp.dll") {
102            eprintln!("Warning: Failed to clean up makcu-cpp.dll: {}", e);
103        } else {
104            println!("Cleaned up makcu-cpp.dll");
105        }
106    }
107
108    Ok(())
109}
Source

pub fn load_application_dll_from_memory( data: &[u8], ) -> Result<Box<dyn MemoryModule>>

Loads an application DLL from a memory buffer.

Application DLLs are DLLs that contain a main application but are compiled as DLL files instead of executables. They typically start a thread in DllMain that runs the application logic.

§Arguments
  • data - Raw bytes of the PE DLL file to load
§Returns

Returns a MemoryModule trait object that can be used to execute the application DLL using the execute_dll_application method.

§Example
use maple_rs::Maple;
use std::fs;

let dll_data = fs::read("app.dll").unwrap();
let module = Maple::load_application_dll_from_memory(&dll_data).unwrap();
module.execute_dll_application().unwrap();
§Errors

Returns MapleError if:

  • The PE format is invalid
  • Memory allocation fails
  • Import resolution fails
  • Platform is not supported
Examples found in repository?
examples/test_application_dll.rs (line 20)
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}
More examples
Hide additional examples
examples/load_demo.rs (line 74)
5fn main() -> Result<()> {
6    let demo_path = Path::new("test/demo.exe");
7    let dll_path = Path::new("test/makcu-cpp.dll");
8    let focus_dll_path = Path::new("test/focus/focus.dll");
9
10    if !demo_path.exists() {
11        eprintln!("demo.exe not found in test directory");
12        return Ok(());
13    }
14
15    if !dll_path.exists() {
16        eprintln!("makcu-cpp.dll not found in test directory");
17        return Ok(());
18    }
19
20    println!("First, copy makcu-cpp.dll to current directory so demo.exe can find it...");
21    if dll_path.exists() {
22        let _ = fs::copy(dll_path, "makcu-cpp.dll");
23    }
24
25    println!("Loading demo.exe from memory...");
26    let demo_data = fs::read(demo_path)?;
27
28    println!("Creating memory module...");
29    match Maple::load_executable_from_memory(&demo_data) {
30        Ok(module) => {
31            println!("Successfully loaded demo.exe into memory");
32            println!("Base address: {:p}", module.base_address());
33            println!("Size: {} bytes", module.size());
34            println!("Is loaded: {}", module.is_loaded());
35
36            println!("Executing entry point...");
37            match module.execute_entry_point() {
38                Ok(_) => println!("Entry point executed successfully"),
39                Err(e) => eprintln!("Failed to execute entry point: {}", e),
40            }
41        }
42        Err(e) => {
43            eprintln!("Failed to load demo.exe: {}", e);
44        }
45    }
46
47    println!("\nLoading makcu-cpp.dll from memory...");
48    let dll_data = fs::read(dll_path)?;
49
50    match Maple::load_library_from_memory(&dll_data) {
51        Ok(module) => {
52            println!("Successfully loaded makcu-cpp.dll into memory");
53            println!("Base address: {:p}", module.base_address());
54            println!("Size: {} bytes", module.size());
55            println!("Is loaded: {}", module.is_loaded());
56        }
57        Err(e) => {
58            eprintln!("Failed to load makcu-cpp.dll: {}", e);
59        }
60    }
61
62    // Demo application DLL loading if focus.dll exists
63    if focus_dll_path.exists() {
64        use std::env;
65
66        println!("\nTesting application DLL loading with focus.dll...");
67
68        // Change to focus directory temporarily for dependency resolution
69        let current_dir = env::current_dir().expect("Failed to get current directory");
70        let focus_dir = focus_dll_path.parent().unwrap();
71        env::set_current_dir(focus_dir).expect("Failed to change to focus directory");
72
73        let focus_dll_data = fs::read(focus_dll_path)?;
74        match Maple::load_application_dll_from_memory(&focus_dll_data) {
75            Ok(module) => {
76                println!("Successfully loaded focus.dll as application DLL");
77                println!("Base address: {:p}", module.base_address());
78                println!("Size: {} bytes", module.size());
79
80                println!("Executing application DLL (will run for 3 seconds)...");
81                match module.execute_dll_application() {
82                    Ok(_) => {
83                        println!("Application DLL started successfully!");
84                        std::thread::sleep(std::time::Duration::from_secs(3));
85                    }
86                    Err(e) => eprintln!("Failed to execute application DLL: {}", e),
87                }
88            }
89            Err(e) => {
90                eprintln!("Failed to load focus.dll as application DLL: {}", e);
91            }
92        }
93
94        // Restore working directory
95        env::set_current_dir(&current_dir).expect("Failed to restore working directory");
96    }
97
98    // Clean up the copied DLL
99    if Path::new("makcu-cpp.dll").exists() {
100        println!("\nCleaning up copied DLL...");
101        if let Err(e) = fs::remove_file("makcu-cpp.dll") {
102            eprintln!("Warning: Failed to clean up makcu-cpp.dll: {}", e);
103        } else {
104            println!("Cleaned up makcu-cpp.dll");
105        }
106    }
107
108    Ok(())
109}

Auto Trait Implementations§

§

impl Freeze for Maple

§

impl RefUnwindSafe for Maple

§

impl Send for Maple

§

impl Sync for Maple

§

impl Unpin for Maple

§

impl UnwindSafe for Maple

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.