Computer Engineering I (Part I) - fh

Download Report

Transcript Computer Engineering I (Part I) - fh

Computer Engineering
Dynamic Link Libraries - DLL'S
Dipl.-Ing. (FH) Christian V. Madritsch
University of Applied Science
Electronics and Equipment Engineering
www.cti.ac.at/rts
Copyright Notice
 All rights reserved. No part of this publication
may be reproduced, stored in a retrieval system
or transmitted, in any form or by any means,
electronic, mechanical, photocopying, recording,
or otherwise, without prior written permission of
the author except for student usage.
 Copyright © 2006 by Christian V. Madritsch
17.07.2015
[email protected]
2
Outline
 How to create DLL's using MSVC?
 How to use DLL's in MSVC Applications?
 Load-Time Dynamic Linking
 Run-Time Dynamic Linking
 How to create DLL's for Matlab?
 Using DLL's with Matlab
 Creating cpp-DLL's with Matlab
 How to create S-Functions for Simulink?
 How to use DLL's in LabVIEW?
17.07.2015
[email protected]
3
General
 Dynamic-link libraries (DLL) are modules that
contain functions and data.
 A DLL is loaded at runtime by its calling modules
(.EXE or DLL).
 When a DLL is loaded, it is mapped into the
address space of the calling process.
 DLLs can define two kinds of functions:

Exported functions can be called by other modules.
 Internal functions can only be called from within the
DLL where they are defined.
17.07.2015
[email protected]
4
Types of Dynamic Linking
 There are two methods for calling a function in a DLL:

In load-time dynamic linking, a module makes explicit calls
to exported DLL functions.



In run-time dynamic linking, a module uses the
LoadLibrary function to load the DLL at run time.



17.07.2015
This requires you to link the module with the import library for
the DLL.
An import library supplies the system with the information
needed to load the DLL and locate the exported DLL functions
when the application is loaded.
After the DLL is loaded, the module calls the GetProcAddress
function to get the addresses of the exported DLL functions.
The module calls the exported DLL functions using the function
pointers returned by GetProcAddress.
This eliminates the need for an import library.
[email protected]
5
DllMain Function (i)

Every DLL must have an entry point. The system calls the entry-point function
whenever processes and threads load or unload the DLL.
BOOL WINAPI DllMain(
HINSTANCE hInstance,
DWORD dwReason,
LPVOID lpReserved);

// handle to DLL module
// reason for calling function
// reserved
dwReason:




