LabVIEW调用C/C++ DLLs

简介: LabVIEW调用C/C++ DLLs

LabVIEW调用C/C++ DLLs


什么是DLL封装,什么时候需要使用DLL封装?


解决方案


所谓封装就是指为另一种软件提供兼容性接口的程序。由于第三方DLL的开发设计通常是为了从C语言(或者其它类似的底层语言)中调用,而不从是LabVIEW中调用,因此当使用LabVIEW进行应用程序开发时,往往会用到封装。例如,有的DLL会返回指针或者其它复杂的数据结构,而这在LabVIEW中却无法很容易的实现。


编写一个DLL封装,可以类比在C语言环境下,按照该DLL原始开发者设计的调用DLL方式,来编写一个完全独立的程序。反过来,这个封装程序也是专门针对在LabVIEW中调用DLL所设计的。也就是说,这个新的用C编写的“封装” 程序将原来的C程序(DLL)封装起来,形成一个接口层。使用封装的好处在于,不再需要使用原来的DLL的源代码,也不需要对这些源代码进行任何改动。

Calling C/C++ DLLs ContainingSimple and Complex Datatypes from LabVIEW


Overview


LabVIEWdevelopers often have to call C/C++ DLLs from LabVIEW and because of thedifferences between LabVIEW and C/C++ datatypes, passing and receiving datafrom these DLLs can be intimidating at first.


This exampleseeks to show developers how to pass and receive different types of dataranging from simple datatypes like numerics (int, float, double, etc), arraysand strings to more complex data types like pointers, struct (cluster), arraysof structs, and even arrays of structs that have arrays of structs in them.


In addition,this example seeks to show developers the different ways to pass and receivedata in LabVIEW with a DLL – when functions pass data by value, return datausing a return statement, or return data using pass by reference.


This examplecovers the following use cases:

DataType

Call Type

 

1. Numeric (Integer)

a) Returning a value (return statement)

2. Array of Numerics

b) Returning a pointer (return statement)

3. Strings

c) Passing a parameter (Pass by value)

4. 2 Dimensional Array

d) Returning values by reference (pass by ref)

5. Simple Struct (with basic datatypes)

6. Complex Struct (with structs & arrays)

Attached Files


LabVIEWWrapper.zip: LabVIEW Library (.lvlib)     containing examples on how to call each individual function.


PassingDataSampleDLL.zip: Source code for the ANSI C     DLL as well as the compiled DLL. Written in CVI, but tested to compile in     Visual Studio as well.


CTestApplication.zip: Examples of calling the C DLL     from ANSI C. Written in Visual Studio.


Example Hierarchy


The majority ofVIs in the example were automatically generated using the Import Shared Library Wizard. The wizarddoes not cover all cases, and could not generate VI wrappers for somefunctions, and some VI wrappers were incomplete. However, it gives us a reallygood starting point for calling our DLL.


For an exampleon using the Import Shared Library Wizard, refer to:

Tutorial: Creating Wrapper VIs for C/C++ DLL functionsusing the Import Shared Library Wizard


For anexplanation on what the Import Shared Library Wizard missed, refer to thesection titled Caveats with the Import Shared Library Wizard.


The VIs in theexample are arranged in the following virtual folders in the library:


Import Shared Library Wizard Generated VIs: These are the     auto-generated VIs that were the output of the Import Shared Library     Wizard


Dereferencing Pointers: VIs that assist     with dereferencing pointers. They use either MoveBlock or     GetValueByPointer. See:

Dereferencing Pointers from C/C++ DLLs in LabVIEW


Custom Controls For Structs: Strict Typedef     controls that represent the struct datatypes in the DLL


Completed, Corrected and Added VIs: Additional VIs that     were added in order to work around the caveats encountered using the     Import Shared Library Wizard


Caveats with the Import SharedLibrary Wizard


The followingcases were not completely handled by the Import Shared Library Wizard:


VI Not Generated


In the followingcase, VIs were not generated at all:


