LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

DsNewPtr, MoveBlock. Simple example crashes labview

Solved!
Go to solution

Hi,

I am trying to call a dll which uses nested structs and the structs contain arrays.  I may have to use DSNewPtr and MoveBlock I think.  However, I have created a very simple example to test these functions and while the VI will run and return the correct data, LabVIEW crashes when I close the VI.  I guess a size or type is configured incorrectly but I cannot spot where. I have tried Signed and Unsigned Pointer Integers. Please take a look, thank you, 

Michael.MoveBlock.png

0 Kudos
Message 1 of 17
(6,716 Views)

My guess is the second MoveBlock needs a fixed size array constant wiring in to set the size of the destination array.  I'm out so I will try when I get back.

0 Kudos
Message 2 of 17
(6,685 Views)
Solution
Accepted by topic author Michael_78

Setting the datatype to Array instead of Adapt to Type works for this example.

0 Kudos
Message 3 of 17
(6,674 Views)
Solution
Accepted by topic author Michael_78

@Michael_78 wrote:

Setting the datatype to Array instead of Adapt to Type works for this example.


That may seem to work but is only a partial fix! It corrects the datatype so that the MoveBlock function can actually see the pointer in the actual LabVIEW array instad of the handle, which the MoveBlock function knows absolutely nothing about!

 

But!!!!

The pointer that the second MoveBlock sees is a pointer to an array that only can hold 0 bytes of data (because you didn't pre-initialize the array to a specific size).

 

You need to make sure that LabVIEW preallocates the array to the necessary number of elements by either explicitedly adding an Initialize Array function to create an array of the neccessary number of U8 elements, or configure the Call Library Node for that parameter to use the size input as "Minimum size".

Move Blocks.png

 

Also to be safe you should make sure that the array that is wired to the first MoveBlock function actually does contain at least the number of elements that you tell the MoveBlock functìon to copy, as there is a chance that the memory beyond the end of the array is not mapped into the current process and even just reading it might generate a general protection fault anyhow.

Rolf Kalbermatter
My Blog
Message 4 of 17
(6,626 Views)

Hi,

 

DLL returns a Pointer to the array (4 byte) in my project. Can I use similar move Block example to read data located at memory address mentioned in 'pointer to array'?

0 Kudos
Message 5 of 17
(5,720 Views)

Yes you can, but you may not have to! Show us what your DLL does and we can very likely show you how to configure your Call Library Node in a way that does not require this kind of magic.

Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 17
(5,715 Views)

Hello, I have to do the same thing but in the ways : get memory address from a 2D array to send it to the DLL and recreate a 2D array from a pointer received from the DLL. Is there a simple way to do that ?

Mathieu Gauquelin
0 Kudos
Message 7 of 17
(5,573 Views)

My previous answer applies here too. Show us what you need to do. There are about 500 different things that might be done about this depending on your specific use case and I'm not going to explain them all here. Smiley Very Happy

Rolf Kalbermatter
My Blog
0 Kudos
Message 8 of 17
(5,568 Views)

I will try to be clear in my explanations.

 

I wrote a C++ DLL with Visual Studio and add a C wrapper to use it on several langages (C++, Python, LabVIEW, ...). In this one, I am dealing with images, so 2D array. I created a labview library using LabVIEW tools so I attached this library to my project.

First, the function to allocate and free the handle for the DLL is working correctly so the communication seems okay. Moreover, my DLL function take as parameter a pointer on the first pixel of the image, so on the first value of the tab, in order to recreate a matrix (library OpenCV) to perform some operations.

 

Here is the C function:

__declspec(dllimport) unsigned short* __stdcall imAdjustBrightness(void* handle, void * ptrImg, int width, int height, int inputType, int brightnessValue)
{
    if (handle)
    {
        EpsImageProcessing* data = (EpsImageProcessing*)handle;
        return data->imAdjustBrightness(ptrImg, width, height, inputType, brightnessValue);
    }
    return NULL;
}

And some images to illustrate :

image.pngimage.png

Mathieu Gauquelin
0 Kudos
Message 9 of 17
(5,565 Views)

Just configure the second parameter of the Call Library Node to be of type Array, 2 dimensions, integer type of your 2D image array, and passed as C array pointer and you are done.

 

Obviously you will also need to make sure that the inputType really matches the image data that you load and pass in, or your EpsImageProcessing function might screw up when trying to reference the array pointer as something else than it is.

Rolf Kalbermatter
My Blog
Message 10 of 17
(5,557 Views)