Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Creating a CIN Project in Visual Studio .NET 2003

6 ratings | 3.50 out of 5
Print

Overview

This document describes how to configure Microsoft Visual Studio .NET 2003 to create a Code Interface Node (CIN) project to write CIN code for use in LabVIEW 7.1.

Note: This document assumes that you have Microsoft Visual Studio .NET 2003 and LabVIEW 7.1 installed on a Windows XP machine.

Creating an Environment Variable

For more information about CINs in LabVIEW 7.1 or earlier, refer to the Using External Code in LabVIEW manual (linked below). In LabVIEW 8.0 or later, refer to the Fundamentals»Calling Code Written in Text-Based Programming Languages book in the LabVIEW Help (linked below) for more information.

You can maintain CIN projects by creating an environment variable that points to the cintools directory of LabVIEW. Complete the following steps to create an environment variable.

  1. In the System control panel accessory, click the Advanced tab.
  2. Click the Environment Variables button.
  3. In the User variables section, click the New button to create a new user-level environment variable.
  4. In the Variable name text box, type CINTOOLS_DIR.
  5. In the Variable value text box, type the absolute path to the cintools directory of LabVIEW.


By creating an environment variable, you can move a project from one machine to another in which LabVIEW is installed in a different location. You do not have to change the Visual Studio project settings; instead you only change the value of the environment variable.

Creating an MFC DLL


Complete the following steps to create an MFC DLL.
  1. Launch Visual Studio .NET.
  2. Click the New Project button to display the New Project dialog box.
  3. Double-click the MFC DLL icon in the Templates section to display the MFC DLL wizard.


    [+] Enlarge Image
  4. Click the Application Settings link.
  5. In the DLL type section, click the MFC extension DLL button.


    [+] Enlarge Image
  6. Click the Finish button to create the project.

    Creating a CPP File

Complete the following steps to remove all files except the SampleCIN.cpp file.
  1. Click the Solution Explorer tab to display the Solution Explorer tree.
  2. Delete all items highighted by the red box below. You can delete corresponding files safely from disk after you delete them from the project and save the project.

    Using LabVIEW to Create Starting Code for the CPP File

Complete the following steps to use LabVIEW to create the starting code for the SampleCIN.cpp file.
  1. Launch LabVIEW.
  2. Click the Blank VI link to create a new, blank VI.
  3. Place a Code Interface Node on the block diagram.
  4. Grow the Code Interface Node so it contains three terminals.
  5. Wire two numeric controls and one numeric indicator to the Code Interface Node, as shown below.


  6. Right-click the last terminal and select Output Only from the shortcut menu.
  7. Save the VI.
  8. Right click the Code Interface Node and select Create .c File from the shortcut menu. LabVIEW creates a .c file that you can use as a starting point for writing code for the Code Interface Node that you configured.
  9. Replace the content of the SampleCIN.cpp file with the content of the .c file that the Code Interface Node generated.
  10. Replace #include "extcode.h" with #include <extcode.h>.
  11. Enclose the function declaration with extern "C" so you can compile the C code in a .cpp file. The .cpp file should be similar to the code below.

    /* CIN Source file*/

    #include <extcode.h>

    extern "C"{
    MgErr CINRun(int32 *Num1, int32 *Num2, int32 *Sum);
    }

    MgErr CINRun(int32 *Num1, int32 *Num2, int32 *Sum){
    *Sum = *Num1 + *Num2;
    return noErr;
    }
  12. Save and close the VI.

    Configuring Visual Studio Project Properties

Complete the following steps to configure the project properties in Visual Studio.
  1. In Visual Studio, select Project»Properties to display the Property Pages dialog box.
  2. On the General page, verify that Use of MFC is set to Use Standard Windows Libraries.


    [+] Enlarge Image
  3. Change the configuration items as shown in the screenshots below. The entries that change are shown in bold text, except as noted.


    [+] Enlarge Image


    [+] Enlarge Image
  4. On the C/C++»Precompiled Header page, delete the content of the Create/Use PCH Through File and Create/Use Precompiled Header File fields.


    [+] Enlarge Image
  5. Continue changing the configuration items as shown in the screenshots below.


    [+] Enlarge Image


    [+] Enlarge Image
  6. On the Linker»Input page, type cin.obj labview.lib lvsb.lib in the Additional Dependencies field.


    [+] Enlarge Image
  7. On the Custom Build Step»General page, type  "$(CINTOOLS_DIR)\lvsbutil" "$(TargetName)" -d "$(ProjectDir)$(OutDir)" in the Command Line field and type $(OutDir)$(TargetName).lsb in the Outputs field.


    [+] Enlarge Image

    Configuring Visual Studio Project Properties for Release

