_declspec(dllexport) int sum(int a, int b)
{
return a+b;
} //Build a dll: sum.dll
2.//Create a new Win32 Console program
//Select Simple Console Application and modify the cpp file where the main function is located
#include "stdafx.h"
#include
using namespace std;
// for base type and LoadLibrary
#include
// define function pointer which will point to sum(a, b)
typedef int(*Func )(int,int);
int main(int argc, char* argv[])
{
HMODULE dll = ::LoadLibrary("sum.dll"); // Load the dll just now, the path must be
if(dll)
{
Func f = (Func)GetProcAddress(dll, ?sum@@YAHHH@Z);
// Load the sum function
if(f)
cout<
cout<<"function call error";
}
else
cout<<"lodad error";
} //Build & Execute
3. One inconvenience in the above is GetProcAddress. The second parameter is a very complex string. You need to use dumpbin sum.dll /exports to get the actual name of the sum function in advance. Therefore, when writing dll at the top, the exported function is not only preceded by the existing red words to indicate export, but also by extern "C" to indicate compilation in C mode, because when compiling in C mode, the function name in the library and in the program code will remain consistent, unlike C++ which generates a new encoded function name. Then, the above dll function declaration is
extern "C" _declspec(dllexport) int sum(int a, int b)
and the GetProcAddress line of the calling program is changed to:
Func f = (Func)GetProcAddress(dll, "sum");
4. Compile to dll
5. Call it in labview using Function->Advance->Call Library Function Node.
The calling method is "C" and the function prototype is long sum(long,long). Run the VI to get the result.
================================================== =============
Calling dll in labview
By calling dll files through labview, we can reuse c and c++ codes, reduce programming workload, facilitate collaborative development, and add new functions to the program.
Let's take the example of finding square roots.
First, we need to create a dll file for solving square roots. Take VC6.0 compiler as an example (note that the compilers recommended in the labview document include Microsoft's MSC, VC, and BCB). First, create a new project, select Win32 Dynamic-Link Library type, we name it labSqrt, continue, select a Simple dll Project, and complete. Then a project is created.
Note that in the above example we used a non-MFC DLL, that is, a DLL written directly in C language without using the MFC class library structure. Its output functions generally use the standard C interface and can be called by applications written in non-MFC or MFC.
Every DLL must have an entry point, just like an application written in C, which must have a WINMAIN function. In this example, DllMain is a default entry function. Using this default entry function can ensure that the dynamic link library is initialized correctly when it is called.
The entry function has been automatically generated. You can add the functions or variables you want to export to the file.
Before an application uses functions in a DLL, it should first export these functions so that they can be used by the application. There are two ways to export these functions. One is to use the export keyword _declspec(dllexport) when defining the function, and the other is to use the module definition file .Def when creating the DLL file. Here we take the first method and add the keyword "extern C", which allows other programming languages to access the functions in the DLL you wrote.
extern"C" _declspec(dllexport) void getSqrt(double input,double *output);
We can set the square root to be a double return value (here we use void type). The reason for using two input parameters is that, please note that the output parameter is a pointer type, and we use it to get the output value. Why not just use a double floating point number? Haha, because the function call in C language is a value-by-value call, the value of the parameter cannot be changed inside the function. Therefore, if you use a floating point number directly, the initial value will be the same as the return value, and you will not be able to get the square root. So, remember, use a pointer when you need to get the parameter value. (Of course, you can get it by directly setting the return value of the function, but there can only be one return value. Using parameter values, we can get multiple ones.)
Fill in the function body as follows
// labSqrt.cpp: Defines the entry point for the DLL application.
//
#indlude "stdafx.h"
#include "math.h"
extern "C" _declspec(dllexport) void getSqrt(double input,double *output);
BOOL APPIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
return TRUE;
}
extern "C" _declspec(dllexport) void getSqrt(double input,double *output)
{
if(input >= 0)
*output = sqrt(input);
else
*output = 0.0;
}
A brief explanation
Add header file description
#include "math.h"
Because we used the mathematical function sqrt().
The function body first checks whether the input is non-negative. If it is, the square root is assigned to the element pointed to by the parameter output pointer. Otherwise, 0.0 is assigned. (This program is purely for illustration and can be modified to make it more powerful)
The compilation is successful and labSqrt.dll is generated
Create a Labview vi. In the block diagram, right-click (Functions)>>Connectivity>>Libraries & Executables>>Call Library Function Node
Double-click or right-click and select Configure to pop up the configuration dialog box.
The first tab is "Function": the first item requires you to select the generated dll. After selecting it, the second item will show the function name getSqrt in the dll. The third and fourth items are "Run in UI thread" and C (we use C, not windows API) respectively.
The second tab is "Parameters": configure (add) the various input parameters of the function. First, for return, select the corresponding void type below. Then click the "+" sign to start adding function parameters
First parameter:
Name, enter anything
Type, obviously our first parameter is numeric, double (8 bytes)
The last item is of course a value, not a pointer
Similarly, the second parameter is also a numeric 8-byte double, but the pass is point to value
After completion, the phototype at the bottom shows:
void getSqrt(double num,double *result1);
Click ok.
OK, let's add input and output to the configured file (hehe, output can be left uninitialized, and input can be connected to input). Run it and see the effect!
This is the whole process. To write complex DLL calls, you will have to consider other issues.
Previous article:Newline Issues in LabVIEW String Input Instructions
Next article:Analysis of the method of naming files by time in LabVIEW
- Popular Resources
- Popular amplifiers
- Seizing the Opportunities in the Chinese Application Market: NI's Challenges and Answers
- Tektronix Launches Breakthrough Power Measurement Tools to Accelerate Innovation as Global Electrification Accelerates
- Not all oscilloscopes are created equal: Why ADCs and low noise floor matter
- Enable TekHSI high-speed interface function to accelerate the remote transmission of waveform data
- How to measure the quality of soft start thyristor
- How to use a multimeter to judge whether a soft starter is good or bad
- What are the advantages and disadvantages of non-contact temperature sensors?
- In what situations are non-contact temperature sensors widely used?
- How non-contact temperature sensors measure internal temperature
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- Experience in solving audio signal ground noise based on BA3121
- ESP32S3 ubuntu+vscode environment construction and use internal USB-JTAG (builtin) debugging program
- 【GD32L233C-START review】+P9813_RGBLED test【3】
- Liquid Level Measurement System Based on Single Chip Microcomputer
- UART emulation LIN problem
- Floating point issues with MicroPython
- Apple allows employees to discuss salaries publicly, sparking heated debate. Do you know how much your colleagues earn?
- Current control circuit
- Review summary: TI LAUNCHXL-CC1352P-4
- DE1-SOC development board usage learning