LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Loading string from DLL using Code Library Function node

Solved!
Go to solution

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 

0 Kudos
Message 1 of 3
(3,144 Views)

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

0 Kudos
Message 2 of 3
(3,124 Views)
Solution
Accepted by topic author cyrax

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

Message Edited by rolfk on 01-11-2010 09:06 AM
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 3 of 3
(3,094 Views)