LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Can I call a dll with a C++ Class Cstring variable?

So,
 
we bought an optiphase interferometer, and it comes with a dll and with LabVIEW example code.
However, there is a big problem!
The dll is a C++ dll and requires the C++ Cstring class!
 
for example I can't get the function Boolean LoadDemodulatorDSPCore(Cstring arg1, U8Bit arg2) to work.
 
They did provide a GetDllVersionchar(Cstr arg1, long arg2) which does work, (offcourse, it uses a standard C string pointer)
but GetDllVersion(Cstring arg1) does not work. (offcourse not, it using the Cstring class)
 
Is there a way to get CString classes to work in LabVIEW? (with a struct, or an binary array or whatever?)
Or do I have to write a wrapper dll to convert CString classes to standard C string pointers?
 
Thanks.
0 Kudos
Message 1 of 9
(3,979 Views)


@Nils Gokemeijer wrote:
So,
 
we bought an optiphase interferometer, and it comes with a dll and with LabVIEW example code.
However, there is a big problem!
The dll is a C++ dll and requires the C++ Cstring class!
 
for example I can't get the function Boolean LoadDemodulatorDSPCore(Cstring arg1, U8Bit arg2) to work.
 
They did provide a GetDllVersionchar(Cstr arg1, long arg2) which does work, (offcourse, it uses a standard C string pointer)
but GetDllVersion(Cstring arg1) does not work. (offcourse not, it using the Cstring class)
 
Is there a way to get CString classes to work in LabVIEW? (with a struct, or an binary array or whatever?)
Or do I have to write a wrapper dll to convert CString classes to standard C string pointers?
 
Thanks.


I'm afraid this won't really be possible without a wrapper DLL. A CString is more like an object with internal pointers and last time I checked into a DLL with a disassembler it seemed that the actual layout of the internal structure depends on the size of the string that is contained, with small strings being entirely embedded and larger strings being a pointer to the string. Trying to figure out these difficulties is something you should leave to compiler developers and not try to do yourself.

One way that might be possible is to actually only create three or four C functions that are exported by a helper DLL. These functions would allocate a CString, copy a CStr into it and out of it and one to deallocate it. As far as LabVIEW is concerned you would treat the CString simply as a uInt32.

Rolf Kalbermatter
Rolf Kalbermatter
My Blog
Message 2 of 9
(3,973 Views)
Dear Nils,

sorry to bother you with this, but at the moment I have to deal with the same problem you decribed in your Post, because we also bought the Optiphase Interferometer. Did you by any chance find a way to solve this Problem?

Thanks,

Robert
0 Kudos
Message 3 of 9
(3,817 Views)

Sorry,

No solution yet, we are still using their standard interface application and A/D to read the signal.

-Nils

0 Kudos
Message 4 of 9
(3,809 Views)
Dear Nils,

sorry to bother you again, but have you ever actually tried to write such a wrapper-dll Rolf Kalbermatter suggests in his reply to your question?
In case you did, why didn't it work out?

Thanks,

Robert
0 Kudos
Message 5 of 9
(3,757 Views)

Dear Nils and Robert,

I have the same trouble with the DLL and their system and am trying to write a DLL.

You both seemed to try already and I would like to hear anything helpful.

Have you gotten any progress?

Thanks,

Chang

0 Kudos
Message 6 of 9
(3,519 Views)
Very sorry, but there's abolutely no progress on my side.

Regards,

Robert
0 Kudos
Message 7 of 9
(3,512 Views)
Dear Nils,

LabVIEW provides support for passing and returning the following string data types with the Call Library Function Node:
Configuring the Call Library Function Node
C String Pointer - a string followed by a null character
Pascal String Pointer - a string preceded by a length byte
String Handle - a pointer to a pointer to four bytes for length information, followed by string data
String Handle Pointer - a pointer to an array of string handles

Neither LabVIEW nor LabWindows/CVI can call C++ class methods and properties from a DLL.  To call methods and properties in a C++ DLL, you will need to use C-style wrapper functions that will make calls into the C++ class methods and properties.  You may also be able to use Andy's post, but I havn't tried it.

For more information on Rolf's suggestion to "treat the CString as a uInt32", please see Configuring the Call Library Function Node in the LabVIEW 8.5 Online Help files.  There you can read, "If the function you are calling returns a data type not listed, choose a return data type the same data size as the one returned by the function.  For example, if the function returns a char data type, use an 8-bit unsigned integer.  A call to a function in a DLL cannot return a pointer because there are no pointer types in LabVIEW.  However, you can specify the return type as an integer that is the same size as the pointer.  LabVIEW then treats the address as a simple integer, and you can pass it to future DLL calls."
0 Kudos
Message 8 of 9
(3,485 Views)


@robert_p1 wrote:
Very sorry, but there's abolutely no progress on my side.

Regards,

Robert


Something along these lines should work:

#include <afx.h>
#include "extcode.h"

MgErr CStringToLString(CString s, LStrHandle lstr)
{
    MgErr err = NumericArrayResize(uB, 1, (UHandle*)&lstr, s.GetLength());
    if (!err)
    {
       MoveBlock(s.LockBuffer(), LStrBuf(*lstr), s.GetLength());
       s.UnlockBuffer();
       LStrLen(*lstr) = s.GetLength();

    }
    return err;
}

MgErr LStringToCString(LStrHandle lstr, CString *ps)
{
    *ps = CString((char*)LStrBuf(*lstr), LStrLen(*lstr));
    if (*ps)
      return mFullErr;
    return noErr;
}

MgErr DeleteCString(CString s)
{
    if (!s)
       return mgArgErr;

    s.~CString();
    return noErr;
}

Hande with care! My C++ knowledge is rather limited.

As far as LabVIEW's Call Library Node is concerned, the LStrHandle parameters are simply LabVIEW String handles (not LabVIEW String Handle Pointers) and the CString parameters should be handled as uInt32 passed by value and the CString * as uInt32 passed by reference.

Rolf Kalbermatter


Message Edited by rolfk on 06-03-2008 10:35 PM
Rolf Kalbermatter
My Blog
0 Kudos
Message 9 of 9
(3,474 Views)