LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calling C++ DLL function in labview

Hi, I am writing a labview wrapper for some DLL functions (C++). The problem is that the name displayed does not exactly match the one selected in the dropdown list (please check the picture). Is this what NI called "name mangling"? If so, how do I export the names correctly using VC6 with the original DLL (no source code)? Your help will be highly appreciated.

-Joe
0 Kudos
Message 1 of 14
(4,981 Views)
Hi Joe;

You need to use the declaration extern "C" to prevent C++ name decorations.

For example:

extern "C"
{

_declspec(dllexport) long Function_1(int var1);

}

// Then, do whatever in your function...

_declspec(dllexport) long Function_1(int var1)
{
// Whatever...
}

etc.

The manual Using External Code in LabVIEW also refers to this.

Best regards;
Enrique Vargas
www.visecurity.com
www.vartortech.com
Message 2 of 14
(4,980 Views)
Joe Guo wrote:
> Hi, I am writing a labview wrapper for some DLL functions (C++). The
> problem is that the name displayed does not exactly match the one
> selected in the dropdown list (please check the picture). Is this
> what NI called "name mangling"?

Yes! Your name mangling is the default stdcall name mangling from MSVC.

> If so, how do I export the names correctly using VC6 with the original
> DLL (no source code)? Your help will be highly appreciated.

You don't, unless you wan't to do Hex editing in the DLL file itself!

The export table is build at link time by the linker based on either a
..def file or on hints added to the binary object file added by the
compiler based on declspec(dllexport) statements in the source code.

If the C source did n
ot declare the function prototypes in between

#ifdef __cplusplus
extern "C" {
#endif

function prototype declaration

#ifdef __cplusplus
}
#endif

and uses the the declspec(dllexport) hint the DLL names are what you got
in your case.

But why bother at all. LabVIEW is able to deal with those mangled names
so just leave it alone.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 3 of 14
(4,980 Views)
I wanted to post a real quick note to get knowledge of the example out.

There is a great example Call DLL.vi that shows how to call DLLs with a tom of different data types. It has example C code and the LabVIEW code calling it.
You can find it in the examples folder under dll/data passing/calling native code.llb

Evan
Message 4 of 14
(4,980 Views)
Thanks for the info, Evan.

-Joe
0 Kudos
Message 5 of 14
(4,981 Views)
Thanks for everyone's help. Here is another question. The functions in the DLL returns a pointer to an error type-def. However, labview has only limited options for the return value. Is it possible to de-reference the pointer? or is it too late once the call library function finishes?

Thanks,

-Joe
0 Kudos
Message 6 of 14
(4,981 Views)
Joe Guo wrote:

> Thanks for everyone's help. Here is another question. The functions
> in the DLL returns a pointer to an error type-def. However, labview
> has only limited options for the return value. Is it possible to
> de-reference the pointer? or is it too late once the call library
> function finishes?

How does your error type def look? Maybe it is just an enum which is
basically just an integer so there wouldn't be a problem.

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

the error type def is a record/cluster, consists of an integer and a fix length char array.

-Joe
0 Kudos
Message 8 of 14
(4,981 Views)
Joe Guo wrote:
> Rolf,
>
> the error type def is a record/cluster, consists of an integer and a
> fix length char array.

Quite bad to do!! But you can define it as an uInt32 and then use a Call
Library Node with following setup:

Library Name: LabVIEW
Function Name: MoveBlock
Calling Convention: cdecl

return value: void
1. param: source (uInt32 by value) (your function return value)
2. param: dest (uInt8 array) as C pointer
3. param: length (int32) (length of the structure in bytes)

Initialize the array to the necessary numbers of bytes before the Call
Library Node call. The length should be your fixed size char array
length + 4 for the uInt32.

Remains the question if the memory returned is maintained statically in
the function whic
h would mean everything is alright or if the function
allocates it dynamically and you should free it afterwards. How to free
it if that is necessary should be documented in your API documentation
for that function.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 9 of 14
(4,981 Views)
Rolf, I have hard time to find the LabVIEW library. What's the exact name? Thanks.

-Joe
0 Kudos
Message 10 of 14
(4,981 Views)