Create DLL with pointer interface

2 views (last 30 days)
Jannis TW
Jannis TW on 22 Nov 2018
Edited: Jannis TW on 23 Nov 2018
I am trying to generate a DLL from a set of Matlab functions with a certain interface.
Simplified, I need a DLL, say myDLL.dll with three functions getPointers(), updateState(), getState(). The function getPointers needs to pass 'a' and 'b' as pointers, i.e. it defines some vectors and passes the physical address of them. From the interface loading the DLL this would be written as getPointers(a,b) - using the input parameters of the function and not the return value. The other functions use the defined physical addresses to pass some information. There are also some data that needs to be accessed from all functions without using 'a' and 'b' - I thought of using global variables.
What is the best way to approach this problem?
Here some example functions using global variables:
function [errorcode, p1,p2] = getPointers()
% global variables? but this does not work with using p1,p2 as function outputs
% global p1
% global p2
global internvariable
% initialise with zeros
p1 = zeros(2048,1);
p2 = zeros(256,1);
% main part of function: call other functions and save stuff in variables
internvariable = 100; % example of saving something internally
p1(17) = 45; % example
p2(3) = 3453; % example
% function return:
errorcode = 0;
end
function errorcode = updateState()
global p1
global p2
global internvariable
% main part of function: read stuff from the interface p1, p2 (changed from outside the dll)
internvariable = p1(10); % example
% function return:
errorcode = 0;
end
function errorcode = getState()
global p1
global p2
global internvariable
% main part of function: save stuff in the interface p1, p2
p1(2) = internvariable; % example
% function return:
errorcode = 0;
end
I have tried with
codegen -config:dll -o myDLL -globals {'p1', zeros(2048,1), 'p2',zeros(256,1), 'internvariable',zeros(1,1)} getPointers updateState getState
and this compiles fine and looks OK in the dependancy walker, but the interface does not seem to be correct, e.g. the generated c header file has this
extern void getPointers(double *errorcode, double b_p1[2048], double p2[256])
whereas it shouldn't be a void function, but a double returning 'errorcode'.
I also tested the GUI App 'Library Compiler' (which is creating prefixes for function names), but I don't know what is the appropriate approach in Matlab? I struggle how to configure both -my code and the compiler- properly to solve the pointer issue. It is also important, that the function names shall not be modified, i.e. no prefix mlf- etc.
  2 Comments
Ryan Livingston
Ryan Livingston on 22 Nov 2018
The signature of getPointers is as shown because the corresponding MATLAB function has multiple outputs. In this case, the outputs are mapped to inputs which are written by reference. If you look at the body of getPointers, you should see that it writes to the memory pointed to by errorcode.
Jannis TW
Jannis TW on 22 Nov 2018
Edited: Jannis TW on 23 Nov 2018
Thanks, Ryan.
Is there any way to control this? So that errorcode is not returned by reference, but only the other two outputs?
Note, that the main problem is that I need to ensure that p1 and p2 are always using the same physical address - which I think is not the case with my above solution (p1 and p2 can't be global variable and output?).

Sign in to comment.

Answers (0)

Categories

Find more on Get Started with MATLAB Compiler SDK in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!