LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle a function with struct return from dll?

Solved!
Go to solution

I have a dll function "SCTSCTL_OPEN" returns a structure like

 

typedef struct _sctsctl_t {
 HANDLE hDevice;
 //unsigned char ctl[2];
 unsigned long data_in;
 unsigned long data_diff;
 HANDLE IsrThread;
 HANDLE IsrEventHandle; 
 PVOID pExt;
 void (*handler)(struct _sctsctl_t* board, int intvec);
} *sctsctl_t;

 

SCTS_API sctsctl_t  __stdcall SCTSCTL_OPEN(DWORD instance);

 

I imported this dll with LabVIEW and vi returned a unsigned long integer shown as follow,

 

return.PNG

 

How do I get a return with structure? Any help would be appreicated.

 

Regards,

Adam

LV 8.5.1

0 Kudos
Message 1 of 7
(7,576 Views)
Solution
Accepted by topic author Meng-Shiang.Lin
Normally I would be attempted to make a wrapper function and call it with LV. LV handles structure not so naturally.
0 Kudos
Message 2 of 7
(7,566 Views)

LabVIEW doesn't support custom structures as return values.

That needn't be a problem as long as you don't want to access

members of your structures directly. LabVIEW treats it as an int

which means as an address of a plain pointer.

Try to feed this address to a function which expects a pointer

to your structure.

 

If you really need your struct members then you have to create some

glue code in C which exposes the required members to LabVIEW.

 

0 Kudos
Message 3 of 7
(7,565 Views)

Hi bo200008,

 

Could you please explain in detail? I really have no idea how to make a wrapper function with LabVIEW.Smiley Sad

 

Thank you!!

Adam

0 Kudos
Message 4 of 7
(7,545 Views)

It appears your DLL function is returning a pointer to the struct you're interested in. If other DLL functions take a pointer to this struct as an input, then you might not need to ever translate it into a LabVIEW cluster. Simply pass that pointer into the next function's input parameter.

 

If you do need individual access to the struct data members, however, you don't necessarily need a wrapper DLL that you program yourself in C. You can access the data directly using only LabVIEW using the Moveblock function. (See Chapter 6 of this document for more detailed info.)

 

This function is exported in the LabVIEW exe itself. Instead of referencing a DLL in the Call Library Function Node, just type in LabVIEW. No extension required. Then select the function Moveblock. This functions takes as an input a pointer to the source and a pointer to the destination where you'll copy the memory to, and finally the size of the memory block to copy.

 

You can pass in your struct pointer as the source pointer, and an appropriate LabVIEW cluster as the destination pointer with the input configuration Adapt to Type (Handles by Value). LabVIEW will then copy whatever data the struct pointer references into the LabVIEW cluster memory zone. If they match up perfectly, then that cluster will contain all the data you need. You will need to be very careful about the data sizes and arrangement in the LabVIEW cluster, or you might corrupt memory.

 

Here's a very ROUGH example of how to get started, but beware this example is not meant to be run immediately. You should inspect and fix any mistakes. But it will get you started if you feel like going this route. The advantage is that this is all contained in LabVIEW, and you don't need any extra DLLs to get the job done.

 

Example saved in LV8.6.

 

Message Edited by Jarrod S. on 07-07-2009 08:07 PM
Jarrod S.
National Instruments
Download All
0 Kudos
Message 5 of 7
(7,517 Views)

Hi Jarrod,

 

The link was dead, I found this one.

 

www.ni.com/pdf/manuals/370109a.pdf

 

Could you please give this example with LabVIEW 8.5? Thanks a million~~

 

Adam

Message Edited by Adam1122 on 07-07-2009 10:57 PM
0 Kudos
Message 6 of 7
(7,501 Views)

Adam1122 wrote:

Hi bo200008,

 

Could you please explain in detail? I really have no idea how to make a wrapper function with LabVIEW.Smiley Sad

 

Thank you!!

Adam


You don't typically make a wrapper function in LabVIEW but use C instead to create a DLL that contains functions to translate between neat LabVIEW datatypes and the actual original DLL dataypes

 

Jarrods solution is nice and can help in isolated cases but it is not a good approach if you want to interface to a complete API with many different functions who have a lot of such complicated parameters or return values.

 

You should be able to create what you need from the picture visible in Jarrods post. It may sound harsh and unfriendly but if that is not possible for you you will also have a lot of troubles to make something useful out of the VI Jarrod posted even if you can open it in your LabVIEW version.

 

Rolf Kalbermatter

Rolf Kalbermatter
My Blog
0 Kudos
Message 7 of 7
(7,492 Views)