LabVIEW什么时候需要使用DLL封装 Calling External APIs

简介: LabVIEW什么时候需要使用DLL封装 Calling External APIs

LabVIEW什么时候需要使用DLL封装


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


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


Calling External APIs


You might need toaccess external APIs from within LabVIEW code. Most often, you access externalAPIs to obtain functionality that the operating system provides. Normally, youcan use the LabVIEW Call Library Function Node to accomplish this goal. Youmust provide the following information to the Call Library Function Node toaccess external APIs from within LabVIEW code:


Function name asit appears in the library


Functionprototype


Library or modulein which the function resides


Callingconventions of the function


Thread-safestatus of the function


Caution  Do not specify the location of a systemshared library, such as kernel32.dll, by path. Because LabVIEW automaticallyincludes by-path shared libraries in build specifications, you might distributea system shared library to another computer inadvertently. Redistributing someshared libraries, such as kernel32.dll, might cause the target computer tocrash. Therefore, always specify system shared libraries by name.


Common Pitfallswith the Call Library Function Node


The functionreference documentation for any API should provide most of the information thatthe Call Library Function Node requires. However, you should keep in mind thecommon errors listed in this section.


Note  Configure the Call Library Function Node toreturn an error when some of the pitfalls described below occur by selectingthe Maximum or Default control on the Error Checking tab of the Call LibraryFunction dialog box.


IncorrectFunction Name


Your library callcan fail when the name of the function as it appears in the library isdifferent than is expected. Usually this error occurs due to function nameredefinition, or to function name decoration, as in the following examples:


Redefinition—Thispitfall appears when an API manufacturer uses a define mechanism, such as#define directive in ANSI C, to define an abstracted function name to one ofmany functions present in the library, based on some external condition such aslanguage or debug mode. In such cases, you can look in the header ( .h) filefor the API to determine whether a #define directive redefined the name of afunction you want to use.


Function NameDecoration—This pitfall appears when certain functions have their namesdecorated when they are linked. A typical C compiler tracks name decoration,and when it looks for a function in a shared library, it recognizes thedecorated name. However, because LabVIEW is not a C compiler, it does notrecognize decorated names. If you suspect that function name decoration iscausing difficulty, inspect the shared library’s exported functions. If thefunction name that appears in the function prototype section has characterssuch as @ appended to it, the function was decorated when the DLL was built.This is most common with C++ compilers.


In LabVIEW, theFunction name control in the Call Library Function dialog box is a pull-downmenu where you can access a list of all functions within the library you haveselected. In addition, most operating systems have a utility you can use toview a library’s exports, for example, QuickView on the Windows operatingsystem and the nm command on most Linux systems.


If the Functionname list contains entries but the function you want to call does not appear inthe list, the most likely reason is that the function has not been exported.Refer to the documentation for your compiler for information about how to markfunctions for export.


Data Types


Your library callcan fail when you do not use the correct data types. LabVIEW only supportsbasic numeric data types and C strings. Also, you can select Adapt to Type fromthe Type pull-down menu of the Call Library Function dialog box and directLabVIEW to pass its own internal data types for a given parameter. You mightencounter the following specific problems:


Non-Standard DataType Definitions—Frequently, other APIs use non-standard definitions for datatypes. For example, instead of using char, short, and long, the Windows APIuses BYTE, WORD, and DWORD. If an API that you are using makes use of such datatypes, you need to find the equivalent basic C data type so that you canproperly configure the Call Library Function Node.


Structure andClass Data Types—Some APIs have structure and, in the case of C++, class datatypes. LabVIEW cannot use these data types. If you need to use a function thathas a structure or class as an argument, you should write a shared librarywrapper function that takes as inputs the data types that LabVIEW supports andthat appropriately packages them before LabVIEW calls the desired function.


ActiveXObjects—If you want to call a shared library that contains ActiveX objects, usethe Automation Open function with the Property Node and the Invoke Node insteadof the Call Library Function Node.


(Windows) Referto the labview\examples\Connectivity\Libraries and Executables\Libraries andExecutables.lvproj for an example of using data types in shared libraries.


Constants


Your library callcan fail when your external code uses identifiers in place of constants. ManyAPIs define identifiers for constants to make the code easier to read. LabVIEWmust receive the actual value of the constant rather than the identifier that aparticular API uses. Constants are usually numeric, but they might also bestrings or other values. To identify all constants, inspect the header file forthe API to find the definitions. The definition might either be in #definestatements or in enumerations, which use the enum keyword.


CallingConventions


Your library callcan fail when certain operating systems use calling conventions other than theC calling convention and the Standard (__stdcall) calling convention. Thecalling convention defines how data is passed to a function and how clean upoccurs after the function call is complete. The documentation for the API shouldsay which calling convention(s) you must use. The Standard (__stdcall) callingconvention is also known as the WINAPI convention, or the Pascal convention.


Use of callingconventions other than the C or Standard calling conventions frequently causes thefailure of library calls in LabVIEW because those other calling conventions usean incompatible method for maintaining the stack.


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

相关文章
|
7月前
|
存储 Java Android开发
Rockchip系列之UART 新增framework系统jni+service接口访问(2)
Rockchip系列之UART 新增framework系统jni+service接口访问(2)
65 1
|
7月前
|
Java Android开发 C++
Rockchip系列之CAN 新增framework系统jni接口访问(2)
Rockchip系列之CAN 新增framework系统jni接口访问(2)
70 3
|
7月前
|
Java Android开发
Rockchip系列之VendorStorage 新增framework系统jni+service接口访问(3)
Rockchip系列之VendorStorage 新增framework系统jni+service接口访问(3)
81 0
用IAR打开STM8时,出现“Unable to create configuration 'Debug' using tool chain ‘STM8’
用IAR打开STM8时,出现“Unable to create configuration 'Debug' using tool chain ‘STM8’
443 0
|
4月前
|
Dart C语言 Windows
Dart ffi 使用问题之要生成plugin_ffi_sample_bindings_generated.dart文件该如何操作
Dart ffi 使用问题之要生成plugin_ffi_sample_bindings_generated.dart文件该如何操作
|
流计算
Blueprint Subsystem Plug-in description
Blueprint Subsystem Plug-in description
90 0
|
IDE Java API
REST Assured API Automation Testing Ⅰ - Getting Started
REST Assured API Automation Testing Ⅰ - Getting Started
REST Assured API Automation Testing Ⅰ - Getting Started
【VCS】PCIe Native Protocol Analyzer 使用方法
【VCS】PCIe Native Protocol Analyzer 使用方法
412 0
【VCS】PCIe Native Protocol Analyzer 使用方法
|
数据可视化 异构计算
TensorBoard报错:Could not load dynamic library ‘cudart64_110.dll‘; dlerror: cudart64_110.dll not found
TensorBoard报错:Could not load dynamic library ‘cudart64_110.dll‘; dlerror: cudart64_110.dll not found
805 0
TensorBoard报错:Could not load dynamic library ‘cudart64_110.dll‘; dlerror: cudart64_110.dll not found
|
Dart 开发工具 Android开发
【错误记录】Flutter 报错 Downloading the Dart SDK using the BITS service failed, retrying with WebRequest...
【错误记录】Flutter 报错 Downloading the Dart SDK using the BITS service failed, retrying with WebRequest...
563 0
【错误记录】Flutter 报错 Downloading the Dart SDK using the BITS service failed, retrying with WebRequest...