Accessing C struct members from external_call
Does Mojo's Foreign Function Interface already support calling C functions that return C structs? If so, how to access the struct's members? Isaiah Norton's call_div example (https://github.com/ihnorton/mojo-ffi/blob/ffi-demo/mojo-call-c/call_div.mojo) seems to show it is possible, but I'm unable to get the value of the latter member in Mojo 24.2.1 although running the same code. The value of the first member seems to return correctly.
GitHub
mojo-ffi/mojo-call-c/call_div.mojo at ffi-demo · ihnorton/mojo-ffi
Contribute to ihnorton/mojo-ffi development by creating an account on GitHub.
5 Replies
Greeeaaaat question! Unfortunately the docs don’t have nearly any info but I believe the repo may have something.
GitHub
mojo/stdlib/src/sys/ffi.mojo at main · modularml/mojo
The Mojo Programming Language. Contribute to modularml/mojo development by creating an account on GitHub.
You may want to try DLHandle instead
Thanks a lot! I have tried both DLHandle and external_call, but cannot get either one work. A good idea to check the source code of the ffi now that the std library is OS. I tried to read it through, but my grasp of mlir and C and low-level stuff is non-existant, so I'm afraid I was not able to find the answer to the question yet. I'm sure it must be possible though – it's hard to believe that the CPython integration could work with limited interoperability.
For the record, here's how I tested the localeconv function of the C standard library. I only manage to print the
decimal_point
member all the other members are blank.
Sorry I couldn’t help 😅
@sora Any thoughts here?