When a function returns anything other than a     numeric, string or void. Pointers are fine because they are treated as     numerics.

    In these cases, the data should be treated as a pointer and the data type     in LabVIEW for the return value should be set as Unsigned Pointer-Sized     Integer


In the examplelvlib, these VIs were manually created and given the suffix “Added” to thename.


Incomplete VIs


In the followingcases, the VIs that were generated were incomplete in some fashion:


When a function returned a pointer and the pointer     wasn’t automatically dereferenced.

    In these cases, the pointer needs to be dereferenced using either     MoveBlock or GetValueByPointer. See:

Dereferencing Pointers from C/C++ DLLs in LabVIEW


In the examplelvlib, these VIs completed in a new VI that has the suffix “Complete” in thename.


Incorrect VIs


In the followingcases, the VIs that were generated had some incorrect behavior:


When a function returned a pointer to a pointer     (strings and 2D Arrays)

    These must be dereferenced twice.


When a function took a struct (cluster) with an     array/string in it

    The individual elements should be dereferenced individually.


In the examplelvlib, these VIs were corrected in a new VI and given the suffix “Corrected” tothe name.


List of Functions and VIs (byData Type and Call Type)


The following isa list of functions exposed by the C DLL as well as the VI that shows how toproperly call the particular function.


1.     Numeric (Integer)  


a.     Returning a value (return statement)

Function: int ReturningAValue_Integer (void);

VI: Returning A ValueInteger.vi

Auto Generated VI Status: Working    


b.     Returning a pointer (return statement)

Function: int*ReturningAValue_PointerToInteger (void);

VI: Returning A Value Pointer To Integer Complete.vi

Auto Generated VI Status: Incomplete    


c.     Passing a parameter (Pass by value)

Function: int PassingParameters_Integer(int x, int y);

VI: Passing Parameters Integer.vi

Auto Generated VI Status: Working    


d.     Returning values by reference (pass by ref)

Function: voidReturningValuesByReference_Integer (int x, int y, int *sum);

VI: Returning ValuesBy Reference Integer.vi

Auto Generated VI Status: Working    


2.     Array of Numerics  


a.     Returning a value (return statement)

Function: int*ReturningAValue_ArrayOfIntegers (int length);

VI: Returning A ValueArray Of Integers Complete.vi

Auto Generated VI Status: Incomplete    


b.     Returning a pointer (return statement)

Function: N/A (arrays variables are already pointers)

VI: N/A

Auto Generated VI Status: N/A    


c.     Passing a parameter (Pass by value)

Function: intPassingParamters_ArrayOfIntegers (int x[], int length);

VI: Passing ParamtersArray Of Integers.vi

Auto Generated VI Status: Working    


d.     Returning values by reference (pass by ref)

Function: void ReturningValuesByReference_ArrayOfIntegers (int *x, int length,int **newArray, int *newLength);

VI: Returning ValuesBy Reference Array Of Integers Complete.vi

Auto Generated VI Status: Incomplete    


3.     Strings  


a.     Returning a   value (return statement)

Function: char*ReturningAValue_String (void);

VI: Returning A ValueString.vi

Auto Generated VI Status: Working    


b.     Returning a pointer (return statement)

Function: N/A (arrays variables are already pointers)

VI:N/A

Auto Generated VI Status: N/A    


c.     Passing a parameter (Pass by value)

Function: intPassingParamters_String (char *str);

VI: Passing ParamtersString.vi

Auto Generated VI Status: Working    


d.     Returning values by reference (pass by ref)

Function: voidReturningValuesByReference_String (char *str, char **newString);

VI: Returning ValuesBy Reference String Corrected.vi

Auto Generated VI Status: Incorrect    


4.     2 Dimensional Array  


a.     ReturningAValue_2DArrayOfIntegers

Function: int**ReturningAValue_2DArrayOfIntegers (int rows, int cols);

VI: Returning A Value2D Array Of Integers Complete.vi

Auto Generated VI Status: Incomplete    


b.     Returning a pointer (return statement)

Function: N/A (arrays variables are already pointers)

VI: N/A

Auto Generated VI Status: N/A    