Complete the following steps to configure the project properties in Visual Studio for the release build.
  1. In the Property Pages dialog box, select Release from the Configuration pull-down menu.
  2. Repeat the steps in the Configuring Visual Studio Project Properties section.


    [+] Enlarge Image
  3. Click the OK button.

    Loading an LSB File

Complete the following steps to load an.lsb file in LabVIEW.
  1. In Visual Studio, select Build»Rebuild Solution to create the .lsb file.
  2. In LabVIEW, open the VI you created in the Using LabVIEW to Create Starting Code section.
  3. Right-click on the Code Interface Node and select Load Code Resource from the shortcut menu.
  4. Select the .lsb file you created.
  5. Run the VI.

Related Links:
Manuals: Using External Code in LabVIEW

Calling Code Written in Text-Based Programming Languages

6 ratings | 3.50 out of 5
Print

Reader Comments | Submit a comment »

Worked in Visual C++ Express Edition 2008 (Part 2)
Continue other comment: 3. Under Linker->General "Output File", the setting "$(OutDir)\$(ProjectName).dll" as is default seems more appropriate than the proposed change made in this document. One last thing: as many settings need to be changed in order to get the code compile and usable for a CIN, it would be nice to include a few steps on how to export these settings and import them into future projects. I'm still trying to figure that one out, as I will be using this method quite a bit on a current project that I am working on.
- Nic Linley, Wendell Hull & Associates. nic@wendellhull.com - Sep 23, 2008

Worked in Visual C++ Express Edition 2008 (Part 1)
Hi! I was able to get this example to work using Visual C++ 2008 Express Edition. Very much appreciated! Visual C++ Express Edition may be a good utility to some LabVIEW developers who need to work with simple external code written in C but do not want to pay for the licensing of the full Visual Studio Suite, such as myself. I'm a novice C programmer, and actually know very little about C++. Here are some notes that may help other readers: 1. The Express Edition does not allow the use of "MFC." I am not 100% sure what MFC would provide to a LabVIEW developer. From "New Project" I was able to select "Win32 Console Application" and in subsequent windows of the Wizard select "DLL" and then "Empty Project". That seemed to work just fine for me. 2. The settings specified for "Custom Build Step" in step 7 don't work for VC++ Express Edition 2008. The "Command Line" setting that worked for me was: "$(CINTOOLS_DIR)\lvsbutil" "$(TargetName)" -d "$(OutDir)" -- this was obvious upon inspecting the BuildLog.htm that was produced from the first error. The variable $(OutDir) contains the path $(TargetDir). Therefore, $(TargetDir) is not redundant and causes a path error for the lvsbutil command line utility.
- Nic Linley, Wendell Hull & Associates. nic@wendellhull.com - Aug 18, 2008

CIN Projects in Visual Studio .net
One addition to my previous comments: It might be helpful to include the following information in the document: - When using the C++ compiler, it's necessary to export CINRun as an undecorated symbol, by prefixing the declaration for CINRun with: extern "C" CINRun... Also, the CIN manual advises you to add cin.obj, labview.lib, lvsb.lib, and lvsbmain.def from the Cintools. The on-line document specifies the same modules, but includes them as additional modules. You can do one of the other, but not both, or you'll see duplicate definitions (the linker is not particularly smart). Hope this helps someone else...
- John Heerema, Andromeda Computer Systems Ltd.. heerema@andromeda.ab.ca - Dec 4, 2005

CIN projects in Visual Studio .net
The document is very clear and explicit. Unfortunately, it failed to address the problem I was having, and which caused me to search for this document: Even after following the instructions, _CINRun is still showing as an unresolved reference. In fact, after modifying my project to match this example, I had additional problems (duplicate definitions for _LVSBHead and _gLVExtCodeDispatchTable). Also, the example failed to address questions an experienced C++ developer would naturally have, such as: - why are precompiled headers not supported? - why pick an MFC dll as your starting point? - would MFC windows work? - why no 64 bit portability?
- John Heerema, Andromeda Computer Systems Ltd.. heerema@andromeda.ab.ca - Dec 4, 2005

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).