LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to fit my C++ program into labview interface?

The driver for the joystick's force feedback is written in C++,  now I want to use Labview to acquire data to run the C++ driver, so could I fit the C++ executable file into Labview?
0 Kudos
Message 1 of 22
(15,944 Views)
If you have the source code, I would compile it into a DLL with an extern "C" interface and call it from LabVIEW via the Call Shared Library node. If you don't have the source code, or it must remain an EXE, then you'll need to use the command shell execute VI to run the EXE, passing it the parameters via the command line and parsing the output text...obviously, the DLL is much, much easier.
Message 2 of 22
(15,921 Views)
Thanks, I will try! : )
0 Kudos
Message 3 of 22
(15,900 Views)
Hi, Brian,
 
I have built(not compile, I don't know the difference of building solution and compiling) one .dll file in Visual Studio 2005 (C++), and then I applied it to the Call Shared Library Node, but this error happened:
 
This applilcation has failed to start because the application configuration is incorrect, reinstalling the applicaition may fix this problem. LABVIEW: Generic error. The file "bbbF.dll" is not a valid LABVIEW file.
 
 
Actually, this .dll file I built can be called in C++ using the implicit way.  But when I ran them in LABVIEW, the error above happened. So what shall I do next?
 
Thanks in advance...
 
skywalker
0 Kudos
Message 4 of 22
(15,857 Views)


@skywalker_hf05 wrote:
Hi, Brian,
 
I have built(not compile, I don't know the difference of building solution and compiling) one .dll file in Visual Studio 2005 (C++), and then I applied it to the Call Shared Library Node, but this error happened:
 
This applilcation has failed to start because the application configuration is incorrect, reinstalling the applicaition may fix this problem. LABVIEW: Generic error. The file "bbbF.dll" is not a valid LABVIEW file.
 
 
Actually, this .dll file I built can be called in C++ using the implicit way.  But when I ran them in LABVIEW, the error above happened. So what shall I do next?
 
Thanks in advance...
 
skywalker


You probably missed in the first answer the part about putting the exports of your DLL in an extern "C" interface. The Call Library Node is a function call interface and can only link to globally exported standard C functions. You can not link to C++ objects or interfaces with it nor with any other method in LabVIEW directly. LabVIEW has however an ActiveX interface that allows to call ActiveX enabled libraries.
Your options are therefore to write standard C wrapper functions that wrap all the relevant C++ interface methods and public variable access that you need using the Call Library Node in LabVIEW or develop an ActiveX enabled interface for your C++ library and use LabVIEWs ActiveX interface methods.
 
Rolf Kalbermatter
Rolf Kalbermatter
My Blog
Message 5 of 22
(15,847 Views)
But in the "call dll.vi" example in LABVIEW, there seems to be some C++ source code like structures of all kinds. The joystick driver at least has 3 big objects and a bunch of structures I must use to run it, so I don't think I can wrap them all in C functions. Does this mean that fitting C++ .dll file into LABVIEW is completely unfeasible?  oh, ......
0 Kudos
Message 6 of 22
(15,823 Views)


@skywalker_hf05 wrote:
But in the "call dll.vi" example in LABVIEW, there seems to be some C++ source code like structures of all kinds. The joystick driver at least has 3 big objects and a bunch of structures I must use to run it, so I don't think I can wrap them all in C functions. Does this mean that fitting C++ .dll file into LABVIEW is completely unfeasible?  oh, ......


Structures are a complete standard C feature. Not sure why you would think them to be C++. Interfaces are C++ and so are templates and I could see someone not familiar with C/C++ to confuse interfaces with structures but they are definitely not the same.

You have basically two issues here. First the objects. The Call Library Node only supports standard C function calls. So what you will have to do is create wrapper functions around all the methods and public variables you may need to access. Something like:

Your C++ object might look like:

interface IEnum : public IUnknown
{
    virtual HRESULT STDMETHODCALLTYPE Next(
        ULONG celt,
        GUID* rgelt,
        ULONG* pceltFetched) = 0;

    virtual HRESULT STDMETHODCALLTYPE Skip(
        ULONG celt) = 0;

    virtual HRESULT STDMETHODCALLTYPE Reset(
        ) = 0;

    virtual HRESULT STDMETHODCALLTYPE Clone(
        IEnumGUID** ppenum) = 0;

};

And this would result in separate C functions for IEnumQueryInterface, IEnumAddRef, IEnumRelease, IEnumNext, IEnumSkip, IEnumReset, IEnumClone and possibly a function to create the object and return an object pointer (though this here is borrowed from a COM API and therefore has no C++ new() mechanisme itself for creating objects but rather uses one of the COM factory methods or gets an object pointer from a method of some other object). Each of above functions would take as first parameter a pointer that points to the object and then the other parameters following and would simply invoke the object method on that pointer.

The second issue are structures. LabVIEW can support typical C structures as function parameter but only if they are flat. That means they can not contain pointers such as arrays and strings. Fixed size arrays (char text[20]) are not pointers but get embedded directly in the structure by C so they can be also dealt with directly in LabVIEW albeit it is not always pretty. If your structures have array or string pointers embedded there is no simple way to deal with this in a LabVIEW diagram at all since LabVIEW uses quite a different type of data structure for strings and arrays than what C usually does. A wrapper function that takes apart the structure into more LabVIEW friendly datatypes is then the best solution. Even if the structure is flat, if it is huge with many different elements in it a wrapper function may also help here.

Is it feasable to incorporate C++ code in LabVIEW?  Definitely! Is it simple? Only if your C++ interface is simple. So it will be the question if the result is worth the effort. Integrating external code into LabVIEW is always quite an effort in comparison to working completely in LabVIEW. LabVIEW allows you to concentrate on the problem at hand while with external code you get into all kinds of difficulties with having to provide the interface infrastructure, frequent modifications of the interface as experience grows, memory management, etc.

Rolf Kalbermatter

Message Edited by rolfk on 01-16-2007 07:49 AM

Message Edited by rolfk on 01-16-2007 07:51 AM

Rolf Kalbermatter
My Blog
Message 7 of 22
(15,813 Views)
Ok, ... let's make things simpler. Since I only need to use one data from LABVIEW as the input of the C++ program, so could I just reserve an address in memory for that data and call it in C++ to realize some force effects, thus running LABVIEW and C++ at the same time, just using that memory address as the bridge of these two interfaces. Could I realize that? How?  Say, output one data from LABVIEW and catch it in C++ instantly and dynamically?  If this can be realized, I can realize the forward location control in LABVIEW and backward force effects in C++.  Thanks...   : )
0 Kudos
Message 8 of 22
(15,777 Views)


@skywalker_hf05 wrote:
Ok, ... let's make things simpler. Since I only need to use one data from LABVIEW as the input of the C++ program, so could I just reserve an address in memory for that data and call it in C++ to realize some force effects, thus running LABVIEW and C++ at the same time, just using that memory address as the bridge of these two interfaces. Could I realize that? How?  Say, output one data from LABVIEW and catch it in C++ instantly and dynamically?  If this can be realized, I can realize the forward location control in LABVIEW and backward force effects in C++.  Thanks...   : )


You make it more complex!!!!

C(++) unlike LabVIEW is not a program you can run alongside. It just can create a program. Of course you can create an executable with C(++) and run that but that will run in its own process space and therefore you can NOT easily share data between LabVIEW and this executable through memory pointers. You could however let LabVIEW start your executable, that would write it's data in a well known file (possible with the name given by LabVIEW as command line parameter) and then after it is finished let LabVIEW read that file. Not that I would consider that easier but YMMV depending on your familiarity with C.

Rolf Kalbermatter

Rolf Kalbermatter
My Blog
0 Kudos
Message 9 of 22
(15,761 Views)

Hi there,

how do i locate command shell execute VI ?

Thank you

0 Kudos
Message 10 of 22
(15,302 Views)