10-16-2014 09:21 AM
Hi
I have an application that is working well in Visual C++ 2012. I would like to load the dll library on labview and to execute the code on labview. I have only the DLL file, the working application and the help file. Is it possible to realise this task on labview using Call Library Function Node.
I have par example the following code in .cpp file:
void CMTBClientUsingCOMDlg::OnBnClickedConnect()
{
// TODO: Add your control notification handler code here
try
{
// login to MTB, using english language
m_MTBConnection->Login(("en"), &m_ID);
// get MTB root (forcing an internal QueryInterface() on IMTBRoot!)
m_Root = (IUnknown*)(m_MTBConnection->GetRoot((BSTR)m_ID));
// ask root to return the number of devices
int count = m_Root->GetDeviceCount();
// list all devices
for (int i=0; i < count; i++)
{
_bstr_t name = ((IMTBIdentPtr)m_Root->GetDevice(i))->GetName();
m_ComboDevices.AddString(name);
}
if( m_ComboDevices.GetCount() > 0)
m_ComboDevices.SetCurSel(0);
OnCbnSelchangeDevices();
m_BtnConnect.EnableWindow(false);
m_BtnDisconnect.EnableWindow(true);
}
catch(_com_error e)
{
// connection close
OnBnClickedDisconnect();
DisplayError(&e);
}
}
How to execute this code on labview?
Best regards
10-16-2014 09:47 AM
http://digital.ni.com/public.nsf/allkb/DCB90714981A1F148625731E00797C33
http://zone.ni.com/reference/en-XX/help/371361J-01/lvexcodeconcepts/configuring_the_clf_node/
I've never had an easy time doing this, but then again most the times I have this task I don't have the original source so maybe you'll have better luck.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord
10-16-2014 02:29 PM - edited 10-16-2014 02:30 PM
It really depends what your DLL is (and your example code shows absolute nothing about how the objects are initialized/instantiated). How do you get the m_MTBConnection variable initialized, for instance?
If it is an ActiveX interface you will use the Active X functions in LabVIEW. If it is however a C++ DLL you will have to write a C wrapper for every method you want to call as well as a C function to instantiate and dispose of the object.
10-16-2014 05:37 PM
Is there any way you can write this with .NET ? Should be simple to port it to C# or something that can adapt this code quickly. The benefit is you can use .NET constructor which is way way wayyyy simpler than using Call Library Node.
10-17-2014 03:07 AM - edited 10-17-2014 03:31 AM
Hi
I have attached the help file describing the dll library (MTBApi.dll). My working program made on Visual C++ Dot Net is very big and therefore I can’t attach it.
In my header file I import at the beginning the dll library on this way:
#import "MTBApi.tlb" named_guids
using namespace MTBApi;
Than I have a class:
class CMTBClientUsingCOMDlg : public CDialogEx
{
public:
CMTBClientUsingCOMDlg(CWnd* pParent = NULL); // standard constructor
~CMTBClientUsingCOMDlg(); // standard destructor
enum { IDD = IDD_MTBCLIENTUSINGCOM_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
protected:
HICON m_hIcon;
IMTBConnectionPtr m_MTBConnection;
IMTBRootPtr m_Root;
CComBSTR m_ID;
IMTBChangerPtr m_Changer;
IMTBDevicePtr m_Device;
...
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedConnect();
CButton m_BtnConnect;
};
In my .cpp file I have:
CMTBClientUsingCOMDlg::CMTBClientUsingCOMDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMTBClientUsingCOMDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
CoInitialize(NULL);
try
{
// create an instance of the connection class which can connect to the server
m_MTBConnection = IMTBConnectionPtr(CLSID_MTBConnection);
}
catch(_com_error e)
{
...
}
...
}
CMTBClientUsingCOMDlg::~CMTBClientUsingCOMDlg()
{
...
}
void CMTBClientUsingCOMDlg::DisplayError(_com_error* e)
{
...
}
void CMTBClientUsingCOMDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_CONNECT, m_BtnConnect);
}
BEGIN_MESSAGE_MAP(CMTBClientUsingCOMDlg, CDialogEx)
ON_BN_CLICKED(IDC_CONNECT, &CMTBClientUsingCOMDlg::OnBnClickedConnect)
...
END_MESSAGE_MAP()
BOOL CMTBClientUsingCOMDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
{
...
}
void CMTBClientUsingCOMDlg::OnPaint()
{
...
}
void CMTBClientUsingCOMDlg::OnBnClickedConnect()
{
try
{
// login to MTB, using english language
m_MTBConnection->Login(("en"), &m_ID);
// get MTB root (forcing an internal QueryInterface() on IMTBRoot!)
m_Root = (IUnknown*)(m_MTBConnection->GetRoot((BSTR)m_ID));
// ask root to return the number of devices
int count = m_Root->GetDeviceCount();
// list all devices
for (int i=0; i < count; i++)
{
_bstr_t name = ((IMTBIdentPtr)m_Root->GetDevice(i))->GetName();
}
catch(_com_error e)
{
// connection close
}
}
My question is how to start this code in labview. How to write the function in labview, par example this function:
m_MTBConnection->Login(("en"), &m_ID);
Best regards
10-17-2014 04:43 AM
This looks very much like a an ActiveX library, but the type library is not included but distributed as a seperate file. As such you should look into the ActiveX functions in LabVIEW to import it into LabVIEW.
Something like following should get you started. The typecast from the refnum from the GetDevice() method I'm not sure about but the rest is basically exactly what you source code example you posted is doing.
You will need both the .tlb and .dll file in the same folder (preferably in the folder where you also save your VIs for accessing this library, and have to point the MTBApi.IMTBConnection refnum to the .tlb file.
10-17-2014 07:18 AM
Hi
Thank you for your answer. I tried the code. When launching the code there is only one error after the execution of IMTBIdent property.
10-17-2014 07:25 AM
Well I checked in another project and ActiveX refnum typecasting needs to use the Variant to Data function. Does this work better?
10-17-2014 07:44 AM
Hi
Unfortunately the situation is the same.
10-20-2014 05:12 AM - edited 10-20-2014 05:13 AM
Does the Variant to Data node throw an error or does the IMTBIdent->Name property give you an error? And what error code do you get?