Calling DLL functions from MATLAB using C Header Files

Knowledge Base Article # Q200339

Read Prior Article Read Next Article
Summary This article demonstrates how to interface with MATLAB from ATEASY with an example for using the GXAO software package used to control and interface the GX1648.
  
Login to rate article

Introduction

MATLAB® support using and calling DLL Windows libraries, This article shows how to use a library GXAO.DLL and the C Header file to call and control the GX1648 instrument. The GXAO.dll and the GxAo.h are provided with the GXAO software package. The example was written with MATLAB® 2020b.

MATLAB® DLL Support

It is useful to note that after MATLAB® release 2015b, 32-bit DLL are no longer supported. Currently, the required tools are provided in a core installation of MATLAB® 9.9 without the addition of toolboxes[1]. Once a library is loaded, you can view[2] and call its C-compatible functions. More information about troubleshooting, limitations, data types etc. regarding MATLAB®'s C -interface is available on MathWorks' website[4], all of which is a recommended read when preparing to use driver libraries from MATLAB®.

Method

Firstly, the shared library needs to be loaded. This can be achieved by using the MATLAB® function loadlibrary(libname, hfile). In the included example, we are modifying the GxAo.h to simplify and remove the included using modified GxAo.h named m_GxAo.h for hfile argument. It includes Windows.h before GxAo.h. There are also other ways to include the needed Windows.h using overrides like loadlibrary(libname,hfile,Name,Value), master headers and so on. Different methods were tested to load libraries. The one used in the provided example was deemed the most stable out of the ones that were experimented with. Once library is loaded successfully, you can inspect the callable functions using another tool libfunctionsview[2].
Calling loaded functions is done using the MATLAB®  function [x1,...,xN] = calllib(libname,funcname,arg1,...,argN) . The function accepts loaded libname, funcname and funcname's arguments[1]. The return values are ordered and correspond to the function prototypes, which are easy to view for example with MATLAB® function libfunctionsview, header file or by following the instrument's function reference available in its user's guide. Using libfunctionsview has the added benefit of mapping data types to Matlab. Please refer to MatlabGxAoExample for examples on how to load, initialize variables and call library functions.
Note that when working with MATLAB® data types and especially when passing pointers, it is not uncommon to crash MATLAB® if not declaring the DLL function parameters properly, using for example libfunctionsview will help with this problem.
he example provided includes a modified header file, m -file as well as a published run of the script using GX1648 that is supported by the GXAO software package.

Summary

Calling Marvin Test Solution DLL functions used to control instruments from MATLAB® is fairly straightforward. MATLAB®  loadlibrary and calllib functions and the DLL C header are used to perform  this task.

References

[1]
Matlab provides functions loadlibrary and calllib to load and call library functions:[2]
Reading library function signatures gives an easy way of initializing MATLAB® variables correctly. A GUI is provided for reading library functions signatures:[3]
A MATLAB® example was written and published using the above knowledge and is available to download:[4]
MATLAB® external language interface for C libraries:
Article Date 9/14/2022
Keywords GxAo, GX1648, MATLAB, driver, PXI, library, Windows


Login to rate article

Read Prior Article Read Next Article
>