From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Labview Passing Class Pointers to a c++ dll

Solved!
Go to solution

I would like to call a c++ dll from labview.

The dll should have 3 functions or routines (see code below). The first function creates an instance to a class and returns this instance to labview. The second function will be called and labview should pass the pointer ot this instance to the function. This function should call the class methods. The third function delete the instance.

Can somebody tell me how to set the parameter for the Object pointer for the call library function node.

I tried it already in different ways, but everthing failed. Labview never passes the right pointer to the functions.

Thank you for your help!

Michael

 

Code:

 

#include <stdint.h>

#include <stdlib.h>

#include <stdio.h>

 

bool bnconstructor=false;

 

class GWCHF

{

public:

~GWCHF();

GWCHF();

void Method1();

 

private:

};

 

GWCHF::GWCHF() //Constructor

{

bnconstructor=true;

}

GWCHF::~GWCHF() //Destructor

{

bnconstructor=false;

}

void GWCHF::Method1()

{

//do calculations...

}

 

 

extern "C" _declspec(dllexport) int32_t _stdcall LVOpenGWCHF(GWCHF *Object);

extern "C" _declspec(dllexport) int32_t _stdcall LVRunGWCHF(GWCHF *Object);

extern "C" _declspec(dllexport) int32_t _stdcall LVCloseGWCHF(GWCHF *Object);

 

extern "C" _declspec(dllexport) int32_t _stdcall LVOpenGWCHF(GWCHF *Object)

{

//Creates an instance to GWCHF

Object =new GWCHF();

return 0;

}

extern "C" _declspec(dllexport) int32_t _stdcall LVRunGWCHF(GWCHF *Object)

{//The pointer of _GWCHF, generated in LVInitGWCHF should be passed to this routine

//by means of the pointer to object i would like to call the method1 from class GWCHF.

Object->Method1();

}

extern "C" _declspec(dllexport) int32_t _stdcall LVCloseGWCHF(GWCHF *Object)

{

//The pointer of _GWCHF, generated in LVInitGWCHF should be passed to this routine

//by means of Object and should be deleted or destroyed here.

delete(Object);

return 0;

 

}

 

0 Kudos
Message 1 of 8
(5,018 Views)

In the call to LVOpenGWCHF, you should pass the GWCHF parameter as a pointer-sized integer passed by pointer. That tells LabVIEW to create a pointer-sized integer, then pass a reference to it to the function. On the output side of the call library function node, you'll get an integer value containing the address of the GWCHF object. For the other functions, configure that value as a pointer-sized value passed by value because you want to pass the function the address of the object.

0 Kudos
Message 2 of 8
(4,988 Views)

Hello, thank you for the contribution.

I set the Object parameter to Signed pointer-size integer and as pointer to value for LVOpenGWCHF function and passed it to the others as value. It did not work for LVRunGWCHF and LVCloseGWCHF as the passed value is a Nullptr (0x0000000) then.

So I passed it as a pointer to value to these functions too. Labview passes a pointer but the address changed.

To give an example using the code shown when I raised the question.

In LVRunGWCHF the Intance pointer will be generated and the pointer  0x0eabc090 is given back to labview. Then labview calls LVRunGWCHF. The pointer which will be passed is 0x0c390ca8 which is different tothe above.

Astonishingly the call object->Method1() will be carried out even the pointer is different. Then Labview calls LVCloseGWCHF. The pointer Labview is passing is still 0x0c390ca8. But, when I want to delete this instance I will either get an error assertion failure or the program does not respond anymore.

Do I change the value of object to 0x0eabc090 by hand in LVRunGWCHF and LVCloseGWCHF, then everthing runs perfect.

Do you have any idea what labview is doing with my pointer or what could I try in addition to solve this problem?

Thank you for the help.

0 Kudos
Message 3 of 8
(4,960 Views)

Please post your VIs, saved in LabVIEW 2015 or earlier. It will be easier to help if we can see exactly what you're doing.

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

I have enclosed the VI as well as the code. As well as the vi as the code I prepared for testing, checking if it works.

I hope you will find a reason why it does not work.

Again: Thank you very much.

Download All
0 Kudos
Message 5 of 8
(4,953 Views)

Sorry, I added the wrong code file.

0 Kudos
Message 6 of 8
(4,949 Views)
Solution
Accepted by topic author RMW1

There's a problem with your C code: LVOpenGWCHF will never return the address of the new GWCHF Object to the calling function. LabVIEW passes an address, and then instead of updating the data at that address, you re-assign the local variable that holds the address, which means that new address never gets back to the LabVIEW code. Consider making your LVOpenGWCHF function:

extern "C" _declspec(dllexport) void _stdcall LVOpenGWCHF(GWCHF **Object)
{  
    *Object =new GWCHF();
}

(note the addition of an addition * before each instance of Object). Then, configure the  Call Library Function Nodes the way I suggested.

Message 7 of 8
(4,929 Views)

It works!

Thank you for your help.

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