microsoft / windows-rs

Rust for Windows
https://kennykerr.ca/rust-getting-started/
Apache License 2.0
10.46k stars 494 forks source link

Feature request: generate types for api functions #1868

Closed kgv closed 2 years ago

kgv commented 2 years ago

Motivation

Generate types for api functions will be useful for dynamic exploration. For example when using GetModuleHandleW and GetProcAddress. GetProcAddress return FARPROC which we need to transmute into a function. To do this, we need to know the type of the function.

Drawbacks

Perhaps there is some way to get function pointer type from function pointer. But I don't know this.

Rationale and alternatives

A similar macro solves the problem, but not for the case with static variable. For statics, you must explicitly specify the type.

pub macro export {
    ($module:expr, $target:ty) => {
        GetProcAddress($module, stringify!($target)).map(|source| transmute::<_, $target>(source))
    },
    ($module:expr, $target:ident) => {
        GetProcAddress($module, stringify!($target)).map(|source| {
            #[allow(unused_assignments)]
            let mut target = &$target;
            target = transmute(source);
            *target
        })
    },
}

Additional context

See func-types feature in ntapi (Implementation).

kennykerr commented 2 years ago

Yep, I'd like to be able to generate these but a little concerned about the explosion in crate size. Will think about it some more.

Zerowalker commented 2 years ago

out of interest, what size differences are we talking about, 20%, 100%, 500%?

kennykerr commented 2 years ago

Code size, build time, maintainability. All factors to consider. There are a lot of API functions in the windows crate. I just have to try it and see how practical it is.

kennykerr commented 2 years ago

While this would be convenient for experimenting, I don't think there is enough demand for such a capability to warrant including all the extra generated code needed to make it happen.

mzdk100 commented 10 months ago

I want to know how to convert the FARPROC type obtained from GetProcAddress to the HOOKPROC type.