LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

about call C++ dll

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“

EBY@W9LTZN$BVZL$SO$FSQL.png

0 Kudos
Message 1 of 14
(1,531 Views)

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.

cbutcher_0-1614846406425.png

 

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).


GCentral
0 Kudos
Message 2 of 14
(1,521 Views)

70I)(R6C`@X(4]LF~E(~HUL.png

 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?

0 Kudos
Message 3 of 14
(1,513 Views)

i want to pass by handle

0 Kudos
Message 4 of 14
(1,512 Views)

Z(0T3460@TWC5]0(NAN7UPY.png

 my C++ code is this ,but it return no correct

0 Kudos
Message 5 of 14
(1,470 Views)

@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).

0 Kudos
Message 6 of 14
(1,456 Views)

could you tell how to do like preview VI ?

0 Kudos
Message 7 of 14
(1,451 Views)

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...


GCentral
0 Kudos
Message 8 of 14
(1,446 Views)

thanks, but  why this pdf is empty?

0 Kudos
Message 9 of 14
(1,439 Views)

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.


GCentral
0 Kudos
Message 10 of 14
(1,425 Views)