Create DLL with pointer interface
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Ryan Livingston
2018-11-22
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.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 C Shared Library Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!