-
Notifications
You must be signed in to change notification settings - Fork 1.3k
_ctypes pt. 3 #5530
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
_ctypes pt. 3 #5530
Conversation
ab1f597
to
856bbd5
Compare
Some updates: I've gotten arbitrary function argument passing to work on a test, I still am figuring out return type inference however. use std::ffi::c_void;
use std::path::Path;
use libffi::middle as libffi;
use libloading::Symbol;
type FP = unsafe extern "C" fn ();
fn main() {
let path = "C:\\Windows\\System32\\kernel32.dll";
let path = Path::new(path);
let lib = unsafe { libloading::Library::new(path) }.unwrap();
let function_bytes = b"Sleep\0";
let function: Symbol<FP> = unsafe { lib.get(function_bytes) }.unwrap();
let function = *function;
let args = vec![libffi::Type::u32()];
let cif = libffi::Cif::new(args.into_iter(), libffi::Type::void());
let ms_wait: u32 = 5000;
println!("Calling function");
let now = std::time::Instant::now();
let _res: *const c_void = unsafe { cif.call(libffi::CodePtr(function as *mut _), &[libffi::arg(&ms_wait)]) };
println!("Function returned in {}ms", now.elapsed().as_millis());
println!("Function called");
}
|
Nevermind, it seems like return type needs to be manually specified and the default is cint |
Ready for review. (this is from the extra test) libc = cdll.msvcrt
print("rand", libc.rand()) /cc @youknowone |
3115d8a
to
06dbae4
Compare
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you so much!
Please check if the extra indent is intended or not.
Done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com>
Here comes the meat of the code and the hardest part implementation-wise.
The main goal is to implement (atleast partially)
CFuncPtr
. By partially, I mean that not supporting arguments to functions is probably fine. At any rate the cpython implementation can be used as a reference since they use libffi. The main challenge is determining the return type of the loaded function from thecvoid
it'll return (unless we don't have to do that for some reason).CFuncPtr
CFuncPtr
that encapsulated the entire thingThe next pr can probably just do some cleanup on this and get the basics for struct and union return types working.
Known issues
c_int
and cannot be changedimport ctypes
fails