01-07-2010 10:24 PM
I am experimenting now with Code Library Function Node element... what i wanted to do is, for example i compiled one .DLL file..which has some function which returns pointer to string... function looks like:
/* This function returns pointer to the character string */
char *pchar(int n)
{
char *str[] = {"error", "String1", "String2", "String3"};
return ((n != 1) && (n != 2) && (n != 3)) ? str[0] : str[n];
}
but when i load it in LabView from DLL, a string element on front panel displays trash... any ideas why?
p.s. when i do same with integer values... say my function is like int x = 5; return x; and then i load it in LabView to the numeric indicator...it works just fine!
so any ideas?
thanx
Solved! Go to Solution.
01-08-2010 11:44 AM
Dear cyrax,
LabVIEW provides support for passing and returning the following string data types with the Call Library Function Node:
C String Pointer - a string followed by a null character
Pascal String Pointer - a string preceded by a length byte
String Handle - a pointer to a pointer to four bytes for
length information, followed by string data
String Handle Pointer - a pointer to an array of string
handles
Are you using the right string format in the CLFN for this specific DLL?
Also check out this resource:
Using a Call Library Function Node to Call a DLL Function that Returns a String that Includes Null Characters
http://digital.ni.com/public.nsf/allkb/FFFF8C757C41CB6486256EB3004C8D76?OpenDocument
and let me know if you are still having problems. Have a good one!
Best Regards,
~Nate
01-11-2010 02:05 AM - edited 01-11-2010 02:06 AM
cyrax wrote:I am experimenting now with Code Library Function Node element... what i wanted to do is, for example i compiled one .DLL file..which has some function which returns pointer to string... function looks like:
/* This function returns pointer to the character string */
char *pchar(int n)
{
char *str[] = {"error", "String1", "String2", "String3"};
return ((n != 1) && (n != 2) && (n != 3)) ? str[0] : str[n];
}
but when i load it in LabView from DLL, a string element on front panel displays trash... any ideas why?
p.s. when i do same with integer values... say my function is like int x = 5; return x; and then i load it in LabView to the numeric indicator...it works just fine!
so any ideas?
thanx
You can't return a pointer to data residing on the stack. At the time the function returns, the stack is reset to the state before the call to the function and in a multithreading environment like LabVIEW most likely long reused for other things when LabVIEW gets around to actually copy the data from the returned pointer.
Since this is a read only variable you could create a permanent memory storage for the strings by declaring your variable as static. This will create a globally allocated memory area for the string data (and also cause a compiler error when you try to write to that area somewhere in your code, which is a good thing).