c.     Passing a parameter (Pass by value)

Function: intPassingParamters_2DArrayOfIntegers (int *x, int rows, int cols);

VI: Passing Paramters2D Array Of Integers Corrected.vi

Auto Generated VI Status: Incorrect    


d.     Returning values by reference (pass by ref)

Function: voidReturningValuesByReference_2DArrayOfIntegers (int rows, int cols, int***newArray);

VI: Returning ValuesBy Reference 2D Array Of Integers Complete.vi

Auto Generated VI Status: Incomplete    


5.     Simple Struct (with basic datatypes)  


a.     Returning a   value (return statement)

Function: structsimpleStructCircle ReturningAValue_SimpleStruct(void);

VI: Returning A ValueSimple Struct Added.vi

Auto Geerated VI Status: Not Generated    


b.     Returning a pointer (return statement)

Function: structsimpleStructCircle* ReturningAValue_PointerToSimpleStruct(void);

VI: Returning A ValuePointer To Simple Struct Added.vi

Auto Generated VI Status: Not Generated    


c.     Passing a parameter (Pass by value)

Function: floatPassingParamters_SimpleStruct (struct simpleStructCircle circle);

VI: Passing ParamtersSimple Struct.vi

Auto Generated VI Status: Working    


d.     Returning values by reference (pass by ref)

Function: voidReturningValuesByReference_SimpleStruct (struct simpleStructCircle circle,struct simpleStructCircle *largerCircle);

VI: Returning ValuesBy Reference Simple Struct.vi

Auto Generated VI Status: Working    


e.     Returning array of struct

Function: voidReturningValuesByReference_ArrayOfSimpleStruct (struct simpleStructCircle**circleArray, int length);

VI: Returning ValuesBy Reference Array Of Simple Struct Complete.vi

Auto Generated VI Status: Incomplete  


6.     Complex Struct (with structs and arrays)  


a.     Returning a   value (return statement)

Function: structcomplexStructPolygon ReturningAValue_ComplexStruct (void);

VI: Returning A ValueComplex Struct Added.vi

Auto Generated VI Status: Not Generated    


b.     Returning a pointer (return statement)

Function: structcomplexStructPolygon* ReturningAValue_PointerToComplexStruct (void);

VI: Returning A ValuePointer To Complex Struct Added.vi

Auto Generated VI Status: Not Generated    


c.     Passing a parameter (Pass by value)

Function: intPassingParamters_ComplexStruct (struct complexStructPolygon triangle);

VI: Passing ParamtersComplex Struct Corrected.vi

Auto Generated VI Status: Incorrect    


d.     Returning values by reference (pass by ref)

Function: voidReturningValuesByReference_PointerToComplexStruct (struct complexStructPolygon*triangle);

VI: Returning ValuesBy Reference Pointer To Complex Struct Corrected.vi

Auto Generated VI Status: Incorrect    


e.      Returning array of struct

Function: void ReturningValuesByReference_ArrayOfComplexStruct(struct complexStructPolygon **triangles, int length);

VI: Returning ValuesBy Reference Array Of Complex Struct Corrected.vi

Auto Generated VI Status: Incorrect        


需要说明的是,上述的例程和文档,都是可以下载的,双击即可打开,其中压缩文件是可以采用粘贴复制的方式,拷贝到硬盘上。这不是图片,各位小伙伴看到后尝试一下,这个问题就不用加微信咨询了。有关LabVIEW编程、LabVIEW开发等相关项目,可联系们。

