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
use syn::__private::{TokenStream, quote::quote};

#[proc_macro_attribute]
pub fn proxy(_attribute: TokenStream, function: TokenStream) -> TokenStream {
    let fn_ts = function.clone();
    let item: syn::Item = syn::parse_macro_input!(fn_ts);
    if let syn::Item::Fn(function) = item {
        let syn::ItemFn {
            attrs,
            block,
            vis,
            sig:
                syn::Signature {
                    ident,
                    unsafety,
                    constness,
                    abi,
                    output,
                    ..
                },
            ..
        } = function;

        let output = quote!(
            #(#attrs)*
            #vis #unsafety #abi #constness fn #ident() #output #block

            //turn the original function into DllMain
            #[no_mangle]
            #[allow(non_snake_case)]
            pub extern "system" fn DllMain(
                _hinstDLL: proxy_dll::HINSTANCE,
                fdwReason: proxy_dll::DWORD,
                _lpvReserved: proxy_dll::LPVOID,
            ) -> proxy_dll::BOOL {

                if fdwReason == proxy_dll::DLL_PROCESS_ATTACH {
                    //call the original function
                    proxy_dll::exports::initialize(_hinstDLL).unwrap_or_else(|e| {
                        ::std::panic!("{}", e);
                    });
                    #ident();
                }
                proxy_dll::TRUE
            }
            
        );

        return output.into();
    }

    function
}