LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing char ** to Labview library function

I am trying to interface to a DLL written in C with labview and
stumbled on a routine with the following prototype:

int getVersion (char **version);

The routine allocates a string buffer locally and returns a pointer to
the string buffer in the argument passed to the function. The
application calling this routine does NOT have to allocate the string
buffer, as it is done in the library.

My goal is to call this function in labview and display the result as
a string. Is this possible in Labview?

As a note, here is how the function is used in a C program:

char *version;
rv = getVersion (&version);
if (rv != SUCCESS) {
goto exit;
}
printf ("Version: %s\n", version);


Thanks in advance
0 Kudos
Message 1 of 3
(2,907 Views)
John wrote:

> I am trying to interface to a DLL written in C with labview and
> stumbled on a routine with the following prototype:
>
> int getVersion (char **version);
>
> The routine allocates a string buffer locally and returns a pointer to
> the string buffer in the argument passed to the function. The
> application calling this routine does NOT have to allocate the string
> buffer, as it is done in the library.
>
> My goal is to call this function in labview and display the result as
> a string. Is this possible in Labview?
>
> As a note, here is how the function is used in a C program:
>
> char *version;
> rv = getVersion (&version);
> if (rv != SUCCESS) {
> goto exit;
> }
> printf ("Version: %s\n", version);

Checkout my answer to the thread "W
in32 call--Possible to use a pointer
to a pointer?". This is the same issue.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 2 of 3
(2,907 Views)
> My goal is to call this function in labview and display the result as
> a string. Is this possible in Labview?
>
> As a note, here is how the function is used in a C program:
>
> char *version;
> rv = getVersion (&version);
> if (rv != SUCCESS) {
> goto exit;
> }
> printf ("Version: %s\n", version);
>
>

Strings in LV are always dynamically allocated, and this DLL is
returning a pointer to or into a static buffer where it retains
ownership and you aren't supposed to free the pointer or anything. LV
doesn't have a string that can just point to this other memory, and so
the string needs to be copied into the LV buffer instead of just being
pointed to.

One way of doing this is, as Rolf pointed out, to make one call the
getVersion function
passing it an integer by pointer. It will overwrite
the integer, and you will have the pointer to the string stored in an
int32. You can pass this pointer to something else, like MoveBlock
along with the pointer to a LV string buffer and get the contents moved
into the LV string. Note that the LV string buffer will need to be
presized, and that MoveBlock is a very low level function that can
easily corrupt memory or crash LV if given bad parameters.

The other way to do this is to write a simple wrapper DLL that does
pretty much what the code above does. It takes in a string buffer and
resizes it and prints/copies the version string into the buffer string.
If you use sprintf instead of printf, it will put the "Version: %s\n"
into the buffer instead of to an output stream. You may also want to
make the function return the size to preallocate instead of doing its
own resizing. Either way, you wind up with a very small wrapper
function that makes this way more useabl
e from LV or from anywhere else
that is returning pointers to static strings.

Greg McKaskle
0 Kudos
Message 3 of 3
(2,907 Views)