Example Programs

cancel
Showing results for 
Search instead for 
Did you mean: 

Using DLLs Built With Visual C++ In LabWindows/CVI

Overview

The example will guide you through using DLLs built using Visual C++ in the C compiler of LabWindows/CVI

 

Description

While calling C based DLLs in LabWindows/CVI is a breeze, calling DLLs built in other languages such as Visual C++ can be somewhat more involved. This example will demonstrate how to properly set up a project in Microsoft Visual Studio to build a visual C++ DLL. Then, we will call functions from that DLL in LabWindows/CVI.

 

Requirements

  • Microsoft Visual Studio 2010 (or compatible)
  • LabWindows/CVI 2013 (or compatible)

 

Steps to Implement or Execute Code

  1. Open The DynamicLibrary.sln solution from the Visual Studio VCPP project folder
  2. Analyze solution and project. Note the MathFuncsDll.h file
  3. Notice that the function declarations in the .h file also include the following:
      extern "C" __declspec(dllexport) double __cdecl DGAdd(double a, double b);
      extern "C" __declspec(dllexport) double __cdecl Subtract(double a, double b);
      extern "C" __declspec(dllexport) double __cdecl Multiply(double a, double b);
    The "extern "C" __declspec(dllexport)" and "__cdecl" declarations are necessary to be able to use this DLL in code that will be compiled with a C based compiler.
  4. If no changes are made skip to step 5. If you do make changes, mode the modified .h, .lib, .pdb, and .dll files over to the CVI project folder of interest.
  5. Open the "dll test.cws" workspace from the CVI project folder.
  6. Open the Command-line Application.c file and analyze
  7. Notice that the only files included in the project are the .h and .lib files. Note: DO NOT INCLUDE .DLL FILE
  8. Run program and notice use of functions from Visual C++ dll.

 

Additional Information or References

Refer to Section 9 of FAQ: Using Dynamic Link Libraries with NI LabWindows/CVI

Calling a Visual Basic DLL From LabWindows/CVI

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

Daniel G.
Semiconductor & Wireless
National Instruments
Comments
Member Bill2@NGC
Member

Creating the DLL is not discussed.  Is it assumed that the C++ DLL has already been built and we then have to add the extern "C"?  Or do add that before we create the DLL?  Thanks.

AleAlejandro
NI Employee

Hi!

You would do that before you create the DLL as noted in step 2 and three. You have to add the modifiers to the .h file and then build the project.

In the calling program, include the .h and .lib file and we will be able to call the functions. The attached .zip file contains the example code on how to accomplish this on both the creating the .dll side and calling it. I hope this helps!

Regards,

Ale

Daniel G.
Semiconductor & Wireless
National Instruments
Member Bill2@NGC
Member

Hi Ale,

Thanks for the quick response and I understand it now. But nowhere does it say build DLL now. It’s kind of implied in Step 3, but not stated outright. Perhaps it’s obvious to a programmer experienced in this, but I’ve never built a DLL before, so I need it spelled out. Thanks again for your help.

Bill

Member eranv
Member

Hi,

i am trying to use C function that wrapped C# dll and call it via CVI, without success. can you advice please?

Member AlexafelderDeni
Member

Hi,

thank you very much indeed for very useful example code

Member Gareth@
Member

Hello,

I'm getting errors when attempting step 8. Below is the build output:

 

Command-line Application.c - 6 errors, 4 warnings
 "MathFuncsDll.h"(2,12)   Warning: Empty declaration.
 "MathFuncsDll.h"(2,12)   syntax error; found 'string constant' expecting ';'.
 "MathFuncsDll.h"(2,12)   Unrecognized declaration.
 "MathFuncsDll.h"(3,12)   Warning: Empty declaration.
 "MathFuncsDll.h"(3,12)   syntax error; found 'string constant' expecting ';'.
 "MathFuncsDll.h"(3,12)   Unrecognized declaration.
 "MathFuncsDll.h"(4,12)   Warning: Empty declaration.
 "MathFuncsDll.h"(4,12)   syntax error; found 'string constant' expecting ';'.
 "MathFuncsDll.h"(4,12)   Unrecognized declaration.
 55, 1   Warning: HIPAR tag - Parameter help for name declared but parameter not defined in function.

I haven't modified the CVI project or the C++ code. I am running CVI 9.0