Calling a DLL Using C++ Strings Shutdowns LabVIEW

Updated Oct 23, 2023

Reported In

Software

  • LabVIEW

Issue Details

I am trying to pass C++ strings to a DLL using a Call Library Function Node , but when I execute my VI, LabVIEW is closing without prompts, warnings or crash logs.

The function that causes the issue uses a prototype similar to:
void Fun1(string &Arg1, string Arg2);

Solution

The "string" data type is a C++ class and is not compatible with being called from LabVIEW. If your DLL is exposing C++ classes in its API, you will not be able to call it directly from LabVIEW. You will need to create a wrapper DLL that uses one of the two following approaches and have the wrapper DLL call the real DLL.:
  • Pass in allocated memory from LabVIEW and fill in that memory in the DLL. Typically this is done by passing both the pointer and a length parameter so the DLL knows how much it can write. This is configured as follows in the Call Library Function Node:
DLLstring.png
 
  • Pass in the LabVIEW string handle and have the DLL use LabVIEW's memory manager functions to resize the allocation and copy the data in. The simpler approach for this is to configure the string to pass by String Handle in the Call Library Function Node parameter configuration, this will guarantee that a handle is allocated. Passing by String Handle Pointer means the DLL has to deal with the handle being null or allocated. Either way once the handle is allocated it should contain a 4-byte length followed by the string content with no null termination.

Additional Information

A DLL handling the "string" datatype using the std namespace, defines that data type to be a std::string class. Trying to directly interface LabVIEW with a DLL function handling that class, can lead to memory exceptions that LabVIEW cannot handle correctly causing it to terminate.