LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

LabVIEW call Win32 API DLL function from third party

Solved!
Go to solution

Hi everyone,

 

I'm trying to build a LabVIEW 2010 interface on 32-bit Window XP system to configurate and communicate to an Anglient laser head through its USB expansion box. I contacted the vendor and they shared a win32 API with some dll files. I started with a function called "A55292Find", but the CLF node always pop up error 1097.I checked the MSDN to get the corresponding LabVIEW data type for CLF.

 

My question is: Do I need to initialize those arguments? (How can I know the values of them if initialization is needed?)

Could anyone help me figure out what's the problem?

 

Here is what I have from API documents about this function:

 

The A55292Find function returns lists of handles and board types for all the 55292 USB
Expansion Modules connected to the system.

A55292RC __cdecl A55292Find( HANDLE hBoxes[], // List of handles for the boxes found. (as Unsigned 32-bit integer numeric) BOARDTYPE PortABrdType[], // List of board types for slot A boards (as Unsigned 16-bit integer numeric) BOARDTYPE PortBBrdType[], // List of board types for slot B boards (as Unsigned 16-bit integer numeric) WORD *spMaxBoxes); // dimension for all of the passed arrays. (as Unsigned 16-bit integer array data pointer) // (function return type as Unsigned 32-bit integer numeric) Parameters HBoxes[] Array that will receive the list of HANDLES for all the 55292 boxes connected. PortABrdType[] An array that will receive Slot ‘A’ board type information for each 55292 box. PortBBrdType[] An array that will receive Slot ‘B’ board type information for each 55292 box. spMxBoxes Passes the maximum number of values that can be held in hBoxes, PortABrdType, and PortBBoardType arrays to the A55292Find routine and returns the actual number of boxes found The return value will be one of the following A55292RC return codes: Value Meaning A55292_NO_ERROR No error A55292_TIMEOUT Timeout accessing a shared memory Remarks This routine must be called to obtain handles and board type information used in calling all the other routines. If there are significantly more array elements than boxes, the routine will take longer to complete.

These are the definitions: 

typedef DWORD A55292RC; enum{A55292_NO_ERROR, A55292_HANDLE_ERROR,...}

typeder WORD BOARDTYPE; enum{A55292_PCAL_BOARD, A55292_NO_BOARD, ...}

 

 

 

 

Thanks a lot,

 

Kang

0 Kudos
Message 1 of 8
(4,912 Views)

Unless explicitly stated in the documentation, when you call functions in a DLL that have arrays as parameters, the calling application is responsible for allocating the arrays. Thus, you need to pre-allocate the arrays in LabVIEW (i.e., by a simple Initialize Array and the appropriate datatype) and pass the individual arrays to the DLL call. The prototype for the function provides a means for you to tell it the size of the arrays you are passing via the spMaxBoxes parameter, which is itself another array.

 

Do you have the header file for this DLL? If so, you can use the Import Shared Library Wizard to have LabVIEW create the wrapper VIs for each function.

 

If not, I would suggest looking at the "Call DLL" example that ships with LabVIEW, as that shows how to call DLL functions using a variety of parameters, including arrays. If you still can't get it to work, then post your code and someone can help you out.

0 Kudos
Message 2 of 8
(4,909 Views)
Solution
Accepted by nkang11

The import library wizard likely will choke on some of the declaration or at least create some incomplete code. For one thing the import library wizard can not know how big the preallocated arrays need to be to pass into the Find function for instance.

 

The VI from the original post has just about every parameter wrongly configured. The first three are really arrays and ABSOLUTELY not integers passed by value. The last parameter is an integer but not passed by value but by reference. In addition the first parameter is really a pointer sized integer array, since the Window handles are defined as being opaque pointers. In addition the function is specifically defined as __cdecl and not as __stdcall in the header.

 

Attached is a corrected version of the VI. Note that this is technically corrected but by no means in the way I would consider necessary to make it a really useful VI for use in a library. For that it lacks proper error handling with error in and error out cluster, and also more useful controls (preferably enums for the board type. Also I did a shot in the blue with the 10 arrray elements to initialize. If you have more interfaces than that you won;t receive all of them unless you increase that number.

 

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 3 of 8
(4,901 Views)

Thank you so much! It works well now. The hBoxes[] parameter returns an integer 25356. So I could use this handle (it should also be an array data which is transported later to other functions.) to further CLF node.

0 Kudos
Message 4 of 8
(4,886 Views)

Thanks for your advice, it works fine now by using initialization array function.

0 Kudos
Message 5 of 8
(4,885 Views)

Hi Mr. Kalbermatter,

 

As you told me, I should try to use the defined enumeration words as control and indicator.

For the previous CLF node, it successfully returned the hardware configuration (I have only connected a board at slot A):

1.png

2.png

 

 

But I really want to see a string return as "A55292_PCAL_BOARD" which is defined by "typedef WORD BOARDTYPE; enumeration {...}; Does that mean I need to change the data type or do something like "converter"? Or initialize the array first, then convert it to string type?

 

Right now, I'm trying to open the laser card by using "A55292PcalOpen(HANDLE hDev, SLOTSELECT SlotSelector);" function which requires to use the enumeration constants from " typedef WORD SLOTSELECT; enum {A55292_PORT_A, A55292_PORT_B};" but I'm confused to shot an constant control to a new CLF node.

 

Since almost all of the arguments in dll function are typedefined as WORD, short and struct et al, I hope you could give me some hints on it.

Thank you very much sincerely!

 

Kang

 

 

0 Kudos
Message 6 of 8
(4,862 Views)

You can simply wire the value you are getting into an Index Array function, where the array is the string of arrays corresponding to the enumeration values. Alternatively, you can simply use a menu ring. The datatype will be numeric, but you will see the text of the menu ring item on the front panel. On the LabVIEW side it does not necessarily need to be an enumeration.

0 Kudos
Message 7 of 8
(4,859 Views)

@nkang11 wrote:

Hi Mr. Kalbermatter,

 

As you told me, I should try to use the defined enumeration words as control and indicator.

For the previous CLF node, it successfully returned the hardware configuration (I have only connected a board at slot A):

1.png

2.png

 

 

But I really want to see a string return as "A55292_PCAL_BOARD" which is defined by "typedef WORD BOARDTYPE; enumeration {...}; Does that mean I need to change the data type or do something like "converter"? Or initialize the array first, then convert it to string type?

 

Right now, I'm trying to open the laser card by using "A55292PcalOpen(HANDLE hDev, SLOTSELECT SlotSelector);" function which requires to use the enumeration constants from " typedef WORD SLOTSELECT; enum {A55292_PORT_A, A55292_PORT_B};" but I'm confused to shot an constant control to a new CLF node.

 

Since almost all of the arguments in dll function are typedefined as WORD, short and struct et al, I hope you could give me some hints on it.

Thank you very much sincerely!

 

Kang

 

 


Depending on the actual C implementation you can simply create a LabVIEW enum with the according item names or use a ring control instead. Teh enum requires the numeric values of the items to be in consecutive order without any gaps in between. This is a given if the C definition never assigns a specific number to the item. If the C enum is not consecutive you need to go with a ring control instead.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 8 of 8
(4,856 Views)