LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Need to pass in a pointer to a function via Call Lib. function

I have a C DLL which takes a pointer and create a buffer then writes data to it and let me know the size of it. I then have to read the data from the buffer. I think Call Library Function let you pass in a pointer. What do I write to the pointer and where is the buffer created? How do I read it?
0 Kudos
Message 1 of 7
(3,287 Views)


@mchips wrote:
I have a C DLL which takes a pointer and create a buffer then writes data to it and let me know the size of it. I then have to read the data from the buffer. I think Call Library Function let you pass in a pointer. What do I write to the pointer and where is the buffer created? How do I read it?


When calling DLLs it is always the best to have the caller allocate the buffers, pass a pointer to the DLL and let the DLL read or write to that buffer. Having the DLL allocate a buffer is somewhat cumbersome to pass back to any caller if you want the caller to be able to directly access that buffer too. And you need to provide a deallocation routine in your DLL as well since you can't assume that the memory management of your DLL is done in exactly the same way than in the caller  and therefore memory always must be deallocated by the entity that created it. And since you are at it adding a deallocater function to your DLL, you would also be good advised to write accessor functions too for reading and writing to/from the buffer.

Doing the allocation in the caller will not require you to privide a deallocator, nor accessor functions in the DLL.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 2 of 7
(3,280 Views)
When calling DLLs it is always the best to have the caller allocate the buffers, pass a pointer to the DLL and let the DLL read or write to that buffer.

Thanks for your reply. This is exactly what my SW engineer wants to do. I need to know how to do it in LV. I am using Call Library Function but I am not sure how to allocate the buffer and pass a pointer of the buffer to the DLL. Thanks.
0 Kudos
Message 3 of 7
(3,274 Views)
It depends on the data type. For example, if it's an array, create an array of the required size with Initialize Array, then pass it to the C function as an Array Data Pointer.
However, with this technique it's the vi that allocates memory; the C function should simply fill the buffer. If you don't know how much data will be returned, you may write (for example) a two-step interface:
1st step: you call the function, it allocates memory, fill the buffer and returns the data size,
2nd step: you allocate memory in your vi and call a second C function that copies data to your buffer and deallocates the original buffer

Paolo

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
Message 4 of 7
(3,271 Views)
Thanks for your reply. It is the first step that he wish to implement. I am having a hard time understading the concept of passing in a pointer to the DLL and later read data from it. How do I get (assign) a pointer of memory in LV? How do i later access data in the memory using the pointer?
Thanks in advance.
0 Kudos
Message 5 of 7
(3,266 Views)
You don't have pointers in LV: you only have wires (coming out from LV functions or controls) that you can pass to/from a C function. A wire is a LV object that (usually) contains data and so it owns some allocated memory.
By configuring the Call Library Function (double click on the node), you decide how to pass these LV objects to the external C function. Specifically, you may choose to pass it as a pointer. If you do so, the C function will receive a pointer to the memory buffer allocated by LV. The C function may manipulate this area, provided it does it the right way (e.g. not overflowing the buffer). When you configure the input as a pointer, you get a corresponding output terminal on the right side of the node from which you can wire out the (manipulated) results. The size of the resulting buffer will be the same as was in input.
Remember that the input may be not only an array, but also a string or a simple value (e.g. an I32).
Check the example provided with the LV help to become familiar with this.
I hope this helps

Paolo
Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
Message 6 of 7
(3,265 Views)
I am asking him to break it into two DLL to give me a size of the buffer then I create it and pass a pointer (wire) and read (right side of the CLF node).  Thanks a lot!
0 Kudos
Message 7 of 7
(3,254 Views)