From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling a char * value from a dll

Hi!
I am using a dll to run a program from labview. The dll file has a function error_msg(int16 code, char *dst). I am using the Call Library Function node to call up the command. However, am not sure what type and data type I should use for the char *dst since there is no option in the configure menu.
Thank you for your time and help.
 
-Amrit
0 Kudos
Message 1 of 19
(4,377 Views)
A "char" in C is simply a U8 in Labview (a byte).  To pass *char, set the parameter Type to "Numeric", Data Type to "Unsigned 8-bit Integer", and Pass to "Pointer to Value".
- tbob

Inventor of the WORM Global
Message 2 of 19
(4,368 Views)


@alrite wrote:
Hi!
I am using a dll to run a program from labview. The dll file has a function error_msg(int16 code, char *dst). I am using the Call Library Function node to call up the command. However, am not sure what type and data type I should use for the char *dst since there is no option in the configure menu.
Thank you for your time and help.
 
-Amrit



Hi Amrit,

      You'll need to obtain the instructions for the DLL to know how to interpret "char *dst" - it might mean a pointer to a single char, but more frequently this specifies a "string" parameter - or even an array of bytes! Smiley Surprised If this parameter is really a pointer to a single char, then tbob's instructions are perfect!

Cheers.

 

"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
Message 3 of 19
(4,361 Views)
tbd is right.  I didn't think about a string of chars as the C language likes to use a pointer to the first char and then looks for the NULL to end the string.  So if you are passing an entire string, this won't work.  If the length of the string is constant, the Labview string can be converted to a byte array (String to Byte Array), and passed to the DLL as an array of U8, passed by pointer.  If the length is not constant, perhaps you could pass an array the size of the longest possible string, with the end of the string and the rest of the elements being \00 (NULL).  C will see the NULL as the end of the string.  I'm not sure it will work like this, but it is worth a try.
- tbob

Inventor of the WORM Global
Message 4 of 19
(4,356 Views)

Watch out! From the context of the function name I assume this parameter is a string buffer the function will use to write the message into. And that means you will have to create a LabVIEW string that is long enough to contain the longest possible string the function might return. Configure the parameter in the CLN to be a string, passed as a C string pointer.

It is very important that you do not pass in a to short string or even an empty string. The function has no way of resizing the string as a LabVIEW node could, so it just assumes the buffer is big enough to write in whatever it needs to write. If your buffer is to small it will overwrite more or less important data in LabVIEW causing symptoms such as immediate crashes (exeception dialog), random crashes at a later stage, corrupted VIs when saving them to disk, or even only a crash dialog when exiting LabVIEW. Whatever you get it is a bad sign and could change at any time into more serious problems and if any of these symptoms arise you know almost for sure that a DLL function gets called with a to small buffer somewhere.

Rolf Kalbermatter

 

Message Edited by rolfk on 01-23-2007 11:18 AM

Rolf Kalbermatter
My Blog
Message 5 of 19
(4,340 Views)

Thank you for your assistance! It has been extremely useful. So I have a question, how do you set the string length that you want to pass in, I want my string length to be 150, but do you just type '150' into the string constant box, or is there something else that must be done?

Thanks again for your time and help!

-Amrit

0 Kudos
Message 6 of 19
(4,315 Views)

You can create a U8 array with 150 elements, set all elements = 0 (NULL).  Use Replace Array Subset to put other characters (ASCII code of each char) into the array.  The array should always be 150 elements in length.  Pass this U8 array into the Call Library node.  Be sure to set it to pass by pointer.

Or make a string of 150 zero's.  Substitute characters where needed, but leave the length 150 and leave zeros after the meaningful characters.  Pass this to the DLL as a C string.

- tbob

Inventor of the WORM Global
Message 7 of 19
(4,313 Views)

Hey tbob, who 4-starred you?

To construct a fixed-length (empty) character-buffer, do what tbob said!

(... though this works too Smiley Wink - )

Message Edited by tbd on 01-23-2007 09:32 PM

"Inside every large program is a small program struggling to get out." (attributed to Tony Hoare)
0 Kudos
Message 8 of 19
(4,299 Views)
tbd:
 
Of course I was thinking about initializing array when I posted my solution.  I should have mentioned it.  I use it all the time.  Can you imagine creating a string and typing in a zero 150 times.  Yeow.  Or making an array and stretching it to 150 elements.  Didn't think about the fact that a novice might actually do this.  I'm glad you posted your diagram.
 
As for the stars, it doesn't bother me.  People are entitled to their opinions.  In fact, I didn't think about a C string, just a character, and since I know C, I should have thought differently.  Just got a lot of work to do right now and can't spend too much time on extra activities such as this forum.  I'm glad I do have to time to sneak in once in a while.
- tbob

Inventor of the WORM Global
0 Kudos
Message 9 of 19
(4,283 Views)
Ok, so I got a char array of size 150, and a deeper knowledge of how this all works thanks to all of you. I really appreciate it.
So now I have a another problem. this dll that I am using returns a int16 value, and this value is to assign what error message is to be read. How is the wiring done so that a string indicator can realize what message to show?
Thanks again for all of your inputs! 🙂
0 Kudos
Message 10 of 19
(4,277 Views)