03-04-2021 02:21 AM
How to call a C++ DLL, intput an empty string and return a non-empty string without causing a memory leak,like this ”read string“
03-04-2021 02:29 AM
If you want to change the contents of the string that you're passing as a C String Pointer, you need to allocate a minimum length in LabVIEW for the output.
This could either be by passing a string containing contents, or more likely by telling LabVIEW in that configuration dialog to use a specific length.
If you output an integer value for the length of the string, you can choose it as a value in that dropdown.
If you instead pass a handle, you have to deal with the string very differently, and link to a LabVIEW-specific library to make calls to LabVIEW-specific DLL functions for allocation/handling.
Pascal String Pointers contain the length in the beginning of the string and so can be automatically parsed, you could consider that too (but you'd have to look up the appropriate memory layout of your string and be careful with that in your C++ code).
03-04-2021 02:34 AM
like this VI,it seem don‘t alloc memory in left but it can return somethings and it will not cause memory leak ,could you tell how to do?
03-04-2021 02:35 AM
i want to pass by handle
03-04-2021 02:56 AM
my C++ code is this ,but it return no correct
03-04-2021 03:05 AM
@yanzhanglin wrote:
i want to pass by handle
You probably want to select String Handle or String Handle Pointer then.
It's your responsibility to make the string large enough.
Use for instance init array to initialize an array of bytes, and convert it to string.
Letting the DLL do it with the minimum size works, but there are quirks when you do this in reentrant VIs (Spoiler: it sometimes returns old data).
03-04-2021 03:08 AM
could you tell how to do like preview VI ?
03-04-2021 03:13 AM - edited 03-04-2021 03:16 AM
The descriptions in this link (https://www.ni.com/pdf/manuals/370109b.pdf) might be useful.
Page 93 (4-10) has an example for string concatenation that might be similar enough to what you need.
Edit: That being said, this forum post (https://forums.ni.com/t5/LabVIEW/Latest-version-of-the-Using-External-Code-in-LabVIEW-Manual/td-p/37...) has some information that can help clarify various changes to the interface and documentation...
03-04-2021 03:18 AM
thanks, but why this pdf is empty?
03-04-2021 03:28 AM
Digging through the commit log on one of my repositories found me a commit containing this:
LStrHandle CreateLabVIEWString(std::string message) {
// Calculate needed memory space
int stringLength = message.length();
const unsigned int memSize = sizeof(int32) + stringLength * sizeof(uChar);
LStrHandle handle = reinterpret_cast<LStrHandle>(LabVIEW_Info::f_DSNewHandle(memSize));
// Empties the buffer
memset(LStrBuf(*handle), '\0', memSize);
// Add data
sprintf(reinterpret_cast<char*>(LStrBuf(*handle)), "%s", message.c_str());
// Inform the LabVIEW string handle about the size of the string
LStrLen(*handle) = stringLength;
return handle;
}
This was in C++ code and used to populate the string in an error cluster, using LabVIEW 2017.
Perhaps it will be of use to you?
For reference, the LabVIEW_Info::f_DSNewHandle is just the DSNewHandle function with, if I recall correctly, dynamic loading.