To put it down, DLL is a dynamic link library and a binary library file and program interface after source code compilation. What is different from a static Link Library is, during compilation, the program does not link the execution body of the dynamic link library. Instead, it retains a call Mark in the file and loads the dynamic link library file into the memory only when the program is running. In addition, the DLL is shared during runtime, that is, when multiple programs are called, only one dynamic link library is maintained in the memory.
There are two methods for calling a dynamic link library: explicit and implicit.
The above plotdata. C, plotdata. H, plotdata. Lib, and plotdata. DLL files are used for implicit links.
First, add plotdata. C and plotdata. H to the project. Note that you must add # include "plotdata. H" to the file that requires the function ".
Then, enter plotdata. Lib in the input link. Right-click the project and choose propertites> link> input> additional dependecies to add plotdata. lib (that is, enter libmat when calling the Matlab Engine. lib, libeng. lib and so on) Pay attention to plotdata. lib also needs to be placed in your project, or write the full path, such as "D: \ data \ plotadata. lib ", which must be enclosed in quotation marks.
In this way, you can directly use the interface function in plotdata. h in your code.
Another explicit link method: the so-called "explicit" means that I want to call this DLL in the code.
First, we need to define a function type to facilitate forced type conversion of the function. We can find the function plotdata we will use in plotdata. H. Its function declaration is as follows:
extern LIB_plotdata_CPP_API void MW_CALL_CONV plotdata(const mwArray& rgbData);
Ignore the complicated macro definitions and mimic defining our own function types:
typedef void (*HMAT)(const mwArray& rgbData);
Then explicitly link plotdata. dll in the code
Hinstance hdll = NULL; // DLL handle hdll = loadlibrary ("plotdata. DLL "); hmat plotdata = (hmat) getprocaddress (hdll," plotdata "); // The first parameter is the DLL handle, and the second parameter is the name of the function to be loaded.
Then you can directly use the plotdata function in the code. This explicit link only requires the plotdata. dll file ~
Next let's take a look at the generated function interface.
extern LIB_plotdata_C_API bool MW_CALL_CONV plotdataInitializeWithHandlers( mclOutputHandlerFcn error_handler, mclOutputHandlerFcn print_handler);extern LIB_plotdata_C_API bool MW_CALL_CONV plotdataInitialize(void);extern LIB_plotdata_C_API void MW_CALL_CONV plotdataTerminate(void);extern LIB_plotdata_C_API void MW_CALL_CONV plotdataPrintStackTrace(void);extern LIB_plotdata_C_API bool MW_CALL_CONV mlxPlotdata(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);extern LIB_plotdata_C_API long MW_CALL_CONV plotdataGetMcrID();extern LIB_plotdata_C_API bool MW_CALL_CONV mlfPlotdata(mxArray* rgbData);
This is the main function in plotdata. h. Plotdatainitialize (void) can be seen as the initialization function. If it is an implicit link DLL, it is best to call this function first to determine the return value. Otherwise, the DLL may not be loaded. If the explicit link is not loaded successfully, no error is reported, however, we can check whether the function is allocated memory (that is, whether it is null) during single-step debugging ). Plotdataterminate (void) Is the function for terminating the dynamic link library.
Mlxplotdatat and mlfplotdata are the two most critical interfaces and functions to be loaded. They perform the same functions as the plotdata function in M files. The two function input parameters are different:
Mlxplotdata (INT nlhs, mxarray * plhs [], int nrhs, mxarray * prhs []). nlhs and plhs indicate the number of output parameters and the mxarray array of output parameters respectively. nrhs, prhs indicates the number of input parameters and the array of input parameters. (This function is a bit generic ......)
Mlfplotdata (mxarray * rgbdata); it is much simpler. Basically, it is the same as the plotdata function defined in m Files (My plotdata is defined as function [] = plotdata (rgbdata ))
Therefore, functions starting with MLF are generally loaded in the program.
Here we need to mention that the dynamic link library of C is compiled by me. If a dynamic link library of C ++ is generated, the generated interface function also carries a function starting with mlx, that is
Bool mw_call_conv mlxplotdata (INT nlhs, mxarray * plhs [], int nrhs, mxarray * prhs [])
However, another function does not contain MLF and is directly
Void mw_call_conv plotdata (const mwarray & rgbdata)
Moreover, the input parameter is not an mxarray array, but an mwarray array, which is also the main difference in mixed programming of C and C ++ and Matlab (I will explain it in detail later)
However, I have never succeeded in trying C ++ dynamic links. Later, we can see that the generated CPP file also has an extend "C" {}, which is a keyword provided by C ++ for compatibility with C, the C ++ compiler treats the code inside the braces of extend "C" as the C language code, which makes me very confused ...... If commented out, a connection error is returned.
The possible reason is that MATLAB does not support C ++ compilation well (his built-in LSS compiler can only compile the interface into C). In short, it did not succeed, currently, dynamic links of C are used in the program.
(Reprinted please indicate the author and Source: http://blog.csdn.net/xiaowei_cqu is not allowed for commercial use)