ffi

Python: SWIG vs ctypes

In python, under what circumstances is SWIG a better choice than ctypes for calling entry points in shared libraries? Let's assume you don't already have the SWIG interface file(s). What are the performance metrics of the two? ...

How do you do ALSA in Mono?

Does anyone know how to access ALSA (low-level audio API) in Linux using Mono? NOTE: I'm hoping to access ALSA using PInvoke. ...

Haskell FFI / C MPFR library wrapper woes

In order to create an arbitrary precision floating point / drop in replacement for Double, I'm trying to wrap MPFR using the FFI but despite all my efforts the simplest bit of code doesn't work. It compiles, it runs, but it crashes mockingly after pretending to work for a while. A simple C version of the code happily prints the number "1...

How do you return a variable in a C -> ruby interface?

A follow up to an earlier question, showing the part that fails when I try to get the error message from my target library: require 'gt4r' @@test_environment = "INCLUDE=C:\\graphtalk\\env\\aiadev\\config\\aiadev.ini" @@normal_user = "BMCHARGUE" describe Gt4r do it 'initializes' do rv = Gt4r.gTD_initialize @@normal_user, @@norm...

How do I use Haskell's FFI on structs?

I have created the following C library for reading an image: typedef struct { unsigned int height; unsigned int width; unsigned char* red; //length=height*width unsigned char* green; unsigned char* blue; } Contents; Contents readJPEGFile(const char* inFilename); I can't really find any info using arrays and struc...

Haskell binding with Ruby through FFI ?

Since both ruby and Haskell supports FFI, Is it possible to call Haskell code from ruby, may be through FFI ? Is there any Haskell binding in Ruby ? ...

Ruby Noobie: How to set a string value in an FFI Struct

I'm having some beginner problems setting an FFI struct in Ruby. What I want to do is pass a pointer to a C string by setting a string property in an FFI::Struct object: class SpSessionConfig < FFI::Struct layout :api_version, :int, :cache_location, :string, :settings_location, :string, ...

Haskell FFI: Calling FunPtrs

Here's my situation: I would like to call ffmpeg's av_free_packet function: // avformat.h static inline void av_free_packet(AVPacket *pkt) { if (pkt && pkt->destruct) pkt->destruct(pkt); } But unfortunately this function is static inline, and so doesn't really appear in the linked library. However, it is a very simple function...

C: better way to do sizeof(((SomeStruct *) 0)->some_member) ?

I want to get the size of a specific member in a struct. sizeof(((SomeStruct *) 0)->some_member) works for me but I feel like there might be a nicer way to do it. I could #define SIZEOF_ELEM(STRUCT, ELEM) sizeof(((STRUCT *) 0)->ELEM) and then use SIZEOF_ELEM(SomeStruct, some_member), but I wonder whether there is already something bett...

Haskell FFI: ForeignPtr seems not to get freed (maybe a GHC bug?)

Consider the following code snippet import qualified Foreign.Concurrent import Foreign.Ptr (nullPtr) main :: IO () main = do putStrLn "start" a <- Foreign.Concurrent.newForeignPtr nullPtr $ putStrLn "a was deleted" putStrLn "end" It produces the following output: start end I would had expected to see "a was deleted" some...

Haskell: How do I get the values of #define-d constants?

In a Haskell program, what's the best way to use constants defined in C headers? ...

Generate C wrapper from C++?

Hi, I want to generate C wrappers from C++ libraries. There are tutorials on how to do it by hand: http://developers.sun.com/solaris/articles/mixing.html http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html http://yosefk.com/c++fqa/mixing.html But it is too much of a manual labor. For example, for this: struct RtAudio { ...

Guides for implementing a foreign function interface

Right now I'm working on a scripting language that doesn't yet have a FFI. I'd like to know what's the most convenient way to get it in, assuming that I'd like to write it like cool geeks do - I'd like to write the FFI in the scripting language itself. The programming language I need to interface is C. So for basics I know that libdl.so...

How efficient is SBCL for storing big graphs?

How much does the garbage collector affect performance when working with lots of objects in memory, how big is the memory allocation and dealocation overhead? Is it wise to use SBCL to do this or is better to build a small C library to connect trough FFI? ...

How to dereference a memory location from python ctypes?

I want to replicate the following c code in python ctypes: main() { long *ptr = (long *)0x7fff96000000; printf("%lx",*ptr); } I can figure out how to call this memory location as a function pointer but not just do a normal dereference: from ctypes import * """ >>> fptr = CFUNCTYPE(None, None) Traceback (most recent call last): ...

Calling mysql_real_escape_string using the PLT-Scheme Foreign Function Interface

Hi, using the PLT-Scheme-FFI, I want to call the C-function unsigned long mysql_real_escape_string(MYSQL *con, char *to, const char *from, unsigned long length) from a scheme procedure and continue using the resulting string 'to' inside the caller. The call of the scheme procedure would go like this: (define-values (to) (escape-str...

Are peekCString and peekCStringLen lazy?

I have a C function that creates a null terminated string and returns a pointer to it, there is also corresponding deallocation function. foreign import ccall unsafe "get_str" getStr :: IO CString foreign import ccall unsafe "free_str" freeStr :: CString -> IO () I want to create a Haskell String from the returned CString, and free CS...

Error running sqlite3 on ruby 1.9: undefined method 'changes'

Here is the error I'm getting. It just started out of the blue undefined method `changes' for #<SQLite3::Driver::FFI::Driver:0xa75235c> I'm using ruby 1.9.1p243 gem 1.3.5 rails 2.3.5 ffi 0.5.4 sqlite3 0.0.3 On ubuntu 9.10 desktop, but i've had the same problem 9.04 Anyone know what's causing this error? ...

importing c++ data types to haskell with ffi

hi, I'm writing a haskell wrapper for a c++ library and as much as I can import functions from the library to my haskell program, I have no clue how to import c++ data types. For instance I have a function which takes as a parameter a video::E_DRIVER_TYPE EDT_OPENGL type defined in some.h file, and as I said before I know how to import...

Using _bitmask from PltScheme FFI

This is a part of a plt-scheme wrapper library: (define InputMask (_bitmask '(NoEventMask = #x00000000 KeyPressMask = #x00000001 KeyReleaseMask = #x00000002 ... OwnerGrabButtonMask = #x01000000) _long)) The thing is I cant figure out ho...