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开发等相关项目,可联系们。

相关文章
|
7月前
|
C++ 数据格式
LabVIEW传递接收C/C++DLL指针
LabVIEW传递接收C/C++DLL指针
208 1
|
7月前
|
存储 C++ Python
LabVIEW使用Python MathWorks® MATLAB®软件和C/C++
LabVIEW使用Python MathWorks® MATLAB®软件和C/C++
69 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...
1111 0
|
26天前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
44 2
|
1月前
|
存储 编译器 C++
【c++】类和对象(下)(取地址运算符重载、深究构造函数、类型转换、static修饰成员、友元、内部类、匿名对象)
本文介绍了C++中类和对象的高级特性,包括取地址运算符重载、构造函数的初始化列表、类型转换、static修饰成员、友元、内部类及匿名对象等内容。文章详细解释了每个概念的使用方法和注意事项,帮助读者深入了解C++面向对象编程的核心机制。
94 5
|
1月前
|
存储 编译器 C++
【c++】类和对象(中)(构造函数、析构函数、拷贝构造、赋值重载)
本文深入探讨了C++类的默认成员函数,包括构造函数、析构函数、拷贝构造函数和赋值重载。构造函数用于对象的初始化,析构函数用于对象销毁时的资源清理,拷贝构造函数用于对象的拷贝,赋值重载用于已存在对象的赋值。文章详细介绍了每个函数的特点、使用方法及注意事项,并提供了代码示例。这些默认成员函数确保了资源的正确管理和对象状态的维护。
81 4
|
1月前
|
存储 编译器 Linux
【c++】类和对象(上)(类的定义格式、访问限定符、类域、类的实例化、对象的内存大小、this指针)
本文介绍了C++中的类和对象,包括类的概念、定义格式、访问限定符、类域、对象的创建及内存大小、以及this指针。通过示例代码详细解释了类的定义、成员函数和成员变量的作用,以及如何使用访问限定符控制成员的访问权限。此外,还讨论了对象的内存分配规则和this指针的使用场景,帮助读者深入理解面向对象编程的核心概念。
90 4
|
2月前
|
存储 编译器 对象存储
【C++打怪之路Lv5】-- 类和对象(下)
【C++打怪之路Lv5】-- 类和对象(下)
31 4
|
2月前
|
编译器 C语言 C++
【C++打怪之路Lv4】-- 类和对象(中)
【C++打怪之路Lv4】-- 类和对象(中)
32 4
|
2月前
|
存储 安全 C++
【C++打怪之路Lv8】-- string类
【C++打怪之路Lv8】-- string类
26 1