ffi

Should I pass a mutable reference or transfer ownership of a variable in the context of FFI?

假装没事ソ 提交于 2020-01-14 05:17:28
问题 I have a program that utilizes a Windows API via a C FFI (via winapi-rs). One of the functions expects a pointer to a pointer to a string as an output parameter. The function will store its result into this string. I'm using a variable of type WideCString for this string. Can I "just" pass in a mutable ref to a ref to a string into this function (inside an unsafe block) or should I rather use a functionality like .into_raw() and .from_raw() that also moves the ownership of the variable to the

Passing a Rust variable to a C function that expects to be able to modify it

这一生的挚爱 提交于 2020-01-12 09:14:09
问题 I'm writing a safe Rust layer with which I can call functions from a C library in Rust. I've generated the unsafe bindings using rust-bindgen, but I'm getting a little confused on the differences between how Rust and C work with regards to passing pointers. The C function looks like this: bool imeGet(unsigned char address, int *value); It reads an I2C sensor at address , stores the result in value , and returns TRUE on success. Bindgen has the Rust function looking like this: pub fn imeGet

In C, given a variable list of arguments, how to build a function call using them?

99封情书 提交于 2020-01-08 16:33:11
问题 Suppose there's a list of arguments stored somehow, in a array for example. Given a function pointer , how could I make a call to it passing the stored list of arguments? I'm not trying to pass the array as an argument ok. You got it, ok? I want to pass each of its elements as an argument. An array is just to illustrate, I could be storing the arguments in some tuple structure. Also, look that I have at hand a function pointer and may have a signature in string format . I'm not trying to just

How to link a dynamic Rust library using only rustc and not cargo?

廉价感情. 提交于 2020-01-06 06:12:31
问题 My main.rs looks like // #[link(name = "lib")] extern "C" { fn hello(); } fn main() { unsafe { hello(); } } And lib.rs : #[no_mangle] pub fn hello() { println!("Hello, World!"); } I have compiled lib.rs using rustc --crate-type=cdylib lib.rs -o lib.so How do I link lib.so to rustc main.rs command ? 回答1: You need to match ABIs. When you use an extern "C" block, you need to declare your functions using the same ABI. Name your dynamic library using the platform's conventions. Use .dylib on macOS

How to link a dynamic Rust library using only rustc and not cargo?

孤者浪人 提交于 2020-01-06 06:11:48
问题 My main.rs looks like // #[link(name = "lib")] extern "C" { fn hello(); } fn main() { unsafe { hello(); } } And lib.rs : #[no_mangle] pub fn hello() { println!("Hello, World!"); } I have compiled lib.rs using rustc --crate-type=cdylib lib.rs -o lib.so How do I link lib.so to rustc main.rs command ? 回答1: You need to match ABIs. When you use an extern "C" block, you need to declare your functions using the same ABI. Name your dynamic library using the platform's conventions. Use .dylib on macOS

Access violation in GHCI

不问归期 提交于 2020-01-06 04:37:14
问题 I have a Haskell program using the FFI to import C++ functions. I'm using Windows. When I compile to an executable or to a DLL, this works. But when I load a module in GHCI, this doesn't always work: sometimes this works, sometimes I get Access violation in generated code when reading 0000000000000000 whenever I call a function from the module (even if this function has nothing to do with the C++). Do you know how to prevent that? 来源: https://stackoverflow.com/questions/47849032/access

Is using the type `[my_struct]` the correct way of passing an array of C structs to a Rust function?

社会主义新天地 提交于 2020-01-05 05:46:04
问题 C file: typedef struct point { int x; int y; } point; typedef struct points { int count; point *array_of_points; } points; Rust file: #[derive(Debug)] #[repr(C)] pub struct point { x: c_int, y: c_int, } #[derive(Debug)] #[repr(C)] pub struct points { count: c_int, array_of_points: [point], } #[no_mangle] pub fn do_something(all_points: &points) { for i in 0..all_points.count { let crr_point = &all_points.array_of_points[i as usize]; println!("{:?}", crr_point); } } In my C file, I allocate a

Programmatically Listing `C-sources` in Haskell Cabal Files

社会主义新天地 提交于 2020-01-05 05:44:06
问题 I am involved in a Haskell project which involves a lot of C FFI. Thus, for every src/HaskellFile.hs I have in my project, I have a corresponding src/HaskellFile.c C source file. This means I have to manually this all of these C sources in my cabal project: C-sources: src/HaskellFile1.c, src/HaskellFile2.c ...and so forth. Question: Is it possible to programmatically list out these files in my cabal project file? Something like: C-sources: src/*.c ..? (For the record, I tried the above and it

LuaJIT, how to convert cdata to userdata?

不打扰是莪最后的温柔 提交于 2020-01-04 14:09:03
问题 I want to use LuaJIT for its ability to create structs and arrays in Lua. But my functions which use the data require userdata or a string (not a string representation, just used as a container) that stores the data. But looking through the api I don't see if this is even possible. Is it? Thanks. 回答1: LuaJIT FFI should not be mixed with classic C/API. While there are mechanisms to convert a const char* pointer to Lua string ( ffi.string ), there is no way to convert an FFI struct to Lua

LuaJit FFI Return string from C function to Lua?

廉价感情. 提交于 2020-01-04 05:20:34
问题 Say I have this C function: __declspec(dllexport) const char* GetStr() { static char buff[32] // Fill the buffer with some string here return buff; } And this simple Lua module: local mymodule = {} local ffi = require("ffi") ffi.cdef[[ const char* GetStr(); ]] function mymodule.get_str() return ffi.C.GetStr() end return mymodule How can I get the returned string from the C function as a Lua string here: local mymodule = require "mymodule" print(mymodule.get_str()) 回答1: The ffi.string function