Questions tagged [ffi]

A foreign function interface (FFI) is a mechanism for one language to interact with software written in another language. Machine code and assembly code is sometimes called native code when referring to platform-dependent parts of language features or libraries.

Filter by
Sorted by
Tagged with
3 votes
0 answers
79 views

Rust ffi wrapper for barcode scanning library

I'm the maintainer of the zxing-cpp library, a barcode image processing library written in C++. I recently decided to invest in a Rust wrapper library/crate that is now on crates.io. It is basically a ...
axxel's user avatar
  • 147
1 vote
0 answers
44 views

Reading (typed) data from file over FFI with `libc`

This is a solution for a workshop which gave me some problems. This solution also available via git: https://github.com/skaunov/6991_ws09/. After the code there also couple of theory questions which ...
Sergey Kaunov's user avatar
4 votes
1 answer
117 views

C# wrapper that binds to Rust FFI, for the purpose of running CavalierContours functions in Unity

I have attempted to write c# bindings for a Rust library. I have never worked with unmanned code / languages before. Superficially this code does work with no apparent bugs or memory leaks. I want to ...
arcadeperfect's user avatar
3 votes
0 answers
32 views

Unsafe Rust FFI code to access Razer products on macOS

I am new to Rust and specially new to FFI programming, and I am porting a macOS specific C library called librazermacos to Rust. While writing some public APIs I came up with my first two methods ...
kolrie's user avatar
  • 131
5 votes
2 answers
1k views

C++ wrapper to call Python code

I'm wondering if the following Python wrapper for C++ I'm developing is worth expanding upon and including in my portfolio. If so, I plan to add more parameter options and return types. The problem is,...
user3605767's user avatar
3 votes
0 answers
909 views

Return a 2D matrix from a C++ function in Python: ctypes versus pybind11

I found two ways in which I can use a custom C++ function (returning a 2D matrix) in Python. One approach that uses the Python module ctypes and another that uses the Python module pybind11. Approach ...
CafféSospeso's user avatar
3 votes
1 answer
101 views

Trailing delimiter string splicing, implemented for Lua in C

I've been writing with C for a couple days, so this is a relatively advanced function compared to the simple things I've been doing previously. It's written for the Lua API, but it's still C. ...
wellinthatcase's user avatar
2 votes
1 answer
151 views

Writing an x64 Code Emitter to eventually make a full fledged Assembler like NASM

As the title suggests, I'm writing an x64 Code Emitter. Right now I've only encoded 1 instruction (The add instruction). I want to know if this API can be improved at all. This is how you I use it. <...
xKaihatsu's user avatar
2 votes
0 answers
170 views

NativeScript - Angular Service to watch iOS keyboard event listeners

I'm writing an app with NativeScript 6.4.1 and Angular version 8. There are a series of UI events in iOS that you can watch to see what the keyboard is doing: https://developer.apple.com/...
user1261710's user avatar
3 votes
1 answer
2k views

Exposing a simple struct using Rust FFI

I am trying to expose a simple struct via FFI. Is this the best way to do it? I am not 100% sure about the get_text function. This is taking the string out of the struct and cloning it before ...
Mongus Pong's user avatar
3 votes
1 answer
117 views

C function to fully consume a Python iterator

I was reading into the proper way to consume an iterator on this question as well as here and I notices there is no actual proper function for this that clearly explains itself. Using: ...
Jab's user avatar
  • 133
3 votes
0 answers
125 views

Racket macro for variadic FFI

I'm working on FFI wrapper for SDL2 library in Racket. The library includes several variadic functions (e.g. SDL_SetError, SDL_LogMessage etc); Racket FFI does not have straight way for importing ...
Andrew Kravchuk's user avatar
2 votes
0 answers
1k views

Wrapping C++ library in C++/CLI using shared_ptr

Background: I'm writing C++ code after a fairly long time, so I may not be up to date on the best practices, so please bear with me. I'm only trying to improve and learn. Problem I need to write a ...
oczkoisse's user avatar
  • 121
5 votes
2 answers
3k views

Passing bitmap from C# to C++ via a struct

First, I've read the a few questions regarding this issue, the most helpful being: https://stackoverflow.com/questions/27463876/passing-bitmap-from-c-sharp-to-c I was unable to get the provided ...
WBuck's user avatar
  • 153
5 votes
1 answer
453 views

Expand every bit into a byte

I have the following code: ...
Johan's user avatar
  • 417
5 votes
1 answer
83 views

Plain C implementation of 'Cumulative All' operation for R

I want to implement a cumulative all through R's C interface. I was able to hack something together that produces the correct results, but I have little idea what idiomatic C should look like. The ...
hoelk's user avatar
  • 230
4 votes
0 answers
146 views

C++ Trie implementation with Python wrapper

I'm just starting to use C++ so beginner feedback is fine here. CPP code (main.cpp, node.h, node.cpp, trie.h, trie.cpp, utils.h) main.cpp ...
Luke's user avatar
  • 501
3 votes
0 answers
119 views

Launcher for python.exe within a self-contained application package

