5.5. Integrate 32-Bit LabWindows/CVI Library into MS VC++

5.5.1. Overview

Software engineers and programmers who are comfortable with ANSI C or Visual C++ development environments may not need to transfer to the LabWindows/CVI development environment if they want to use the LabWindows/CVI tool and library to build test and measurement systems. Programmers can enjoy both the comfortable development environment of ANSI C or VC++ and the advantages of the LabWindows/CVI test and measurement system through integration.

This section describes how to integrate the 32-bit LabWindows/CVI library into a third-party compiler environment, such as Visual C++, and develop a test and measurement project under that environment. In this way, the power of the LabWindows/CVI run-time engine can be utilized and called by the third-party compiler as the program runs.

There are several key points for this integration.

  • First, you should develop a Graphic User Interface (GUI) for your project by using the LabWindows User Interface Editor.

  • The GUI developed by using LabWindows/CVI can be accessed by the events that will be activated by calling the callback functions.

  • Those callback functions can be considered as a bridge to set the connection between the GUI and your program developed in either ANSI C or VC++.

  • Two options can be adopted to built this bridge:

    • Build the callback functions in the third-party compiler, such as VC++.

    • Build the callback functions in LabWindows/CVI and compile them as a Dynamic Link Library (DLL) to be linked to the external compiler code, such as Visual C++ or Visual Basic.

In addition to the key points just mentioned, we can follow a certain sequence to integrate the 32-bit LabWindows/CVI library into MS Visual C++.

  1. Develop your GUI under the LabWindows/CVI environment by using the User Interface Editor.

  2. Develop the callback functions that will respond to the events generated by activating the controls or components on the GUI in the Visual C++ environment.

  3. Depending on the applications, these callback functions can be compiled into a DLL and can be called by the third-party compiler as the program runs.

  4. Create a new project in the third-party compiler environment, such as in Visual C++, build callback functions, and add the GUI with its head file (developed in LabWindows/CVI) into this newly created project (non-DLL).

  5. Compile your project, which contains the GUI and callback functions, to build your target project in the third-party compiler (Visual C++).

  6. If the callback functions are built in a DLL, add the GUI and its DLL library file into the newly created project. Those callback functions can be called later on as your program runs (late-banding).

It is a challenging task to identify and connect the callback functions built in VC++ with the GUI developed in the LabWindows/CVI environment. Traditionally, when we build a project in LabWindows/CVI, three files are created: the User Interface Resource (.uir) File, head file (.h), and source code file (.c). When compiling the project, LabWindows/CVI creates and keeps a table of the nonstatic functions that are in your project. Upon loading the User Interface, such as a panel, using the LoadPanel() function, the User Interface Library uses this table to identify callback functions associated with the controls loaded from the User Interface Resource (.uir) File.

But when you build callback functions in a third-party compiler, such as in VC++, no such table is available when you link the User Interface built in LabWindows/CVI with callback functions built in VC++. Thus, callback functions written in VC++ will not be recognized by the User Interface Resource File (.uir) unless this table is created. LabWindows/CVI can generate an object file containing this table that can be used in the VC++ compiler.

The object file containing this table must be added into your new project in VC++ in order for callback functions to be recognized by the User Interface Resource File (.uir) when you link the LabWindows/CVI library with your new project in VC++. The process of integrating the LabWindows/CVI library into Visual C++ can be shown in Figure 5-68 (non-DLL).


Figure 5-68.


5.5.2. Using VC++ Compiler to Build User Interface Callback Functions

In this section, we illustrate the integration process using the User Interface that we developed in Section 5.3.

5.5.2.1. Design the User Interface

Please refer to Section 5.3 to get a detailed description of the First.prj project and the User Interface, as well as its resource file, First.uir. Open the First.prj project and open its User Interface Resource File First.uir, as shown in Figure 5-69.


Figure 5-69.


5.5.2.2. Using the CodeBuilder and Function Panel to Generate the Source Code