17.07.2015
DLL_PROCESS_ATTACH: Indicates that the DLL is being loaded into the virtual
address space of the current process. DLLs can use this opportunity to initialize any
instance data.
DLL_THREAD_ATTACH: Indicates that the current process is creating a new thread.
DLL_THREAD_DETACH: Indicates that a thread is exiting cleanly.
DLL_PROCESS_DETACH: Indicates that the DLL is being unloaded from the virtual
address space of the calling process. The DLL can use this opportunity to free any
thread local data.
[email protected]
6
DllMain Function (ii)
#include <windows.h>
BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD dwReason, LPVOID
lpReserved)
{ switch(dwReason)
{
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;
case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;
case DLL_PROCESS_DETACH:
// Perform any necessary cleanup.
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
17.07.2015
[email protected]
7
Exporting Functions and
Import Libraries
 Using the _declspec(dllexport) modifier:
__declspec(dllexport) int Add(int x, int y)
{
return x+y;
}
__declspec(dllexport) int Sub(int x, int y)
{
return x-y;
}
 The import library (.LIB) file contains information the linker
needs to resolve external references to exported DLL
functions, so the system can locate the specified DLL and
exported DLL functions at run time.
17.07.2015
[email protected]
8
Outline
 How to create DLL's using MSVC?
 How to use DLL's in MSVC Applications?
 Load-Time Dynamic Linking
 Run-Time Dynamic Linking
 How to create DLL's for Matlab?
 Using DLL's with Matlab
 Creating cpp-DLL's with Matlab
 How to create S-Functions for Simulink?
 How to use DLL's in LabVIEW?
17.07.2015
[email protected]
9
Using Load-Time Dynamic Linking (i)
 Copy .lib-File to the search path of the compiler
 Include .lib-File in Project Settings/Link/Input
 Define import functions or user header-file
17.07.2015
[email protected]
10
Using Load-Time Dynamic Linking (ii)
#include <windows.h>
#include <iostream.h>
int Add(int x, int y);
int main (void)
{
int x, y = 0;
cout << "Enter x: ";
cin >> x;
cout << "Enter y: ";
cin >> y;
cout << "Result x+y= " << Add(x, y) << endl;
return 0;
}
17.07.2015
[email protected]
11
Outline
 How to create DLL's using MSVC?
 How to use DLL's in MSVC Applications?
 Load-Time Dynamic Linking
 Run-Time Dynamic Linking
 How to create DLL's for Matlab?
 Using DLL's with Matlab
 Creating cpp-DLL's with Matlab
 How to create S-Functions for Simulink?
 How to use DLL's in LabVIEW?
17.07.2015
[email protected]
12
Using Run-Time Dynamic Linking (i)
 Create .def-File for the DLL:
LIBRARY
EXPORTS
Add
Sub
dll_01
@1
@2
 Use the following functions to access the DLL:



17.07.2015
LoadLibrary
GetProcAddress
FreeLibrary
[email protected]
13
Using Run-Time Dynamic Linking (ii)
#include <windows.h>
#include <iostream.h>
typedef int (*MYPROC)(int, int);
int main (void){
HINSTANCE hinstLib;
MYPROC ProcAdd;
hinstLib = LoadLibrary("dll_01");
if (hinstLib != NULL){
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "Add");
if (ProcAdd != NULL){
int x, y = 0;
cout << "Enter x: ";
cin >> x;
cout << "Enter y: ";
cin >> y;
cout << "Result x+y= " << ProcAdd(x, y) << endl;
}
FreeLibrary(hinstLib);
}
return 0;
17.07.2015
[email protected]
}
14
Outline
 How to create DLL's using MSVC?
 How to use DLL's in MSVC Applications?
 Load-Time Dynamic Linking
 Run-Time Dynamic Linking
 How to create DLL's for Matlab?
 Using DLL's with Matlab
 Creating cpp-DLL's with Matlab
 How to create S-Functions for Simulink?
 How to use DLL's in LabVIEW?
17.07.2015
[email protected]
15
Using DLL's with Matlab
 DLL modifications:
 Create a header-File containing prototypes:
__declspec(dllexport) int Add(int x, int y);
__declspec(dllexport) int Sub(int x, int y);
 Matlab functions:
 loadlibrary
 libfunctions
 libfunctionsview
 libisloaded
 calllib
 unloadlibrary
17.07.2015
[email protected]
16
Matlab Functions (i)
 loadlibrary:
 To load an external library into MATLAB
loadlibrary(libfile, headerfile)
 libfunctions:
 Return information on functions in external library
 libfunctionsview:
 Create window displaying information on functions in
external library
 libisloaded:
 To determine if external library is loaded
17.07.2015
[email protected]
17
Matlab Functions (ii)
 calllib:
 Call function in external library
 calllib(libfile, functions, arg1, arg2, ...)
 unloadlibrary:
 To unload an external library from memory
 More information:
 Matlab External Interfaces Reference
17.07.2015
[email protected]
18
Example
loadlibrary('C:\temp\dll_01.dll', 'c:\temp\dll_01.h')
libfunctions dll_01
libfunctionsview dll_01
a = calllib('dll_01', 'Add', 2, 3)
b = calllib('dll_01', 'Sub', 5, 2)
unloadlibrary('dll_01')
17.07.2015
[email protected]
19
Outline
 How to create DLL's using MSVC?
 How to use DLL's in MSVC Applications?
 Load-Time Dynamic Linking
 Run-Time Dynamic Linking
 How to create DLL's for Matlab?
 Using DLL's with Matlab
 Creating cpp-DLL's with Matlab
 How to create S-Functions for Simulink?
 How to use DLL's in LabVIEW?
17.07.2015
[email protected]
20
Creating cpp-DLL's with Matlab
 cpp-File:
 The entry point is called mexFunction
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])

nlhs: number of left-handed side arguments
 plhs: pointer to array of left-handed side arguments
 nrhs: number of right-handed side arguments
 prhs: pointer to array of right-handed side arguments
 [a, b, c, ...] = function(d, e, f, ...)
