06-11-2011 04:52 PM - edited 06-11-2011 04:59 PM
Hello, I've noticed that I cannot do ' extern "C" ' on the methods Open(), Close(), StartStreaming(), and StopStreaming() in the following Class:
class ApplicationIo : public FiclIo
{
friend class X5ScriptPlayer;
friend class ApplicationSettings;
typedef std::vector<__int64> IntArray;
public:
//
// Member Functions
ApplicationIo(IUserInterface * ui);
~ApplicationIo();
ModuleIo & ModIo()
{ return Module; }
unsigned int BoardCount();
void Open();
bool IsOpen()
{ return Opened; }
void Close();
void StartStreaming();
void StopStreaming();
...
}
How can I export the methods of the Class mentioned above? I've read the manuals and seen examples about how to get DLLs working in LabView, however none of them ever talk about when there is an actual Class involved and exporting the methods of said Class.
Any help would be greatly appreciated.
06-11-2011 08:50 PM
As noted here, you cannot call functions inside C++ classes inside a DLL from LabVIEW. You will need to write C wrappers around the class and export those functions.
06-11-2011 08:55 PM
Extern c beasically tells the compiler to look at the method as if it's written in c and should be compiled using c mechinsms. C doesn't support classes, so I don't think you could define a c++ concept - classes, to be compiled as c.
What are you trying to do, i.e. can your explain project a bit more? Because you're trying to access a object's methods without calling them on a object (i.e. when exporting them in a dll the program loading the dll doesn't call it on a object).
06-11-2011 11:21 PM
Okay, well at least that clears up one piece of the puzzle.
Here is the wrapper I need:
Here's the .h of the wrapper:
------------------------------------
#ifndef _APPIO_DLL_WRAPPER_H_
#define _APPIO_DLL_WRAPPER_H_
#define DLLEXPORT __declspec (dllexport)
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ApplicationIo ApplicationIo; /* make the class opaque to the wrapper */
DLLEXPORT ApplicationIo* Construct_AppIo(IUserInterface* ui);
DLLEXPORT void Destruct_AppIo(ApplicationIo* LV_Ref);
DLLEXPORT void Open(ApplicationIo* LV_Ref);
DLLEXPORT void Close(ApplicationIo* LV_Ref);
DLLEXPORT void StartStreaming(ApplicationIo* LV_Ref);
DLLEXPORT void StopStreaming(ApplicationIo* LV_Ref);
#ifdef __cplusplus
}
#endif
#endif /* _APPIO_DLL_WRAPPER_H_ */
Here's the .cpp of the wrapper:
--------------------------------------
#include "AppIoDll.h"
#include "ApplicationIo.h"
ApplicationIo* Construct_AppIo(IUserInterface* ui)
{
return new ApplicationIo(ui);
}
void Destruct_AppIo(ApplicationIo* LV_Ref)
{
LV_Ref->Close();
delete LV_Ref;
}
void Open(ApplicationIo* LV_Ref)
{
LV_Ref->Open();
}
void Close(ApplicationIo* LV_Ref)
{
LV_Ref->Close();
}
void StartStreaming(ApplicationIo* LV_Ref)
{
LV_Ref->StartStreaming();
}
void StopStreaming(ApplicationIo* LV_Ref)
{
LV_Ref->StopStreaming();
}
The one problem I have with this is how am I supposed to get the pointer to the IUserInterface class to the C Wrapper constructor?
06-12-2011 09:20 AM - edited 06-12-2011 09:22 AM
Two things: 1, I don't think this will compile because in the h file you're using the IUserInterface class but you haven't declared yet. Two, I imagine that the IUserInterface class is supposed to be passed in to the ApplicationIo constructor because it's used to initialize the new ApplicationIo struct based on what's in the IUserInterface class. If that's the case you'd have had to create the IUserInterface class earlier and passed it in.
However, to get around this do this (it's probably break the code because it expects a initialized IUserInterface class and perhaps it expects to hold on to the ui object so deleting it might invalidate the ai object).
h file DLLEXPORT ApplicationIo* Construct_AppIo(); cpp file ApplicationIo* Construct_AppIo() { IUserInterface* ui= new IUserInterface(); ApplicationIo* ai= new ApplicationIo(ui); delete ui; return ai; }
Alternitavly, the ApplicationIo class might have a constructor that doesn't require IUserInterface as input, i.e. ApplicationIo* ai= new ApplicationIo();
Didn't you get any example code with these libraries? I'd have to understand the library better before I could tell what the IUserInterface is supposed to do.