Questions tagged [ffi]
Machine code and assembly code is sometimes called native code when referring to platform-dependent parts of language features or libraries.
36
questions
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. ...
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.
...
8
votes
0
answers
340
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++ ...
7
votes
2
answers
281
views
Linspace method build in Python's C API
Today I built a linspace function in Python's C API:
...
7
votes
1
answer
133
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:
...
6
votes
2
answers
903
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 ...
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 ...
5
votes
2
answers
929
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,...
5
votes
2
answers
2k
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 ...
5
votes
1
answer
406
views
Expand every bit into a byte
I have the following code:
...
5
votes
1
answer
127
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}{\...
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 ...
5
votes
1
answer
348
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 ...
5
votes
0
answers
713
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 ...
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 ...
4
votes
1
answer
93
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 ...
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
...
4
votes
0
answers
229
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 ...
3
votes
2
answers
609
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 ...
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 ...
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 ...
3
votes
1
answer
97
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.
...
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 ...
3
votes
1
answer
110
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:
...
3
votes
1
answer
183
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 ...
3
votes
0
answers
29
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 ...
3
votes
0
answers
790
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 ...
3
votes
0
answers
124
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 ...
3
votes
0
answers
118
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-...
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, ...
2
votes
2
answers
184
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 ...
2
votes
1
answer
254
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 ...
2
votes
1
answer
141
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.
<...
2
votes
0
answers
167
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/...
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 ...
1
vote
0
answers
42
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 ...