How to Integrate 32-Bit Microsoft Visual C/C++ or Borland C/C++ DLLs into LabWindows/CVI 4.0
Overview
It is often necessary to include functions from a DLL into a LabWindows/CVI project. These DLLs are typically created in third-party compilers, such as the Microsoft Visual C++ and Borland C++ compilers. This document describes a simple example of how to create a DLL in these third-party compilers and use it in a LabWindows/CVI project.
Table of Contents
Creating New Projects and Inserting Source Code to a Project
The following examples require knowledge of creating new projects and adding source code to projects in the Microsoft Visual C++ and Borland C++ development environments. If you are unfamiliar with this process, go through the following steps for your specific compiler.Creating a New Project in Microsoft Visual C++
1. Start the Microsoft Developer Studio.
2. Choose New from the File Menu.
3. Click on Project Workspace and click the OK button. You should then see the following dialog box.

4. Click on Dynamic-Link Library in the Type: box. Type in the path in the Location: control, and then type in the name of your project in the Name: control. You should then click the Create button. This will be the project where you can do the following exercise.
5. To create and add the source code file into the project, click New under the File menu item. Choose Text File. Type in the source code in the new text file. Save the file by clicking Save under the File menu item. You then must add the file to the project by selecting Files Into Project from the Insert menu.
6. When the Insert Files into Project dialog box appears, you must select the source file, and click the Add button. At this point the source file is part of the project and should be seen in the project workspace.
Creating a New Project in Borland C++
1. Start the Borland C++ development environment.
2. Choose New: Project under the File menu item. You will see the following dialog box.

3. Uncheck the OWL option under Frameworks. Choose Dynamic Library [.dll] in the Target Type: control.
4. Select Libraries: Static Linking.
5. You should then type in the project path and name along with the target name in the appropriate controls.
6. Then click the Advanced button and you will see the following dialog box.

7. Uncheck the .rc and .def options and click OK. Click OK in the New Target dialog box. At this point you will have a project where you can do the following exercise.
8. You should be able to double click on the .cpp file in the project and type in the source code in the new text file. Save the file by clicking Save under the File menu item.
9. To create a new source code file and add it to a project, choose the New: Text Edit under the File menu item. Add your code to the source window. Save the file. Right click on the .exe file in the project window and choose Add Node. You will see the following dialog box.

10. You must select the source file, and click the Open button. At this point the source file is part of the project and should be seen in the project window.
Creating DLLs
Creating DLLs in the Microsoft Visual C++ and Borland C++ environments is a relatively simple process.
1. First of all, we create a new project in the development environment of your choice named Array_Add.
2. Then open a new source file called Array_Add.cpp and type in the following code.
#include "Array_Add.h"
__declspec(dllexport) int __stdcall Array_Add(int *array, int num)
{
int sum = 0;
for(int i=0; i<num; i++)
sum += array[i];
return sum;
}
__declspec(dllexport) is needed to tell the Borland C++ and Microsoft Visual C++ compilers to export this function in a import library.
the __stdcall specifies that the function will use the stdcall function calling convention so that when it is included in LabWindows/CVI, LabWindows/CVI will know how to make the function call.
3. Next create another source file named Array_Add.h and add the following source code to that file.
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) int __stdcall Array_Add(int *array, int num);
#ifdef __cplusplus
}
#endif
The #ifdef statements are needed for external compilers that compile .cpp file as C++ code. This forces the external compiler to compile the function prototypes as C code instead of C++ code.
4. At this point you should be able to build the project into a DLL and an import library for use in LabWindows/CVI.
Using DLLs Created in Third-Party Compilers in LabWindows/CVI
To use the DLL created above in LabWindows/CVI do the following.
1. Open a new project in LabWindows/CVI.
2. Include the .lib file that was created in the third-party compiler into the LabWindows/CVI project, by clicking Add Files to Project: Library (*.lib) from the Edit menu item in the project window. You will see the following dialog box.

3. Select the Array_Add.lib file and click the Add button. Next click the OK button. You should now she the library file in the project window.
4. Next select New> Source (*.c) from the File menu item in the project window.
5. Type in the following source code in the source code editor.
#include <ansi_c.h>
#include "Array_Add.h"
void main()
{
int array[5] = {1,2,3,4,5};
int sum = Array_Add(array, 5);
printf("%d\n", sum);
}
6. Make sure the .h and .dll file are in the same directory as the LabWindows/CVI project. At this point you should be able to compile and run the program. The output will be “15” in the LabWindows/CVI stdio window.
Using C++ Code in a DLL
We will now go a step further after having successfully created and used a DLL in CVI. This next section will demonstrate how to use C++ code in a simple DLL.
Before we get started with the example, one point should to be emphasized. LabWindows/CVI is not a C++ environment. This places some restrictions on your use of C++ code. Your entry into a C++ DLL must be ANSI C; therefore, C++ class objects cannot be passed into or out of a DLL. Once inside the DLL, there are no C++ restrictions.
Creating a C++ DLL
1. Create a new project in Borland C++ or Microsoft C++ with the name cpptest.
2. Create a new source file.
3. Type in the following code:
#include <string.h>
#include <iostream.h>
#include <windows.h>
#include "cpptest.h"
const int max_len = 255;
class string
{
char s[max_len];
int len;
public:
void assign(const char* st) { strcpy(s, st); len = strlen(st); }
int length() { return(len); }
};
int FAR PASCAL LibMain (HINSTANCE hInstance, WORD wDataSeg, WORD cbHeapSize,
LPSTR lpCmdLine)
{
return 1; /* return non-zero to indicate success */
}
__declspec(dllexport) void __stdcall getstrlen(char *buff, int *length)
{
string temp;
temp.assign(buff);
*length = temp.length();
}
4. Save the source file as cpptest.cpp.
5. We will now create the header file. Type the following in a new source window and save it as cpptest.h.
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) void __stdcall getstrlen(char *buff, int *length);
#ifdef __cplusplus
}
#endif
6. After doing this, build the DLL.
Using the C++ DLL in CVI
1. Create a new project in CVI and add cpptest.lib to the project list. Create a new source file and type the following:
#include <formatio.h>
#include "cpptest.h"
main()
{
char tester[100]= "This is LabWindows/CVI";
int len=0;
getstrlen(tester, &len);
FmtOut("%s\nLength: %d[b2]\n", tester, len);
}
2. Save this file as usecpp.c and add it to the project list. Run the project and you should see: This is LabWindows/CVI 22
That’s it! You have successfully called C++ code from LabWindows/CVI. Again, you should remember that the function prototype must contain only ANSI C, meaning that C++ class objects cannot be passed into or out of the DLL function. All C++ code must be contained within the DLL function.
Reader Comments | Submit a comment »
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/).