Because we have developed the source code using the CodeBuilder and the Function Panel for this example, we need to just open the source code, First.c, and make it ready to be used. You need to develop the source code if you didn't do it earlier. Combining the CodeBuilder and the Function Panel, you can generate the skeleton of the source code and add callback functions to this skeleton to get the completed source code.

5.5.2.3. Generate the Object Table File

To create the object file that includes a callback functions table for the user interface we generated in Section 5.3, follow these steps:

  • From your project window, click Build and select External Compiler Support to open the External Compiler Support dialog box.

  • Check the UIR Callbacks Object File: checkbox.

  • In the following text input box, enter the path and the name of your desired object file. You can click the Browse . . . button to locate your desired path and type the name for your object file. In our case, type c:cvidemo utorialirst.obj.

  • Click the Create button.

  • A message box will pop up to indicate whether your process is successful.

  • Click the Done button if a successful message is received.

Your completed dialog box for creating an object file should match the one shown in Figure 5-70.


Figure 5-70.


At this point, an object file including a table of callback functions for the user interface in LabWindows/CVI has been generated. You need to add this object file into the new project you will create in the VC++ environment later.

5.5.2.4. Create a New Project in Microsoft Visual C++

Perform the following steps to create a new project in the Visual C++ 6.0 environment:

  • Before starting MS Visual C++, create a new folder under your root drive to save your new project. In our case, create a new folder named LabView under the root drive C:; that is, C:LabView.

  • Start the Microsoft Development Studio, Visual C++ 6.0.

  • Click the File menu, and select the New menu to open a New dialog box.

  • Select Win32 Console Application, and click the button after the Location input box to find the location to which you will save this new project. In our case, it is C:LabView.

  • Enter the project name in the Project name: input box. Enter FirstLabView as the project name here.

  • Click the Finish button and keep the default selection, An empty project, in the following dialog box.

  • Click the OK button to open an empty project.

  • Click File and select New to open the New dialog box. Select C++ Source File and enter FirstLabView as the name of the source code.

  • A new empty source code file is opened. Open your source code file, First.c, which was developed in LabWindows/CVI, and copy this source code into the new empty source file, FirstLabView.cpp.

  • In VC++, click Project and select Add To Project, then click Files to open the Insert File into Project dialog box.

  • In the Files of Type box, select the Library File (.lib). Click the arrow in the look in box until you get the C:cviextlib directory; select cvirte.lib, cvisupp.lib, and cviwmain.lib by pressing and holding the Ctrl key. Then click the OK button. The library files will be added to your new project.

The Insert Files into Project dialog box should look like the one shown in Figure 5-71.


Figure 5-71.


  • Again, click Project and select Add To Project, then click Files to open the Insert Files into Project dialog box. Go to the directory where you saved the First.obj file created in LabWindows/CVI (in our case, this object file was saved in the labwork subdirectory), and select this First.obj file. Click OK to add this object file to your new project in VC++ as shown in Figure 5-72.


    Figure 5-72.


  • In VC++, click Tools and select Options to open the Options dialog box. Using this dialog box, we can locate and connect all head files defined in the LabWindows/CVI library from within the VC++ environment.

  • In the Options dialog box, select the Directories tab and double-click the last row of the Directories: list box. Click the button on the right-end of the last row, and find the path for the head files. In our case, it should be located at C:cviinclude. Select this path and click OK to connect the head file path with the new project. Your completed Options dialog box should look like the one shown in Figure 5-73.


    Figure 5-73.


At this point, you can compile and build your new project in the VC++ environment.

In order to run this project in the VC++ environment, you need to copy the user interface resource file First.uir, developed in the LabWindows/CVI environment, into the directory where the executable files are located in VC++. Microsoft Visual C++, by default, uses Debug or Release directories to store executable files for debug or release builds. So you have to copy the First.uir file to either the Debug or Release directory, depending on which directory is currently used by VC++. In our case, copy First.uir to C:LabViewFirstLabViewDebug. Also make sure that you copy the head file, First.h, into the directory where you store your source files. In our case, you should copy First.h to C:LabViewFirstLabView, where you saved your source code for your new project.

