LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

interface .dll - can I involve some C++ code

got told that arrays and strings are objects and they do not get deleted at the end of the program. So I did this

 

Steffen01_1-1734642868467.png

 

what happened, the vi crashed straight away. Without the 2 delete lines I can run the vi as long as I want, the error comes when I close the vi. Maybe somehow then it disconnects from the dll and data is gone before the vi ended gracefully.

Or maybe, because I compiled for debugging? That as a side note, I try c_str now.

 

For the future reader, just override the dll in the library

Steffen01_2-1734643104700.png

and run vi again

0 Kudos
Message 71 of 73
(80 Views)

ok, still not happy

Steffen01_0-1734651722280.png

need to initialise to 129 in Labview and set minimum string length to 129, otherwise it crashes. But the error persists on closing. Error string is truncated to 128 characters for sure, so in principle it works

 

0 Kudos
Message 72 of 73
(72 Views)

@Steffen01 wrote:

ok, still not happy

Steffen01_0-1734651722280.png

need to initialise to 129 in Labview and set minimum string length to 129, otherwise it crashes. But the error persists on closing. Error string is truncated to 128 characters for sure, so in principle it works

 


Yes, for sure you will get an assertion here:

Andrey_Dmitriev_0-1734681002251.png

Please read documentation thoroughly and feel difference:

 

#include <iostream>

using namespace std;

int main()
{
    string errorMSG(512, '-');
    char toLVerror_string[129] = { 0 };
    
    // This code below is OK
    // refer to https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strncpy-s-strncpy-s-l-wcsncpy-s-wcsncpy-s-l-mbsncpy-s-mbsncpy-s-l?view=msvc-170
    strncpy_s(toLVerror_string, 129, errorMSG.c_str(), 128);
    printf("Destination OK: %s", toLVerror_string);
    // also check example https://en.cppreference.com/w/c/string/byte/strncpy
    /* 
    * The strncpy_s function try to copy the first D characters of strSource to strDest, 
    * where D is the lesser of count and the length of strSource.
    * If those D characters will fit within strDest (whose size is given as numberOfElements)
    * and still leave room for a null terminator, then those characters are copied and a terminating null is appended;
    */

    // But this code is not OK
    // refer to https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-170
    strcpy_s(toLVerror_string, 128, errorMSG.c_str());
    printf("Destination too small: %s", toLVerror_string);
    // and example https://en.cppreference.com/w/c/string/byte/strcpy
    /*
    * The strcpy_s function copies the contents in the address of src, 
    * including the terminating null character, to the location that's specified by dest.
    * !!! The destination string must be large enough to hold the source string and its terminating null character
    */
}

 

0 Kudos
Message 73 of 73
(55 Views)