LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling frunction from dll that has struct parameters that consist of struct

I have function in DLL:

 

unsinged long MyFunction( void *pHandle, OverCurrentProtection *pStatus);

 

Parameter *pStatus is of a type OverCurrentProtection that is defined in c++ as:

 

struct OverCurrentProtection : OverVoltageProtection

{

 uint32_t current_ma

uint32_t current_A

}

 

and the parent class of a OverCurrentProtection is OverVoltageProtection, that is defined as:

 

struct OverVoltageProtection

{

 uint32_t testFinished;

uint32_t rec = 0;

}

 

When I try to import DLL using Import Shared Library wizard the function is not recognized.

My Questions:

1. How can function be defined using Call library function node?

2. How should function be defined in c++ so that Labview will be able to recognize it?

 

 

0 Kudos
Message 1 of 2
(2,055 Views)

@Skoda3 wrote:

 


When I try to import DLL using Import Shared Library wizard the function is not recognized.

My Questions:

1. How can function be defined using Call library function node?

2. How should function be defined in c++ so that Labview will be able to recognize it?

 


Very simple: de-C++ify it! The Call Library Node is not able to interface to C++ code. Now for this specific case where only the structs are C++ syntax, this doesn't mean that the code is really an object. It is really the same as this C syntax:

 

 

typedef union
{
struct
{
uint32_t testFinished;
uint32_t rec;
} OverVoltageProtection;
struct {
uint32_t current_ma;
uint32_t current_A; } OverCurrentProtection; } Protection;

But the import library wizard does not support any C++ syntax of any sort (and neither some of the C11 and newer stuff!

 

That said, the import library wizard is despite its name no magician either. It should NEVER be used for more than the first step to create a VI library to interface to your DLL. Every single VI needs to be manually checked and rechecked afterwards to be really correct and that is the really challenging part that requires the most work in terms of C knwledge and time. The import library wizard should ONLY be used to ease the creation of initial VI wrappers, but in my experience that part is only a very small fraction of time and effort in comparison to making a real workable VI library to call a DLL. I never use it myself, the time saving is minimal to non-existent and the icons look pretty bad Smiley Very Happy

Most use it because they don't really know how to setup the Call Library Node properly but how can you make sure the Call Library Node is setup right if you don't know that? The C syntax is notorously unsuited to define anything about the requirements for a function call, besides the basic datatype of the parameter list. Not a single yota about memory allocation requirements for non scalar parameters and such is present in the C syntax and that is the most challenging part about making a DLL import VI library that properly works rather than trashing your memory randomly (and about calling a C function in a C program too!). Since the C syntax doesn't describe many of the aspects of how to call a function properly, the import library wizard can't suddenly and magically pull this out of the hat when scanning the header file. And inside the DLL there is even less information present. So for many things it just has to guess based on the most common situations and it pretty regularly will guess wrong.

 

Remember: Just because your VI library is not crashing LabVIEW anymore, does not mean that you have successfully and properly interfaced a DLL! There needs to be substantial proof by someone who knows the stuff that your VI library is actually correct.

 

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