One point we need to emphasize is that you cannot integrate LabWindows/CVI Library into a third-party compiler, such as VC++, using the evaluation software. NI did not put an external library in the free evaluation software for the LabWindows/CVI package. You have to use the formal software package to do this job.


After you have finished building your project, you can run it in the VC++ environment. Click Build and select Execute FirstLabView.exe to run your project. Your user interface will be opened as your program runs; which is very similar to the one that runs in the LabWindows/CVI environment, as shown in Figure 5-74. You can click the Start button to begin your project.


Figure 5-74.


5.5.3. Using a DLL Created in LabWindows/CVI to Define Callback Functions

We still want to use the First.prj as an example to develop a DLL in the LabWindows/CVI environment. But before we go further, go to the project window in LabWindows/CVI and save the First.prj file as Firstdll.prj.

Open the Firstdll.prj project, and open the associated files, such as First.uir, First.h, and First.c. Save them as associated files, such as Firstdll.uir, Firstdll.h, and Firstdll.c. While still in the project window, click Build and select Target, then select Dynamic Link Library as shown in Figure 5-75.


Figure 5-75.


After you finish the last operation, open your source code file, Firstdll.c, and you will find that some new statements have been added to your source code automatically.

  1. Two new functions have been declared at the top of your source code:

    int InitUIForDLL (void);
    void DiscardUIObjectsForDLL (void);
    

    These two functions are used to initialize and delete the DLL you will create. We have to modify these two functions in the following format to match the general calling style for both C and C++ applications.

    int __stdcall InitUIForDLL (void);
    void __stdcall DiscardUIObjectsForDLL (void);
    

    The __stdcall is used to indicate that calling convention for the standard call convention. In C, the calling convention is represented by __cdecl. Using the standard calling convention works for both C and C++.

  2. A new function, DllMain(), has been added to your source code, as shown in Figure 5-76. This DllMain() is the entry point for your DLL and it has the same functionality as the main() function. You don't need to keep this function if you did not have any initialization work to do for your DLL. For our current application, just keep it.

    Figure 5-76.
    								int __stdcall DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
    								{
    								switch (fdwReason)
    								{
    								case DLL_PROCESS_ATTACH:
    								/*Needed if linking in external compiler;harmless otherwise */
    								if (InitCVIRTE (hinstDLL, 0, 0) == 0)
    								return 0;      /*out of memory */
    								break;
    								case DLL_PROCESS_DETACH:
    								/* Do not call CVI functions if cvirte.dll has already been detached. */
    								if (!CVIRTEHasBeenDetached())
    								{
    								/* Discard the panels loaded in InitUIForDLL */
    								DiscardUIObjectsForDLL ();
    								/* Needed if linking in external compiler; harmless otherwise */
    								CloseCVIRTE ();
    								}
    								break;
    								}
    								return 1;
    								}
    							

    We need to make some other modifications to the source code.

    • Replace the main() function with a new function, InitUIForDll(), as shown in Figure 5-77.

      The highlighted codes are deleted codes, and the underscored codes are newly added codes. This function will replace the old main() function to work as a starting point. It will be called at the beginning by the DllMain() as your DLL runs (in other words, the DllMain() is the first function to be called).

    • Add a new function, DiscardUIObjectsForDLL(), as shown in Figure 5-78. This function will be called when your DLL is no longer needed for an application.

    Figure 5-77.
    								int __stdcall InitUIForDll(void)
    								{
    								//call this function from the appropriate place in your code to load & display this panel.
    								i = 0;
    								/* Needed if linking in external compiler; harmless otherwise */
    								// if (InitCVIRTE (0, argv, 0) == 0)
    								//    return -1; /* out of memory */
    								if ((panelHandle = LoadPanelEx (0, "Firstdll.uir", PANEL,  CVIUserHInst)) < 0)
    								return -1;
    								DisplayPanel (panelHandle);
    								RunUserInterface ();
    								return 0;
    								}
    							

    Figure 5-78.
    								void __stdcall DiscardUIObjectsForDLL (void)
    								{
    								if (panelHandle > 0)
    								DiscardPanel (panelHandle);
    								}
    							

    We need to make a few more modifications to your source code before we can close it.

    In the include file section, change #include "First.h" to "Firstdll.h".

    Add one more include file, #include <utility.h>.

    Now, we can finish the source code modification.

  3. Create a new head file export.h that will declare the InitUIForDLL() function we created in step 2, and later we can insert this head file into an external project to make the InitUIForDLL() function available to that project.

    In your project window in LabWindows/CVI, click File and select New, then click Include (*.h) to open a new head file window. Enter the code shown in Figure 5-79 into this new head file window. Save this file as export.h.

    Figure 5-79.
    								#ifdef __cplusplus
    								extern "C" {
    								#endif
    								int __stdcall InitUIForDLL (void);
    								#ifdef _cplusplus
    								}
    								#endif
    							

    In the project window, click Edit and select Add Files To Project, then click Include (*.h) to open the Add Files To Project dialog box. In the file list, select export.h and click the Add button. Click the OK button to add this head file into your current project.

  4. To create your DLL, begin in the project window, and click File and select Save to save your project. Then click Build and select Create Dynamic Link Library. Select OK if you are prompted to set debug to none in order to create a DLL. The Create Dynamic Link Library dialog box will appear, as shown in Figure 5-80.


    Figure 5-80.


    Click the Import Library Choices . . . button. In the resulting dialog box, click Generate Import Library for Current compatibility mode, and click OK. In this way, the created import library will work only for Visual C++ 6.0.

    Click the Change . . . button in the Exports section. In the DLL Export Options dialog box, check the export.h file we created before, and add it into the project. In this way, the InitUIForDLL() function can be exported.

    Now, click OK to create the DLL and import libraries. By default, the name of the DLL and import libraries are identical, except for different extensions. In our case, they are Firstdll.dll and Firstdll.lib, respectively. One of the import libraries has been automatically stored under the msvc subdirectory.

    If you want to use this DLL, first you should perform the following tasks:

    • Insert the head file to the new project in which your new VC++ source code will be saved.

    • Insert the import library into the new project directory as above.

    • Insert the DLL file to the new project directory, or copy it to the Windows System directory.

    • In the new project window in VC++, click Project and select Settings to open the Project Settings dialog box. Click the Link tab, and select the Objects/libraries modules input box. Move the cursor to the end of that box and enter the import library's name in the input box. In our case, enter Firstdll.lib.

    • You have to open your system path window and set a new path identical to your current project directory if you insert the DLL into your new project directory. In this way, the compiler can recognize the DLL when your project runs. You don't need to do that if you copy the DLL directly to the Windows System folder.

    • In your new project window in VC++, specifically in the source code window, add the include files and source code shown in Figure 5-81 in your new source code, appFirstdll.cpp, which is developed in VC++.

    Figure 5-81.
    								#include <Windows.h>
    								#include "export.h "
    								int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    								LPSTR lpszCmdLine, int nCmdShow)
    								{
    								InitUIForDLL();
    								return 0;
    								}
    							

    Now, save the Firstdll.prj project, and close all windows in the LabWindows/CVI environment.

  5. To implement the DLL from a new project, in VC++ create a new project named appFirstdll and perform the following tasks:

    • Insert the import library Firstdll.lib, which is developed in Lab Windows/CVI, into this new project.

    • Insert the export.h head file into your new project directory.

    • Click Tools and select Options; click the Directories tab and move the cursor to the last row in the Directories list box. Click that dashed-line box to find the directory under which the DLL head is located. Select that folder and click OK to add that directory into your new project. In our case, the directory should be C:cviinclude.

    • Add the include codes and source code as shown in Figure 5-60 at the top of your new source code. In our case, add these codes at the top of the appFirstdll.cpp source code file.

    • Also, you need to copy the user interface file Firstdll.uir into the new project executable directory. In our case, copy this user interface file to the folder appFirstdllDebug.

    At this point, you can compile and build your new project, appFirstdll.

When start your project, the panel will be displayed, and when you click a button, the event associated with that button will be created and will be passed to the callback function that is located in the DLL developed using LabWindows/CVI.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.216.0