mug*_*tsu 2 dll matlab loadlibrary
我正在尝试在 matlab 中调用 dll 函数。我有一个 C++ 结构,如 Sixense.h 所示:
typedef struct _sixenseControllerData {
float pos[3];
float rot_mat[3][3];
float joystick_x;
float joystick_y;
float trigger;
...
} sixenseControllerData;
Run Code Online (Sandbox Code Playgroud)
和我可以调用的功能:
SIXENSE_EXPORT int sixenseInit( void );
SIXENSE_EXPORT int sixenseGetAllNewestData( sixenseAllControllerData * );
Run Code Online (Sandbox Code Playgroud)
calllib('sixense','sixenseInit')由于没有输入,我可以轻松地使用它,但是对于函数 SixenseGetAllNewestData 我需要有一个结构指针。我意识到 libstruct 是我需要使用的。但是,我似乎做得不对。
所以我像这样尝试了 libstruct:
libstruct('sixenseControllerData')
Run Code Online (Sandbox Code Playgroud)
我得到错误:
??? Error using ==> feval
Undefined function or variable 'lib.sixenseControllerData'.
Error in ==> libstruct at 15
ptr=feval(['lib.' structtype]);
Run Code Online (Sandbox Code Playgroud)
编辑:这是我当前未编辑的原型文件:http : //pastebin.com/PemmmMqF
完整的头文件可在此处获得:https : //github.com/rll/sixense/blob/master/include/sixense.h
对于 C 结构,loadlibrary生成名为的类型:s_{NAME}其中{NAME}是结构的名称。在您的情况下,我们创建一个指针:
s = libstruct('s_sixenseControllerData');
Run Code Online (Sandbox Code Playgroud)
我们可以通过指示 MATLAB 生成原型文件来看到这一事实:
>> loadlibrary('sixense', 'sixense.h', 'proto','sixense_proto')
Run Code Online (Sandbox Code Playgroud)
原型文件是 MATLAB 命令的文件,我们可以修改和使用它来代替头文件。在这种情况下,文件将包含以下内容:
...
structs.s_sixenseControllerData.members = struct('pos', 'single#3', 'rot_mat', 'single#9', 'joystick_x', 'single', 'joystick_y', 'single', 'trigger', 'single', 'buttons', 'uint32', 'sequence_number', 'uint8', 'rot_quat', 'single#4', 'firmware_revision', 'uint16', 'hardware_revision', 'uint16', 'packet_type', 'uint16', 'magnetic_frequency', 'uint16', 'enabled', 'int32', 'controller_index', 'int32', 'is_docked', 'uint8', 'which_hand', 'uint8', 'hemi_tracking_enabled', 'uint8');
structs.s_sixenseAllControllerData.members = struct('controllers', 's_sixenseControllerData#4');
....
Run Code Online (Sandbox Code Playgroud)
不幸的是,限制的loadlibrary是,它不支持嵌套结构非常好,特别是如果一个结构包含一个指向到另一结构(或在这种情况下,数组):
不支持嵌套结构或包含指向结构的指针的结构。但是,MATLAB 可以访问在外部库中创建的结构体数组。
所以你将无法直接sixenseAllControllerData在 MATLAB 端创建结构体,它在 C 头文件中定义为:
typedef struct _sixenseAllControllerData {
sixenseControllerData controllers[4];
} sixenseAllControllerData;
Run Code Online (Sandbox Code Playgroud)
根据以下讨论,一种解决方法是将数组“展开”/“展平”为单独的变量。您可以在头文件的副本中执行此操作,也可以在生成的原型文件中进行更改(我认为这是首选方式)。您无需重新编译共享库即可执行此操作。
在您的情况下,将生成的sixense_proto.m文件中的嵌套结构更改为:
structs.s_sixenseAllControllerData.members = struct(...
'controllers1', 's_sixenseControllerData', ...
'controllers2', 's_sixenseControllerData', ...
'controllers3', 's_sixenseControllerData', ...
'controllers4', 's_sixenseControllerData');
Run Code Online (Sandbox Code Playgroud)
现在我们可以创建一个指向这个结构的指针,并调用 C 方法:
s = libstruct('s_sixenseAllControllerData');
s.controllers1 = libstruct('s_sixenseControllerData');
s.controllers2 = libstruct('s_sixenseControllerData');
s.controllers3 = libstruct('s_sixenseControllerData');
s.controllers4 = libstruct('s_sixenseControllerData');
out = calllib('sixense', 'sixenseGetAllNewestData', s);
get(s)
Run Code Online (Sandbox Code Playgroud)
一个完全不同的解决方案是编写一个MEX 函数来与库交互。它就像任何其他 C/C++ 代码一样,仅使用mxArrayMX-API 与 MATLAB 接口...
为了测试上述内容,我创建了一个具有类似结构的简单 DLL,并实现了上述解决方案。如果有人想测试它,这是代码:
#ifndef HELPER_H
#define HELPER_H
#ifdef _WIN32
#ifdef EXPORT_FCNS
#define EXPORTED_FUNCTION __declspec(dllexport)
#else
#define EXPORTED_FUNCTION __declspec(dllimport)
#endif
#else
#define EXPORTED_FUNCTION
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
#ifndef MYLIB_H
#define MYLIB_H
#include "helper.h"
typedef struct _mystruct {
int pos[3];
double value;
} mystruct;
typedef struct _mystruct2 {
mystruct arr[2];
int num;
} mystruct2;
EXPORTED_FUNCTION void myfunc(mystruct *);
EXPORTED_FUNCTION void myfunc2(mystruct2 *);
#endif
Run Code Online (Sandbox Code Playgroud)
#define EXPORT_FCNS
#include "helper.h"
#include "mylib.h"
void myfunc(mystruct *s)
{
s->pos[0] = 10;
s->pos[1] = 20;
s->pos[2] = 30;
s->value = 4.0;
}
void myfunc2(mystruct2 *s)
{
int i;
for(i=0; i<2; i++) {
myfunc(&(s->arr[i]));
}
s->num = 99;
}
Run Code Online (Sandbox Code Playgroud)
将上述内容编译成DLL后,我们生成初始原型文件:
loadlibrary('./mylib.dll', './mylib.h', 'mfilename','mylib_proto')
unloadlibrary mylib
Run Code Online (Sandbox Code Playgroud)
我像之前描述的那样编辑原型文件:
function [methodinfo,structs,enuminfo,ThunkLibName] = mylib_proto()
MfilePath = fileparts(mfilename('fullpath'));
ThunkLibName = fullfile(MfilePath,'mylib_thunk_pcwin64');
enuminfo = [];
structs = [];
structs.s_mystruct.members = struct('pos','int32#3', 'value','double');
structs.s_mystruct2.members = struct('arr1','s_mystruct', ...
'arr2','s_mystruct', 'num','int32');
ival = {cell(1,0)};
methodinfo = struct('name',ival, 'calltype',ival, 'LHS',ival, ...
'RHS',ival, 'alias',ival, 'thunkname',ival);
methodinfo.thunkname{1} = 'voidvoidPtrThunk';
methodinfo.name{1} = 'myfunc';
methodinfo.calltype{1} = 'Thunk';
methodinfo.LHS{1} = [];
methodinfo.RHS{1} = {'s_mystructPtr'};
methodinfo.thunkname{2} = 'voidvoidPtrThunk';
methodinfo.name{2} = 'myfunc2';
methodinfo.calltype{2} = 'Thunk';
methodinfo.LHS{2} = [];
methodinfo.RHS{2} = {'s_mystruct2Ptr'};
end
Run Code Online (Sandbox Code Playgroud)
现在我们终于可以调用 DLL 公开的函数了:
%// load library using proto file
loadlibrary('./mylib.dll', @mylib_proto)
%// call first function with pointer to struct
s = struct('pos',[0,0,0], 'value',0);
ss = libstruct('s_mystruct',s);
calllib('mylib', 'myfunc', ss)
get(ss)
%// call second function with pointer to struct containing array of struct
xx = libstruct('s_mystruct2');
xx.arr1 = libstruct('s_mystruct');
xx.arr2 = libstruct('s_mystruct');
calllib('mylib', 'myfunc2', xx)
get(xx)
%// clear references and unload library
clear ss xx
unloadlibrary mylib
Run Code Online (Sandbox Code Playgroud)