Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
9 ratings:
 3.44 out of 5     Rate this Document

How Do I Create a DLL That Can Call LabVIEW Manager Functions?

Primary Software: LabVIEW Development Systems>>Full Development System
Primary Software Version: 8.5
Primary Software Fixed Version: N/A
Secondary Software: N/A

Problem:
I want to create a DLL (dynamic link library) rather than a CIN (code interface node) that I can use with my LabVIEW program. Inside of this DLL, I would like to resize data structures passed in from LabVIEW, or perform some other sort of operation requiring one of the LabVIEW manager functions. How can I call these functions from my DLL?

Solution:

Introduction
The process of creating a DLL that can call manager functions is very similar to the process of creating a CIN. However, a few of the steps necessary to create a CIN are not necessary to create a DLL. Notably, the DLL does not need to link against cin.obj or lvsb.lib. Also, the build does not need to include the execution of lvsbutil.exe. However, be aware that functions specific to CINs (such as SetCINArraySize) will not work in a DLL.

Note: In LabVIEW 8.0 or later, refer to LabVIEW Help for more information about using CINs. In LabVIEW 7.1 or earlier, refer to the Using External Code in LabVIEW manual for more information about using CINs.

To ensure that your code knows the prototypes of the manager functions and the LabVIEW data types, be sure to include extcode.h in any file that calls manager functions or makes use of the LabVIEW data type notation (Int32, Bool32, etc):
    #include <extcode.h>
If the compiler does not know to look in the directory in which extcode.h resides, you may need to explicitly specify it:
    #include "c:\program files\national instruments\labview\cintools\extcode.h"

Instructions
In order to build a DLL that can access LabVIEW's manager functions, you must create a multithreaded Win32 DLL and link against labview.lib. In addition, you should set the structure alignment to 1 byte. In the Microsoft Visual C++ IDE, follow these steps:
  1. Create a new DLL project. Select File » New and select Win32 Dynamic-Link Library as the project type. You can name your project whatever you like.

  2. Add CIN objects and libraries to the project. Select Project » Add To Project»Files and select labview.lib from the Cintools\Win32 subdirectory. This file is needed to call the LabVIEW manager functions.

  3. Add Cintools to the include path. Select Project » Properties... and change the Configuration: drop down menu to All Configurations. Select Configuration Properties » C/C++ » General item in the tree. Add the path to your cintools directory in the Additional Include Directories field.

  4. Set alignment to 1 byte. Select Project » Properties... and change the Configuration: drop down menu to All Configurations. Select Configuration Properties » C/C++ » Code Generation item in the tree. Choose 1 Byte from the Struct Member Alignment field.

  5. Choose run-time library. Select Project » Properties... and change the Configuration: drop down menu to All Configurations. Select the Configuration Properties » C/C++ » Code Generation item in the tree. Choose Multithreaded DLL from the Runtime Library field.

Alternate Method
If for some reason you prefer not to link against labview.lib, it is possible to call the LabVIEW manager functions by address. With this method linking against labview.lib is not required, but the remainder of the steps listed above should still be followed. This method requires an understanding of function pointers. If you do not understand the following, use the first method. Also, this method will not work from within a LabVIEW built executable.

Assuming that LabVIEW is calling the DLL you are building, you can call the Win32 API function GetModuleHandle to return a pointer to the module labview.exe. Specifying NULL as the module name returns a handle to the calling module:
    HMODULE hlv = GetModuleHandle(NULL);
Once the module handle is acquired, GetProcAddress can be called to obtain an address to the desired function:
    FARPROC pNumericArrayResize = GetProcAddress(hlv, "NumericArrayResize");
When calling the function, the function pointer will need to be dereferenced and cast as the appropriate type:
    MgErr error = (*((MgErr (__cdecl *)(int32, int32, UHandle*, int32))pNumericArrayResize)) (iL, 1, (UHandle *)&array, 50);
Note that all manager functions use C calling conventions, not standard.


Troubleshooting
  • If you are getting unresolved external linker errors on manager functions, it is likely that you are using CIN only functions. There is usually another manager function that will accomplish the same job that is not a CIN only function.

  • If you getting multiply defined symbols, try instructing the linker not to use the default libraries. In MSVC goto Project » Properties... and change the Configuration: drop down menu to All Configurations. Select the Linker » Input item in the tree and check Ignore All Default Libraries. However, you must at the very least link agains kernel32.lib, user32.lib and msvcrt.lib. Depending on your application, it may be necessary to link against other default libraries.

  • If you are using Microsoft Visual C/C++, and you are having difficulties with precompiled headers, disable the use of precompiled headers. Select Project » Properties... and change the Configuration: drop down menu to All Configurations. Select Configuration Properties » C/C++ » Precomplied Headers item in the tree. Choose Not Using Precompiled Headers from the Create/Use Precompiled Header field.


Related Links:
Product Manuals: LabVIEW Help
Product Manuals: Using External Code in LabVIEW
Developer Zone Tutorial: Building a DLL with Visual C++
Developer Zone Tutorial: External Code Advances in LabVIEW



Attachments:





Report Date: 10/04/1999
Last Updated: 04/01/2008
Document ID: 1Q3FAFNV

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit