_ctypes pt. 3#5530
Conversation
ab1f597 to
856bbd5
Compare
February 27, 2025 18:15
|
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");
} |
Sorry, something went wrong.
|
Nevermind, it seems like return type needs to be manually specified and the default is cint |
Sorry, something went wrong.
|
Ready for review. (this is from the extra test) libc = cdll.msvcrt
print("rand", libc.rand())/cc @youknowone |
Sorry, something went wrong.
3115d8a to
06dbae4
Compare
February 28, 2025 04:59
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>
youknowone
left a comment
There was a problem hiding this comment.
Looks good, thank you so much!
Please check if the extra indent is intended or not.
Sorry, something went wrong.
|
Done. |
Sorry, something went wrong.
youknowone
left a comment
There was a problem hiding this comment.
👍
Sorry, something went wrong.
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 thecvoidit'll return (unless we don't have to do that for some reason).CFuncPtrCFuncPtrthat 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_intand cannot be changedimport ctypesfails