LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

calling a labview dll with CVI/Problems with LStrHandle

Hello,

I'm trying to use a labview DLL (which I can't modify since I've no access to the source) in CVI. The function I use needs 1 LStrHandle as input and 2 LStrHandle as output. But I'm getting the following error message when I'm executing the function :

"error #3: "memory.cpp", line 563, labview 6.1"

I've tried to allocate memory to the handle but I'm not sure at all what I'm doing (when not doing this, I receive a general protection error) :

hraw = (LStr**)DSNewHandle(sizeof(LStr));
hout = (LStr**)DSNewHandle(sizeof(LStr));
houtp = (LStr**)DSNewHandle(sizeof(LStr));

Thanks for helping,

Arnaud

0 Kudos
Message 1 of 10
(4,887 Views)
Hi Arnaud,

Because you are using a LabVIEW string handle, you need to use the LabVIEW memory manager functions to allocate memory for the string handle. For example, here is a code snippet to get you started on allocating memory for that LabVIEW string handle.

LStrHandle lvStringHandle;  

lvStringHandle = (LStrHandle)DSNewHandle(sizeof(int32) + lengthOfString * sizeof(uChar));

// Empty the buffer
memset(LStrBuf(*lvStringHandle), '\0', lengthOfString);

// Fill the string buffer with stringData
sprintf((char*)LStrBuf(*lvStringHandle),"%s",stringData);

// Inform the LabVIEW string handle about the size of the stringData
LStrLen(*lvStringHandle) = strlen(stringData);


The memory has now been allocated so you can safely pass it to LabVIEW. 

Hope this helps!

Best Regards,


Message Edited by Jonathan N on 03-19-2008 08:05 AM
Jonathan N.
National Instruments
Message 2 of 10
(4,876 Views)

Hi Jonathan,

Thank you for your reply.

I've just tried what you told me but I'm still getting the same error. Anyway, the dll gives back the result in two string handles. Should I initialize them the same way (the length is not fixed) ? I've tried to initialize them with a length of 10000 bytes which should be more than enough but still the memory.cpp error.

Thanks again for your time.

Best Regards,

Arnaud

 



Message Edited by tyller on 03-19-2008 08:41 AM
0 Kudos
Message 3 of 10
(4,868 Views)
Hi Arnaud,

Using that same code I didn't have a problem returning a LStrHandle from a LabVIEW DLL call.  Now, I am using LabVIEW 8.5 but I don't think that should matter in this case. The error you are getting simply means you are accessing memory that hasn't been allocated by the LabVIEW runtime.  I would suggest creating a very basic LabVIEW DLL that passes out a LStrHandle and see if you can just call that without issues.  This post and this post also talk about this topic. 

Also, please look at the link I posted earlier which is for the LabVIEW memory manager help. That should give you an in depth reference to how you should allocate memory for use in LabVIEW.

If you can reproduce the issue with a simple example, you can post it here or either create a support request with our Application Engineers and send it to them. 

Hope this helps

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 4 of 10
(4,853 Views)
I don't have the function DSNewHandle available on my machine. Do I have to install any special software package?
0 Kudos
Message 5 of 10
(4,667 Views)
How do I convert an LStrHandle to a char array? 
0 Kudos
Message 6 of 10
(4,664 Views)
Hi Steve,

DSNewHandle is a function associated with the LabVIEW memory manager. To access the LabVIEW memory functions, you need to have included the extcode.h header file along with the labview.lib import library. Both of these files are found in the <LabVIEW>/cintools/ directory. 

Check out this post for converting an LStrHandle to a character string.

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 7 of 10
(4,608 Views)
The link that you sent me is a CPP link. I need to know how to convert the labview string to a c string in CVI.
0 Kudos
Message 8 of 10
(4,599 Views)
Hi Steve,

In the link I sent you, the first response by rolfk shows you how to do this in C. Then in later posts, he shows you how to do it in C++.  In essence, all you have to do is something like:

char errorString[50] = "This is a bad error";
char *myString = NULL;
LStrHandle lvHandle;  
int length;
   
length = strlen(errorString) + 1;
lvHandle=(LStrHandle)DSNewHandle(sizeof(int32) + length * sizeof(uChar));
   
// Empty the buffer
memset(LStrBuf(*lvHandle),'\0',100);

// Fills the string buffer with errorString
sprintf((char*)LStrBuf(*lvHandle),"%s",errorString);
   
// Inform LabVIEW string handle about the size of errorString
LStrLen(*lvHandle)= length;
   
myString = malloc(LStrLen(*lvHandle) + 1);
memcpy(myString,(char*) LStrBuf(*lvHandle), LStrLen(*lvHandle));
myString[LStrLen(*lvHandle)] = 0;


Best Regards,


Message Edited by Jonathan N on 05-30-2008 08:40 AM
Jonathan N.
National Instruments
Message 9 of 10
(4,596 Views)

Thank you for that one..

 

@National Intstruments

Something like that should make it into the offical doc.

0 Kudos
Message 10 of 10
(2,037 Views)