Main Content

calllib

Call function in C shared library

Description

example

[x1,...,xN] = calllib(libname,funcname,arg1,...,argN) calls function funcname in C library libname, passing input arguments arg1,...,argN. The calllib function returns output values obtained from funcname in x1,...,xN.

Examples

collapse all

Load the library.

if ~libisloaded('shrlibsample')
   addpath(fullfile(matlabroot,'extern','examples','shrlib'))
   loadlibrary('shrlibsample')
end

Display function signature.

libfunctionsview shrlibsample
[double, c_structPtr] addStructByRef(c_structPtr)

The input argument is a pointer to a c_struct data type.

Create a MATLAB® structure, struct:

struct.p1 = 4; struct.p2 = 7.3; struct.p3 = -290; 

Call the function.

[res,st] = calllib('shrlibsample','addStructByRef',struct);

Display the results.

res
res =
         -279

Cleanup.

unloadlibrary shrlibsample

Input Arguments

collapse all

Name of shared library, specified as a character vector. Do not include the path or file extension in libname.

If you call loadlibrary using the alias option, then you must use the alias name for the libname argument.

Data Types: char

Name of function in library, specified as a character vector.

Data Types: char

Input arguments, 1 through N, required by funcname (if any), specified by any type. The funcname argument list specifies the argument type.

Output Arguments

collapse all

Output arguments, 1 through N, from funcname (if any), returned as any type. The funcname argument list specifies the argument type.

Limitations

  • Use with libraries that are loaded using the loadlibrary function.

Tips

  • MATLAB validates input argument types before calling funcname. If MATLAB displays error messages about data types, check the MATLAB function signature. For example, if funcname is in library mylib type:

    libfunctions('mylib','-full')

    To find funcname, scroll through the output. For more information, refer to your library documentation.

    When you call funcname, that function might display errors. For information about error messages, refer to your library documentation.

Version History

Introduced before R2006a