matlab调用dll文件

标签:
matlabdll调用it |
分类: Matlab |
addpath([matlabroot '\extern\examples\shrlib'])%以matlab的dll为例
loadlibrary shrlibsample
shrlibsample.h alias lib%加载shrlibsample库,并重命名为lib,注意加载时常常需要shrlibsample的头文件
libfunctionsview
lib%显示lib库中的可用函数
libfunctions lib
-full%在命令窗口显示lib库函数
Functions in library lib:
[double, doublePtr] addDoubleRef(double, doublePtr, double)
double addMixedTypes(int16, int32, double)
[double, c_structPtr] addStructByRef(c_structPtr)
double addStructFields(c_struct)
c_structPtrPtr allocateStruct(c_structPtrPtr)
voidPtr deallocateStruct(voidPtr)
lib.pointer exportedDoubleValue
lib.pointer getListOfStrings
doublePtr multDoubleArray(doublePtr, int32)
[lib.pointer, doublePtr] multDoubleRef(doublePtr)
int16Ptr multiplyShort(int16Ptr, int32)
doublePtr print2darray(doublePtr, int32)
printExportedDoubleValue
cstring readEnum(Enum1)
[cstring, cstring]
stringToUpper(cstring)
str = 'This was a Mixed Case string';
calllib('lib', 'stringToUpper',
str)%调用lib库函数
THIS WAS A MIXED CASE
STRING
如果lib库函数的参数为结构体,如下
double addStructFields(struct c_struct st)
{
double t = st.p1 + st.p2 + st.p3;
return t;
}
则要传递该结构体要用如下方法
sm.p1 = 476;sm.p2 = -299; sm.p3 = 1000;
sc = libstruct('c_struct',
sm);%用libstruct函数构造dll中的结构体,还可做初始化
get(sc)%显示该结构体
calllib('lib', 'addStructFields', sc)%调用函数用calllib,参数依次为库名,库函数,库函数输入参数
clear sc
%用完结构体要清除
如果lib库函数的参数为指针,如下
void multiplyShort(short *x, int size)
{
int i;
for (i = 0; i < size; i++)
*x++ *= i;
}
则要传递该指针要用如下方法
v = [4 6 8; 7 5 3];
pv = libpointer('int16Ptr',
v);%用libpointer函数来构造指针
get(pv, 'Value')%显示指针的内容
calllib('lib', 'multiplyShort', pv, 6);%函数调用
unloadlibrary
lib%调用结束要卸载库