Below is code modified from the source code for python.exe. The motivation is to be able to build a suite of python applications that can be distributed with an installer, that share an isolated run-...
Vince W.'s user avatar
  • 131
6 votes
2 answers
914 views

Moving average function in C++ for use with R

I am trying to improve the code and speed up in C++ (Rcpp) a (centered) weighted moving average function I coded. An example of what the roll_mean function does. Note that the function works no ...
Frostic's user avatar
  • 163
7 votes
1 answer
135 views

Fastest way to find a string delimited by two words

Motivated by this question, I created a simple benchmark for investigating what is the fastest way to extract a substring given two delimiting words: ...
Håkon Hægland's user avatar
3 votes
2 answers
104 views

Safe, simultaneous string replacement

I've written a function which takes as an input: a string to be modified a vector of regular expressions on which to match a vector of replacements option to ignore case in the match statements The ...
Mark's user avatar
  • 141
5 votes
1 answer
349 views

Transform a vector with the FFI in Haskell

In Haskell, I want to apply a transformation to each element of a vector using the FFI. I am rather ignorant in C, I am more or less novice in Haskell and this is the first time I use storable vectors ...
Stéphane Laurent's user avatar
3 votes
1 answer
1k views

Correction for barrel and pincushion distortion

I have a c++ dll I am calling (PInvoking) from C# which corrects barrel and pincushion distortion. Basically I call a method which generates a set of look up arrays and I adjust the pixels of the ...
Steven Wood's user avatar
6 votes
2 answers
4k views

C# to C++ function call that fills and auto resize array if size is not enough

I have a C# function that calls a C++ function to fill the array with pixel data from a camera. This is better than allocating memory on the C++ side and marshaling it on C# side each time the ...
Programmer's user avatar
8 votes
0 answers
347 views

.NET DLL Injector in C++

I built a DLL Injector with User-Interface. The program lists all processes, the user chooses one of them and a DLL, and inject it. It's my first project in C++, and I know it's not exactly C++ ...
Gil Mans's user avatar
3 votes
1 answer
185 views

Reading a string-to-string map from an archive

I have a CArchive Which contains a serialized CMapStringToString. From my research it is my understanding that it is quite ...
Andrew Truckle's user avatar
11 votes
2 answers
1k views

Call a lua function - the C++ way

I'm not that good when it comes to templates, especially when it comes to variadic templates. I want to show you some code where I use both - templates and variadic templates - to call a lua function. ...
tkausl's user avatar
  • 614
2 votes
1 answer
139 views

Two knot-removal function for curve and surface

Recently, I have been using the Wolfram LibraryLink wrapper. By that technique, I could call the function written in C from Wolfram Mathematica. Here are two C wrapper functions, ...
xyz's user avatar
  • 217
2 votes
2 answers
185 views

How do I write my multithreading code correctly so that it is faster than a single thread? [closed]

I have the following code that I am trying to optimize. I am running it on a Linux machine with 24 cores. I thought I could use multithreading to make it faster, but it's somehow making it way slower ...
Naresh Vankayalapati's user avatar
2 votes
1 answer
256 views

Counting matrix elements that have "4" or "5" as a neighbor

I have a matrix (500x500) of integers. For each entry, I need to look at its surrounding neighbours (so 8 elements) and determine what the integers are, and run a function on these integers. Here ...
masfenix's user avatar
  • 123
4 votes
0 answers
232 views

Reduce encoded length of UTF-8 encoded Ruby string in C extension

I'm writing a Ruby extension in C. It's a string processing module working on UTF-8 encoded strings only. One method, full_width_to_ascii!, converts full width ...
bsa's user avatar
  • 795
5 votes
0 answers
717 views

Calculating complex exponential numbers using OpenMP in Cython

I am trying to create a function that can either beat numexpr or perform comparably for the vectorized mathematical operation ...
Trekkie's user avatar
  • 151
4 votes
1 answer
1k views

Same PyObject for a shared_ptr C++ object

I have a similar problem as in this mail. I want that every time I return the same C++ managed object to Python, I get the same PyObject. With the standard ...
Roland Spatzenegger's user avatar
3 votes
2 answers
623 views

Hello World using Python embedded in C++, with RAII library initialization

I have written a small Hello World application that uses Python's C-API. The C library requires some functions to be called for global library (de-)initialization and the objects created by the ...
moooeeeep's user avatar
  • 430
5 votes
1 answer
128 views

Mean of the squares of differences for a large matrix

I have tried to obtain speed gains by transferring Matlab calculations to C and call it using .mex. My goal is to operate on a matrix \$B\$ which has roughly the dimension 10000x1000 $$ \frac{1}{\...
Phun's user avatar
  • 153
8 votes
3 answers
5k views

k-nearest neighbors using MATLAB with MEX

I have implemented kNN (k-nearest neighbors) as follows, but it is very slow. I want to get an exact k-nearest-neighbor, not the approximate ones, so I didn't use the FLANN or ANN libraries. ...
mining's user avatar
  • 235
7 votes
2 answers
282 views

Linspace method build in Python's C API

Today I built a linspace function in Python's C API: ...
mjgpy3's user avatar
  • 729