Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not compile then call original function. #3

Closed
chtotut opened this issue Nov 13, 2017 · 4 comments
Closed

Can not compile then call original function. #3

chtotut opened this issue Nov 13, 2017 · 4 comments

Comments

@chtotut
Copy link

chtotut commented Nov 13, 2017

Hi. I try use detour for ws2_32::send.
But then i try to execute original function from detour closure:

pub(super) type Function = unsafe extern "system" fn(winapi::SOCKET, *const winapi::c_char, winapi::c_int, winapi::c_int) -> winapi::c_int;

static_detours! {
    struct Send: unsafe extern "system" fn(winapi::SOCKET, *const winapi::c_char, winapi::c_int, winapi::c_int) -> winapi::c_int;
}

Send.initialize(
                ws2_32::send,
                move |socket, buffer, length, flags| {
...
let original = Send.get().expect("Get original function fail.");
let result = original.call(
    socket as u64,
    buf.as_ptr() as *const i8,
    buf.len() as i32,
    flags as i32,
);
...

Code does not compiles.
Error:

error[E0599]: no method named `call` found for type `&detour::GenericDetour<unsafe extern "system" fn(u64, *const i8, i32, i32) -> i32>` in the current scope

let result = original.call(
                      ^^^^

note: the method `call` exists but the following trait bounds were not satisfied:
            `&detour::GenericDetour<unsafe extern "system" fn(u64, *const i8, i32, i32) -> i32> : std::ops::Fn<_>`

Where there can be an error?

@darfink
Copy link
Owner

darfink commented Nov 14, 2017

Not quite sure why, try dropping the unsafe modifier in the macro:

static_detours! {
    struct Send: unsafe extern "system" fn(winapi::SOCKET, *const winapi::c_char, winapi::c_int, winapi::c_int) -> winapi::c_int;
}

static_detours! {
    struct Send: extern "system" fn(winapi::SOCKET, *const winapi::c_char, winapi::c_int, winapi::c_int) -> winapi::c_int;
}

@chtotut
Copy link
Author

chtotut commented Nov 14, 2017

ws2_32::send define as unsafe (https://retep998.github.io/doc/ws2_32/fn.send.html).

If delete unsafe modifier in the macro - It throw this error:

Send.initialize(
                      ws2_32::send,
    |                 ^^^^^^^^^^^^ expected normal fn, found unsafe fn
    |
    = note: expected type `extern "system" fn(u64, *const i8, i32, i32) -> i32`
               found type `unsafe extern "system" fn(u64, *const i8, i32, i32) -> i32 {ws2_32::send}`

@darfink
Copy link
Owner

darfink commented Nov 16, 2017

Okay, this should be fixed in latest master.
You can try it now and see if your issue is resolved:

[dependencies]
detour = { git = "https://github.com/darfink/detour-rs", branch = "master" }

@chtotut
Copy link
Author

chtotut commented Nov 17, 2017

Yes, latest master fixed this. Thanks.

@chtotut chtotut closed this as completed Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants