LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Complex arrays for DLLs

I have spent a lot of time trying to pass arrays of complex numbers between LabVIEW and a DLL I have written in C++, but with no success. The data types I wish to interface with in the C++ are things like std::vector<std::complex<float> > and std::vector<std::complex<int16_t> > (or just the basic arrays they contain). However, I would be willing to use any sensible data representation (i.e. such that I don't need to shift it around in memory unnecessarily).

 

I have gone through all the DLL examples in LabVIEW 2013, which include complex numbers and arrays, but not arrays of complex numbers. I tried using the typedef struct approach for handling complex numbers (suggested in one example). This works fine within my C++ code: I can reinterpret_cast<...>(...) to get back to arrays of std::complex. However, every attempt at interfacing this with LabVIEW has failed in a variety of ways. At best, I managed to get some garbled numbers passing back and forth.

 

So, does anyone know where I can find a (very) basic example/step-by-step guide to passing complex arrays between LabVIEW and DLLs? Given that this is trivial in C++, I imagine there must be a straightforward solution out there somewhere.

 

Many thanks!

0 Kudos
Message 1 of 4
(2,795 Views)

A LabVIEW complex number is simply a structure with the imaginary and real value both represented as a floating point number. As such there is very little you can do on the LabVIEW side to change that. C++ object pointers are compiler specific and often even dependent on the version of the template library that was used to compile them. As such there is absolutely NO way to use them in any code that needs to be called from code that was not compiled with the same C++ compiler and the same compile settings. You can bend in circles and jump through hoops but there is simply no way to get around that.

 

The only C++ object oriented framework that I know and avoids this limitation for the most part is the COM/DCOM standard which imposes certain limits on object interfaces so that you can get around the compiler specific ABI conventions, but only if you are very careful about how you invoke the compiler. With a bit of macro magic that can be enabled in the IDL precompiler you can even call COM/DCOM objects from standard C. Also COM/DCOM definitely will not work wth C++ template libraries and the like.

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 4
(2,772 Views)

@rolfk wrote:

A LabVIEW complex number is simply a structure with the imaginary and real value both represented as a floating point number. As such there is very little you can do on the LabVIEW side to change that.


Yes, I noticed that LabVIEW does not support integer complex types (which are what I use most often). Therefore, what I have done on the LabVIEW side so far (for integer types) is to stuff the real and imaginary parts into integers of twice the size (e.g. 16-bit real/imag into 32bit integers). This seems to work pretty well, but does not seem to extend easily to float types. Is it possible to reinterpret N-bit floats as N-bit integers? If so, I could append floats in exactly the same way.


@rolfk wrote:

C++ object pointers are compiler specific and often even dependent on the version of the template library that was used to compile them. As such there is absolutely NO way to use them in any code that needs to be called from code that was not compiled with the same C++ compiler and the same compile settings. You can bend in circles and jump through hoops but there is simply no way to get around that.


I don't totally follow. However, I'm happy to avoid using object pointers if required. Really, I just want to pass (pointers to) blocks of bytes from LabVIEW into my DLL. I would prefer for these bytes to be arranged in a sensible manner, so I can dynamic_cast to std::complex<...> and carry on without any unnecessary shifting of the data.

0 Kudos
Message 3 of 4
(2,757 Views)

To answer my own question, it does look like LabVIEW supports type casting:

 

http://zone.ni.com/reference/en-XX/help/371361J-01/glang/type_cast/

 

Therefore, it should be possible to stuff complex floats into integers of twice the size. As usual, I still have to worry that LabVIEW will implement this in a horrendously inefficient way behind the scenes, but this should work in principle.

 

Many thanks, Rolf, for your insightful comments.

 

P.S. I should note that I realise that the storage format of std::complex is not specified in the C++ standard. However, all major implementations of the standard library do conform to the same format (interleaved, as per the C99 standard). A very good discussion of this can be found here:

 

http://stackoverflow.com/questions/24229808/getting-pointers-to-the-real-and-imaginary-parts-of-a-co...

0 Kudos
Message 4 of 4
(2,749 Views)