相关文章
|
8月前
|
C++ 数据格式
LabVIEW传递接收C/C++DLL指针
LabVIEW传递接收C/C++DLL指针
226 1
|
8月前
|
存储 C++ Python
LabVIEW使用Python MathWorks® MATLAB®软件和C/C++
LabVIEW使用Python MathWorks® MATLAB®软件和C/C++
91 0
Using Microsoft Visual C++ DLLs with C++Builder
摘自《Borland C++BuilderT 6 Developer's Guide》  一书 Using Microsoft Visual C++ DLLs with C++Builder As powerful as C++Builder is, the majority of DLLs d...
1120 0
|
10天前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
51 18
|
10天前
|
存储 编译器 数据安全/隐私保护
【C++面向对象——类与对象】CPU类(头歌实践教学平台习题)【合集】
声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,以及两个公有成员函数run、stop。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。​ 相关知识 类的声明和使用。 类的声明和对象的声明。 构造函数和析构函数的执行。 一、类的声明和使用 1.类的声明基础 在C++中,类是创建对象的蓝图。类的声明定义了类的成员,包括数据成员(变量)和成员函数(方法)。一个简单的类声明示例如下: classMyClass{ public: int
37 13
|
11天前
|
编译器 数据安全/隐私保护 C++
【C++面向对象——继承与派生】派生类的应用(头歌实践教学平台习题)【合集】
本实验旨在学习类的继承关系、不同继承方式下的访问控制及利用虚基类解决二义性问题。主要内容包括: 1. **类的继承关系基础概念**:介绍继承的定义及声明派生类的语法。 2. **不同继承方式下对基类成员的访问控制**:详细说明`public`、`private`和`protected`继承方式对基类成员的访问权限影响。 3. **利用虚基类解决二义性问题**:解释多继承中可能出现的二义性及其解决方案——虚基类。 实验任务要求从`people`类派生出`student`、`teacher`、`graduate`和`TA`类,添加特定属性并测试这些类的功能。最终通过创建教师和助教实例,验证代码
37 5
|
11天前
|
存储 算法 搜索推荐
【C++面向对象——群体类和群体数据的组织】实现含排序功能的数组类(头歌实践教学平台习题)【合集】
1. **相关排序和查找算法的原理**:介绍直接插入排序、直接选择排序、冒泡排序和顺序查找的基本原理及其实现代码。 2. **C++ 类与成员函数的定义**:讲解如何定义`Array`类,包括类的声明和实现,以及成员函数的定义与调用。 3. **数组作为类的成员变量的处理**:探讨内存管理和正确访问数组元素的方法,确保在类中正确使用动态分配的数组。 4. **函数参数传递与返回值处理**:解释排序和查找函数的参数传递方式及返回值处理,确保函数功能正确实现。 通过掌握这些知识,可以顺利地将排序和查找算法封装到`Array`类中,并进行测试验证。编程要求是在右侧编辑器补充代码以实现三种排序算法
26 5
|
11天前
|
Serverless 编译器 C++
【C++面向对象——类的多态性与虚函数】计算图像面积(头歌实践教学平台习题)【合集】
本任务要求设计一个矩形类、圆形类和图形基类,计算并输出相应图形面积。相关知识点包括纯虚函数和抽象类的使用。 **目录:** - 任务描述 - 相关知识 - 纯虚函数 - 特点 - 使用场景 - 作用 - 注意事项 - 相关概念对比 - 抽象类的使用 - 定义与概念 - 使用场景 - 编程要求 - 测试说明 - 通关代码 - 测试结果 **任务概述:** 1. **图形基类(Shape)**:包含纯虚函数 `void PrintArea()`。 2. **矩形类(Rectangle)**:继承 Shape 类,重写 `Print
32 4
|
11天前
|
设计模式 IDE 编译器
【C++面向对象——类的多态性与虚函数】编写教学游戏:认识动物(头歌实践教学平台习题)【合集】
本项目旨在通过C++编程实现一个教学游戏,帮助小朋友认识动物。程序设计了一个动物园场景,包含Dog、Bird和Frog三种动物。每个动物都有move和shout行为,用于展示其特征。游戏随机挑选10个动物,前5个供学习,后5个用于测试。使用虚函数和多态实现不同动物的行为,确保代码灵活扩展。此外,通过typeid获取对象类型,并利用strstr辅助判断类型。相关头文件如<string>、<cstdlib>等确保程序正常运行。最终,根据小朋友的回答计算得分,提供互动学习体验。 - **任务描述**:编写教学游戏,随机挑选10个动物进行展示与测试。 - **类设计**:基类
26 3
|
2月前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
76 2