Using External Code
in LabVIEW
Using External Code in LabVIEW
July 2000 Edition
Part Number 370109A-01
Worldwide Technical Support and Product Information
www.ni.com
National Instruments Corporate Headquarters
11500 North Mopac Expressway Austin, Texas 78759-3504 USA Tel: 512 794 0100
Worldwide Offices
Australia 03 9879 5166, Austria 0662 45 79 90 0, Belgium 02 757 00 20, Brazil 011 284 5011,
Canada (Calgary) 403 274 9391, Canada (Ontario) 905 785 0085, Canada (Québec) 514 694 8521,
China 0755 3904939, Denmark 45 76 26 00, Finland 09 725 725 11, France 01 48 14 24 24,
Germany 089 741 31 30, Greece 30 1 42 96 427, Hong Kong 2645 3186, India 91805275406,
Israel 03 6120092, Italy 02 413091, Japan 03 5472 2970, Korea 02 596 7456, Mexico (D.F.) 5 280 7625,
Mexico (Monterrey) 8 357 7695, Netherlands 0348 433466, New Zealand 09 914 0488, Norway 32 27 73 00,
Poland 0 22 528 94 06, Portugal 351 1 726 9011, Singapore 2265886, Spain 91 640 0085,
Sweden 08 587 895 00, Switzerland 056 200 51 51, Taiwan 02 2528 7227, United Kingdom 01635 523545
For further support information, see the Technical Support Resources appendix. To comment on the
documentation, send e-mail to techpubs@ni.com
© Copyright 1993, 2000 National Instruments Corporation. All rights reserved.
Important Information
Warranty
The media on which you receive National Instruments software are warranted not to fail to execute programming instructions,
due to defects in materials and workmanship, for a period of 90 days from date of shipment, as evidenced by receipts or other
documentation. National Instruments will, at its option, repair or replace software media that do not execute programming
instructions if National Instruments receives notice of such defects during the warranty period. National Instruments does not
warrant that the operation of the software shall be uninterrupted or error free.
A Return Material Authorization (RMA) number must be obtained from the factory and clearly marked on the outside of
the package before any equipment will be accepted for warranty work. National Instruments will pay the shipping costs of
returning to the owner parts which are covered by warranty.
National Instruments believes that the information in this document is accurate. The document has been carefully reviewed
for technical accuracy. In the event that technical or typographical errors exist, National Instruments reserves the right to
make changes to subsequent editions of this document without prior notice to holders of this edition. The reader should consult
National Instruments if errors are suspected. In no event shall National Instruments be liable for any damages arising out of
or related to this document or the information contained in it.
E
XCEPT AS SPECIFIED HEREIN,NATIONAL INSTRUMENTS MAKES NO WARRANTIES, EXPRESS OR IMPLIED, AND SPECIFICALLY DISCLAIMS ANY
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
.CUSTOMER’S RIGHT TO RECOVER DAMAGES CAUSED BY FAULT OR
NEGLIGENCE ON THE PART OF
NATIONAL INSTRUMENTS SHALL BE LIMITED TO THE AMOUNT THERETOFORE PAID BY THE CUSTOMER.NATIONAL
INSTRUMENTS WILL NOT BE LIABLE FOR DAMAGES RESULTING FROM LOSS OF DATA, PROFITS, USE OF PRODUCTS, OR INCIDENTAL OR
CONSEQUENTIAL DAMAGES
, EVEN IF ADVISED OF THE POSSIBILITY THEREOF. This limitation of the liability of National Instruments will
apply regardless of the form of action, whether in contract or tort, including negligence. Any action against National Instruments
must be brought within one year after the cause of action accrues. National Instruments shall not be liable for any delay in
performance due to causes beyond its reasonable control. The warranty provided herein does not cover damages, defects,
malfunctions, or service failures caused by owner’s failure to follow the National Instruments installation, operation, or
maintenance instructions; owner’s modification of the product; owner’s abuse, misuse, or negligent acts; and power failure or
surges, fire, flood, accident, actions of third parties, or other events outside reasonable control.
Copyright
Under the copyright laws, this publication may not be reproduced or transmitted in any form, electronic or mechanical, including
photocopying, recording, storing in an information retrieval system, or translating, in whole or in part, without the prior written
consent of National Instruments Corporation.
Trademarks
CVI
™
,LabVIEW
™
, National Instruments
™
, and ni.com
™
are trademarks of National Instruments Corporation.
Product and company names mentioned herein are trademarks or trade names of their respective companies.
WARNING REGARDING USE OF NATIONAL INSTRUMENTS PRODUCTS
(1) NATIONAL INSTRUMENTS PRODUCTS ARE NOT DESIGNED WITH COMPONENTS AND TESTING FOR A LEVEL
OF RELIABILITY SUITABLE FOR USE IN OR IN CONNECTION WITH SURGICAL IMPLANTS OR AS CRITICAL
COMPONENTS IN ANY LIFE SUPPORT SYSTEMS WHOSE FAILURE TO PERFORM CAN REASONABLY BE
EXPECTED TO CAUSE SIGNIFICANT INJURY TO A HUMAN.
(2) IN ANY APPLICATION, INCLUDING THE ABOVE, RELIABILITY OF OPERATION OF THE SOFTWARE PRODUCTS
CAN BE IMPAIRED BY ADVERSE FACTORS, INCLUDING BUT NOT LIMITED TO FLUCTUATIONS IN ELECTRICAL
POWER SUPPLY, COMPUTER HARDWARE MALFUNCTIONS, COMPUTER OPERATING SYSTEM SOFTWARE
FITNESS, FITNESS OF COMPILERS AND DEVELOPMENT SOFTWARE USED TO DEVELOP AN APPLICATION,
INSTALLATION ERRORS, SOFTWARE AND HARDWARE COMPATIBILITY PROBLEMS, MALFUNCTIONS OR
FAILURES OF ELECTRONIC MONITORING OR CONTROL DEVICES, TRANSIENT FAILURES OF ELECTRONIC
SYSTEMS (HARDWARE AND/OR SOFTWARE), UNANTICIPATED USES OR MISUSES, OR ERRORS ON THE PART OF
THE USER OR APPLICATIONS DESIGNER (ADVERSE FACTORS SUCH AS THESE ARE HEREAFTER
COLLECTIVELY TERMED “SYSTEM FAILURES”). ANY APPLICATION WHERE A SYSTEM FAILURE WOULD
CREATE A RISK OF HARM TO PROPERTY OR PERSONS (INCLUDING THE RISK OF BODILY INJURY AND DEATH)
SHOULD NOT BE RELIANT SOLELY UPON ONE FORM OF ELECTRONIC SYSTEM DUE TO THE RISK OF SYSTEM
FAILURE. TO AVOID DAMAGE, INJURY, OR DEATH, THE USER OR APPLICATION DESIGNER MUST TAKE
REASONABLY PRUDENT STEPS TO PROTECT AGAINST SYSTEM FAILURES, INCLUDING BUT NOT LIMITED TO
BACK-UP OR SHUT DOWN MECHANISMS. BECAUSE EACH END-USER SYSTEM IS CUSTOMIZED AND DIFFERS
FROM NATIONAL INSTRUMENTS' TESTING PLATFORMS AND BECAUSE A USER OR APPLICATION DESIGNER
MAY USE NATIONAL INSTRUMENTS PRODUCTS IN COMBINATION WITH OTHER PRODUCTS IN A MANNER NOT
EVALUATED OR CONTEMPLATED BY NATIONAL INSTRUMENTS, THE USER OR APPLICATION DESIGNER IS
ULTIMATELY RESPONSIBLE FOR VERIFYING AND VALIDATING THE SUITABILITY OF NATIONAL
INSTRUMENTS PRODUCTS WHENEVER NATIONAL INSTRUMENTS PRODUCTS ARE INCORPORATED IN A
SYSTEM OR APPLICATION, INCLUDING, WITHOUT LIMITATION, THE APPROPRIATE DESIGN, PROCESS AND
SAFETY LEVEL OF SUCH SYSTEM OR APPLICATION.
© National Instruments Corporation v Using External Code in LabVIEW
Contents
About This Manual
Conventions ...................................................................................................................xiii
Related Documentation..................................................................................................xiv
Chapter 1
Introduction
Calling Code in Various Platforms................................................................................1-1
Characteristics of the Two Calling Approaches ............................................................1-2
Details of Call Library Function......................................................................1-3
Details of a CIN...............................................................................................1-3
Chapter 2
Shared Libraries (DLLs)
Calling Shared Libraries................................................................................................2-1
Calling Conventions (Windows).....................................................................2-3
Parameters .......................................................................................................2-3
Calling Functions That Expect Other Data Types...........................................2-5
Building a Shared Library (DLL)..................................................................................2-6
Task 1: Build the Function Prototype in LabVIEW........................................2-6
Task 2: Complete the .c File............................................................................2-8
Task 3: Build a Library Project in an External IDE........................................2-10
Calling External APIs....................................................................................................2-14
Common Pitfalls with the Call Library Function............................................2-14
Example 1: Call a Shared Library that You Built ...........................................2-16
Example 2: Call a Hardware Driver API.........................................................2-17
Example 3: Call the Win32 API......................................................................2-19
Additional Examples of LabVIEW Calls to DLLs........................................................2-25
Debugging DLLs and Calls to DLLs.............................................................................2-26
Troubleshooting the Call Library Function.....................................................2-26
Troubleshooting your DLL..............................................................................2-26
Troubleshooting Checklist...............................................................................2-27
Module Definition Files.................................................................................................2-29
Array and String Options...............................................................................................2-30
Arrays of Numeric Data ..................................................................................2-30
String Data.......................................................................................................2-31
Array and String Tip........................................................................................2-33
Contents
Using External Code in LabVIEW vi www.ni.com
Chapter 3
CINs
Supported Languages .................................................................................................... 3-1
Macintosh........................................................................................................ 3-1
Microsoft Windows......................................................................................... 3-1
Solaris, Linux, and HP-UX.............................................................................3-2
Resolving Multithreading Issues................................................................................... 3-2
Making LabVIEW Recognize a CIN as Thread Safe ..................................... 3-2
Using C Code that is Thread Safe...................................................................3-3
Creating a CIN...............................................................................................................3-3
Step 1. Set Up Input and Output Terminals for a CIN....................................3-4
Step 2. Wire the Inputs and Outputs to the CIN .............................................3-6
Step 3. Create a .c File .................................................................................... 3-6
Step 4. Compile the CIN Source Code ........................................................... 3-8
Step 5. Load the CIN Object Code ................................................................. 3-16
LabVIEW Manager Routines........................................................................................ 3-16
Pointers as Parameters ....................................................................................3-17
Debugging External Code .............................................................................................3-18
DbgPrintf......................................................................................................... 3-19
Windows .........................................................................................................3-19
UNIX...............................................................................................................3-21
Chapter 4
Programming Issues for CINs
Passing Parameters........................................................................................................ 4-1
Parameters in the CIN .c File..........................................................................4-1
Passing Fixed-Size Data to CINs.................................................................... 4-2
Return Value for CIN Routines ......................................................................4-3
Examples with Scalars ....................................................................................4-4
Creating a CIN That Multiplies Two Numbers ..............................................4-4
Passing Variably Sized Data to CINs............................................................................ 4-7
Resizing Arrays and Strings............................................................................ 4-9
SetCINArraySize.............................................................................. 4-11
NumericArrayResize ........................................................................ 4-12
Examples with Variably Sized Data ............................................................... 4-14
Manager Overview........................................................................................................ 4-21
Basic Data Types ............................................................................................ 4-23
Memory Manager............................................................................................4-27
File Manager.................................................................................................................. 4-32
Support Manager............................................................................................. 4-36