- •Worldwide Technical Support and Product Information
- •National Instruments Corporate Headquarters
- •Worldwide Offices
- •Important Information
- •Warranty
- •Copyright
- •Trademarks
- •WARNING REGARDING USE OF NATIONAL INSTRUMENTS PRODUCTS
- •Contents
- •About This Manual
- •Conventions
- •Related Documentation
- •Calling Code in Various Platforms
- •Characteristics of the Two Calling Approaches
- •Details of Call Library Function
- •Details of a CIN
- •Calling Shared Libraries
- •Figure 2-1. Call Library Function Dialog Box
- •Calling Conventions (Windows)
- •Parameters
- •Calling Functions That Expect Other Data Types
- •Building a Shared Library (DLL)
- •Task 1: Build the Function Prototype in LabVIEW
- •Task 2: Complete the .c File
- •Required Libraries
- •Task 3: Build a Library Project in an External IDE
- •Figure 2-2. Creating a Project in Visual C++
- •Figure 2-3. Setting the Use run-time library control, Microsoft Visual C++
- •Gnu C or C++ Compilers on Solaris, Linux, or HP-UX
- •Metrowerks CodeWarrior on Power Macintosh
- •Calling External APIs
- •Common Pitfalls with the Call Library Function
- •Incorrect Function Name
- •Data Types
- •Constants
- •Calling Conventions
- •Example 1: Call a Shared Library that You Built
- •Configuration of Call Library Function
- •Create Front Panel
- •Create the Block Diagram
- •Example 2: Call a Hardware Driver API
- •Figure 2-4. VI That Calls Hardware
- •Example 3: Call the Win32 API
- •Table 2-1. Mapping Win32 Data Types to Standard C Data Types
- •Table 2-2. Mapping Win32 Data Types to LabVIEW Data Types
- •Constants
- •Table 2-3. Selected Constants for MessageBox
- •Figure 2-5. Combining Function Constants in LabVIEW
- •Determining the Proper Library and Function Name
- •Unicode Versions and ANSI Versions of Functions
- •Configuring a Call to the Win32 API
- •Figure 2-6. Configuring Call Library Function to call the Win32 API
- •Figure 2-7. Block Diagram for a Call to the Win32 API
- •Figure 2-8. Running a LabVIEW Call to the Win32 API
- •Additional Examples of LabVIEW Calls to DLLs
- •Debugging DLLs and Calls to DLLs
- •Troubleshooting the Call Library Function
- •Troubleshooting your DLL
- •Troubleshooting Checklist
- •Module Definition Files
- •Array and String Options
- •Arrays of Numeric Data
- •String Data
- •Figure 2-9. The LabVIEW String Format
- •Figure 2-10. The Pascal String Format
- •Figure 2-11. The C String Format
- •Array and String Tip
- •Supported Languages
- •Macintosh
- •Microsoft Windows
- •Solaris, Linux, and HP-UX
- •Resolving Multithreading Issues
- •Making LabVIEW Recognize a CIN as Thread Safe
- •Using C Code that is Thread Safe
- •Creating a CIN
- •Step 1. Set Up Input and Output Terminals for a CIN
- •Input-Output Terminals
- •Output-Only Terminals
- •Step 2. Wire the Inputs and Outputs to the CIN
- •Step 3. Create a .c File
- •Step 4. Compile the CIN Source Code
- •Compile on Macintosh
- •Microsoft Windows
- •Solaris 2.x
- •HP-UX and Linux
- •gcc Compiler
- •Step 5. Load the CIN Object Code
- •LabVIEW Manager Routines
- •Pointers as Parameters
- •Debugging External Code
- •DbgPrintf
- •Windows
- •UNIX
- •Passing Parameters
- •Parameters in the CIN .c File
- •Passing Fixed-Size Data to CINs
- •Scalar Numerics
- •Scalar Booleans
- •Refnums
- •Clusters of Scalars
- •Return Value for CIN Routines
- •Examples with Scalars
- •Creating a CIN That Multiplies Two Numbers
- •Passing Variably Sized Data to CINs
- •Alignment Considerations
- •Arrays and Strings
- •Paths
- •Clusters Containing Variably Sized Data
- •Resizing Arrays and Strings
- •SetCINArraySize
- •NumericArrayResize
- •Examples with Variably Sized Data
- •Concatenating Two Strings
- •Working with Clusters
- •Manager Overview
- •Basic Data Types
- •Scalar
- •char
- •Dynamic
- •Memory-Related
- •Constants
- •Memory Manager
- •Memory Allocation
- •Memory Zones
- •Using Pointers and Handles
- •File Manager
- •Identifying Files and Directories
- •Path Specifications
- •File Descriptors
- •File Refnums
- •Support Manager
- •CIN Routines
- •Data Spaces and Code Resources
- •One Reference to the CIN in a Single VI
- •Loading a VI
- •Unloading a VI
- •Loading a New Resource into the CIN
- •Compiling a VI
- •Running a VI
- •Saving a VI
- •Aborting a VI
- •Multiple References to the Same CIN in a Single VI
- •Multiple References to the Same CIN in Different VIs
- •Single-Threaded Operating Systems
- •Multithreaded Operating Systems
- •Code Globals and CIN Data Space Globals
- •Examples
- •Memory Manager Functions
- •Support Manager Functions
- •Mathematical Operations
- •ASCIITime
- •AZCheckHandle/DSCheckHandle
- •AZCheckPtr/DSCheckPtr
- •AZDisposeHandle/DSDisposeHandle
- •AZDisposePtr/DSDisposePtr
- •AZGetHandleSize/DSGetHandleSize
- •AZHandAndHand/DSHandAndHand
- •AZHandToHand/DSHandToHand
- •AZHeapCheck/DSHeapCheck
- •AZHLock
- •AZHNoPurge
- •AZHPurge
- •AZHUnlock
- •AZMaxMem/DSMaxMem
- •AZMemStats/DSMemStats
- •AZNewHandle/DSNewHandle
- •AZNewHClr/DSNewHClr
- •AZNewPClr/DSNewPClr
- •AZNewPtr/DSNewPtr
- •AZPtrAndHand/DSPtrAndHand
- •AZPtrToHand/DSPtrToHand
- •AZPtrToXHand/DSPtrToXHand
- •AZRecoverHandle/DSRecoverHandle
- •AZSetHandleSize/DSSetHandleSize
- •AZSetHSzClr/DSSetHSzClr
- •BinSearch
- •BlockCmp
- •Cat4Chrs
- •ClearMem
- •CPStrBuf
- •CPStrCmp
- •CPStrIndex
- •CPStrInsert
- •CPStrLen
- •CPStrRemove
- •CPStrReplace
- •CPStrSize
- •CToPStr
- •DateCString
- •DateToSecs
- •FAddPath
- •FAppendName
- •FAppPath
- •FArrToPath
- •FCopy
- •FCreate
- •FCreateAlways
- •FDepth
- •FDirName
- •FDisposePath
- •FDisposeRefNum
- •FEmptyPath
- •FExists
- •FFlattenPath
- •FFlush
- •FGetAccessRights
- •FGetDefGroup
- •FGetEOF
- •FGetInfo
- •FGetPathType
- •FGetVolInfo
- •FileNameCmp
- •FileNameIndCmp
- •FileNameNCmp
- •FIsAPath
- •FIsAPathOfType
- •FIsAPathOrNotAPath
- •FIsARefNum
- •FIsEmptyPath
- •FListDir
- •FLockOrUnlockRange
- •FMakePath
- •FMClose
- •FMOpen
- •FMove
- •FMRead
- •FMSeek
- •FMTell
- •FMWrite
- •FName
- •FNamePtr
- •FNewDir
- •FNewRefNum
- •FNotAPath
- •FPathCmp
- •FPathCpy
- •FPathToArr
- •FPathToAZString
- •FPathToDSString
- •FPathToPath
- •FRefNumToFD
- •FRefNumToPath
- •FRelPath
- •FRemove
- •FSetAccessRights
- •FSetEOF
- •FSetInfo
- •FSetPathType
- •FStrFitsPat
- •FStringToPath
- •FTextToPath
- •FUnFlattenPath
- •FVolName
- •GetALong
- •HexChar
- •HiByte
- •HiNibble
- •IsAlpha
- •IsDigit
- •IsLower
- •IsUpper
- •LoByte
- •Long
- •LoNibble
- •LStrBuf
- •LStrCmp
- •LStrLen
- •LToPStr
- •MilliSecs
- •MoveBlock
- •NumericArrayResize
- •Offset
- •PPStrCaseCmp
- •PPStrCmp
- •Printf
- •PStrBuf
- •PStrCaseCmp
- •PStrCat
- •PStrCmp
- •PStrCpy
- •PStrLen
- •PStrNCpy
- •PToCStr
- •PToLStr
- •QSort
- •RandomGen
- •SecsToDate
- •SetALong
- •SetCINArraySize
- •StrCat
- •StrCmp
- •StrCpy
- •StrLen
- •StrNCaseCmp
- •StrNCmp
- •StrNCpy
- •SwapBlock
- •TimeCString
- •TimeInSecs
- •ToLower
- •ToUpper
- •Unused
- •Word
- •Glossary
Chapter 2 Shared Libraries (DLLs)
Metrowerks CodeWarrior on Power Macintosh
Create a shared library using the process that the Metrowerks documentation describes. To use this shared library with LabVIEW, you must set struct alignment to 68k in the PPC Processor settings panel. Be sure to export the function(s) that you want to call from LabVIEW.
Calling External APIs
It is frequently desirable to access external APIs from within LabVIEW code. Most often, a LabVIEW programmer accesses external APIs to obtain functionality that the operating system provides. Normally, you can use the LabVIEW Call Library Function object to accomplish this goal. You must provide the following information to the Call Library Function.
•Function name as it appears in the library
•Function prototype
•Library or module in which the function resides
•Calling conventions of the function
•Thread-safe status of the function
Common Pitfalls with the Call Library Function
The function reference documentation for any API should provide most of the information that Call Library Function requires. However, you should keep in mind the common errors listed in this section.
Incorrect Function Name
Your library call can fail when the name of the function as it appears in the library is different than is expected. Usually this error occurs due to function name redefinition, or to function name decoration, as in the following examples:
•Redefinition—This pitfall appears when an API manufacturer uses a define mechanism, such as #define directive in ANSI C, to define an abstracted function name to one of many functions present in the library, based on some external condition such as language or debug mode. In such cases, you can look in the header (.h) file for the API to determine whether a #define directive redefined the name of a function you want to use.
•Function Name Decoration—This pitfall appears when certain functions have their names decorated when they are linked. A typical C compiler tracks name decoration, and when it looks for a function in
Using External Code in LabVIEW |
2-14 |
www.ni.com |
Chapter 2 Shared Libraries (DLLs)
a shared library, it recognizes the decorated name. However, because LabVIEW is not a C compiler, it does not recognize decorated names. If you suspect that function name decoration is causing difficulty, inspect the shared library’s exported functions. In LabVIEW 6.0, the
Function Name control in the Call Library Function dialog box is a pull-down list where you can access a list of all functions within the library you have selected. In addition, most operating systems have a utility you can use to view a library’s exports, for example, QuickView on the Windows operating system and the nm command on most UNIX systems.
Data Types
Your library call can fail when you do not use the correct data types. LabVIEW only supports basic numeric data types and C strings. Also, you can select Adapt to Type in the Type control of the Call Library Function dialog box and direct LabVIEW to pass its own internal data types for a given parameter. You might encounter the following specific problems:
•Non-Standard Data Type Definitions—Frequently, other APIs use non-standard definitions for data types. For example, instead of using char, short, and long, the Windows API uses BYTE, WORD, and DWORD. If an API that you are using makes use of such data types, you need to find the equivalent basic C data type so that you can properly configure the Call Library Function object. The Example 3: Call the Win32 API section presents an example of this process.
•Structure and Class Data Types—Some APIs have structure and, in the case of C++, class data types. LabVIEW cannot use these data types. If you need to use a function that has a structure or class as an argument, you should write a CIN or shared library wrapper function that takes as inputs the data types that LabVIEW supports and that appropriately packages them before LabVIEW calls the desired function.
Constants
Your library call can fail when your external code uses identifiers in place of constants. Many APIs define identifiers for constants to make the code easier to read. LabVIEW must receive the actual value of the constant, rather than the identifier that a particular API uses. Constants are usually numeric, but they may also be strings or other values. To identify all constants, inspect the header file for the API to find the definitions. The definition may either be in #define statements, or in enumerations, which
© National Instruments Corporation |
2-15 |
Using External Code in LabVIEW |