Calling DLL files generated by VC++ in LabVIEW

Publisher:yanfeng00Latest update time:2016-08-03 Source: eefocusKeywords:LabVIEW Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. // Create a new empty Win32 Dll project, add a new cpp, and write the following code 

  _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<           else 
              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.

Keywords:LabVIEW Reference address:Calling DLL files generated by VC++ in LabVIEW

Previous article:Newline Issues in LabVIEW String Input Instructions
Next article:Analysis of the method of naming files by time in LabVIEW

Latest Test Measurement Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号