LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Return C++ String From Dll

All,
  I have had some limited success trying to find an answer to this question.  I am writing a wrapper DLL to handle calls to and from the Berkeley Database XML library.  Long story short, I need to return an array of strings from the DLL call.  I cannot preallocate a large buffer to place the strings in prior to the call, as I could be returning fairly large strings, as well as small ones and I dont want to use a consistently large buffer.  In the C++ function I have the array of strings, how can I simply return that.  If I had to I could loop the call and return the strings one at a time, but I would really like to return the string in the DLL without having to copy to a buffer created by labview prior to the call.  Do I have to resort to using the labview external code base to create and return strings without copying into a preallocated buffer?

Thanks,
Paul
0 Kudos
Message 1 of 4
(3,937 Views)
Normally, you need a mechanism to get the size of the data. See for instance GetClipboardFormatName.


So, let's say you have a function "ArrayOfStrings". You could make the prototype int ArrayOfStrings (LPTSTR lpszBuffer, int SizeOfBuffer).


If you call this function like this:


NeededBufferSize = ArrayOfStrings (NULL, 0)


ArrayOfStrings knows how big the buffer should be. If SizeOfBuffer is too small, the needed size is returned in NeededBufferSize.


Of course, you can make ArrayOfStrings so it accepts a pointer to the buffer size, so the needed value is returned in SizeOfBuffer, and the return value is -1, or another error value.


Now for the data...


Normally Arrays of strings are passes as NULL terminated strings, and the array is terminated with NULL NULL. In LabVIEW, I'd make my input buffer an array of U8's, with the right size. When it returns, you can get strings from it by searching for 0, until you find two 0's.


Returning one string at the time doesn't really solve the problem, since you don't know how long each string is going to be.


If your function takes a long time, you need to cache the results.


Regards,


Wiebe.
0 Kudos
Message 2 of 4
(3,927 Views)
I was really looking for a solution that doesn't involve creating a first call method that would return the size of the buffer needed to be created.  I want the DLL to simply return the string without a two part call to first tell labview the buffer size needed.  I think I need to use the labview memory manager for that, if so could someone please point me to a simply example of doing that.

-Paul
0 Kudos
Message 3 of 4
(3,924 Views)
I've never allocated memoery inside a dll with LabVIEW functions. It's possible, but I'd use the Windows Heap (HeapCreate, HeapAlloc, etc.) functions. But that is only because I'm familiar with them.


Regards,


Wiebe.
0 Kudos
Message 4 of 4
(3,902 Views)