17.07.2015
[email protected]
21
Example
#include "mex.h"
int Add(double x[], double y[]){
return x[0]+y[0];
}
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
double *x, *y, *res;
if(nrhs < 2){
mexPrintf("Too few arguments!");
return;}
if(nlhs > 1){
mexPrintf("Too many outputs!");
return;}
x = mxGetPr(prhs[0]);
y = mxGetPr(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);
res = mxGetPr(plhs[0]);
res[0] = Add(x, y);
}
17.07.2015
[email protected]
22
How to compile?
 mex -setup

Adjust Microsoft Compiler
 mex add.app

Compiles add.cpp File into Matlab DLL
 More information:

17.07.2015
Matlab External Interfaces Reference
[email protected]
23
Outline
 How to create DLL's using MSVC?
 How to use DLL's in MSVC Applications?
 Load-Time Dynamic Linking
 Run-Time Dynamic Linking
 How to create DLL's for Matlab?
 Using DLL's with Matlab
 Creating cpp-DLL's with Matlab
 How to create S-Functions for Simulink?
 How to use DLL's in LabVIEW?
17.07.2015
[email protected]
24
S-Functions for Simulink
 A S-function is a computer language description of a Simulink




block.
 S-functions can be written in MATLAB, C, C++, Ada, or
Fortran.
C, C++, Ada, and Fortran S-functions are compiled as MEXfiles using the mex utility.
As with other MEX-files, they are dynamically linked into
MATLAB when needed.
S-functions allow you to add your own blocks to Simulink
models.
More information:
 Simulink: Writing S-Functions
17.07.2015
[email protected]
25
S-Functions Structure
17.07.2015
[email protected]
26
Sample Code (i)
#define S_FUNCTION_NAME add
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
static void mdlInitializeSizes(SimStruct *S){
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S))
return;
if (!ssSetNumInputPorts(S, 2))
return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED );
ssSetInputPortDataType(S, 0, DYNAMICALLY_TYPED );
ssSetInputPortDirectFeedThrough(S, 0, 1 );
ssSetInputPortWidth(S, 1, DYNAMICALLY_SIZED );
ssSetInputPortDataType(S, 1, DYNAMICALLY_TYPED );
ssSetInputPortDirectFeedThrough(S, 1, 1 );
if (!ssSetNumOutputPorts(S, 1))
return;
ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED );
ssSetOutputPortDataType(S, 0, DYNAMICALLY_TYPED );
ssSetNumSampleTimes(S, 1);
ssSetOptions(S,SS_OPTION_WORKS_WITH_CODE_REUSE | SS_OPTION_EXCEPTION_FREE_CODE);
}
17.07.2015
[email protected]
27
Sample Code (ii)
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
InputRealPtrsType x = ssGetInputPortRealSignalPtrs(S,0);
InputRealPtrsType y = ssGetInputPortRealSignalPtrs(S,1);
real_T *ergebnis
= ssGetOutputPortRealSignal(S,0);
*ergebnis = *x[0] + *y[0];
}
17.07.2015
[email protected]
28
Sample Code (iii)
static void mdlTerminate(SimStruct *S)
{
}
/*=============================*
* Required S-function trailer *
*=============================*/
#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endif
17.07.2015
[email protected]
29
Include S-Functions to Simulink (i)
17.07.2015
[email protected]
30
Include S-Functions to Simulink (ii)
17.07.2015
[email protected]
31
Outline
 How to create DLL's using MSVC?
 How to use DLL's in MSVC Applications?
 Load-Time Dynamic Linking
 Run-Time Dynamic Linking
 How to create DLL's for Matlab?
 Using DLL's with Matlab
 Creating cpp-DLL's with Matlab
 How to create S-Functions for Simulink?
 How to use DLL's in LabVIEW?
17.07.2015
[email protected]
32
Including DLL's in LabVIEW
 Select:

Connectivity/Libraries&Executables function palette
 Select:

Call Library Function Node and place
it on diagram
 Double-Click LFN on diagram
17.07.2015
[email protected]
33
Configuring
 Enter Library or Path Name
 Select Function Name
 Select Return Type
 Add Parameters
17.07.2015
[email protected]
34
Calling the function
17.07.2015
[email protected]
35