LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Introduct C++ in Labview 7.1 with DLL

I have a DLL from Agilent technologies and I want to program a laser with this DLL and Labview. The broblem is that there are some type def that I can't use in labview that are defined in Windows, for example, "typedef void *HANDLE". I would like to know if I can wire this type of variable in Labview from one function to another and how.
Because the functions in the DLL do not return the kind of variables I expect, I would like to create some other functione that return the good ones. I know that I can use strings in Labview but when I use the call libraries function, there's no C++ compiler, there's only C and there are no strings in C like in C++ so I don't know if I cas really use strings in my C++ program. Would it work?


Thank you!
0 Kudos
Message 1 of 8
(3,529 Views)
I know that I can use strings in Labview but when I use the call libraries function, there's no C++ compiler
I have no idea what this means.

there are no strings in C like in C++
I don't know what you mean, exactly. A char pointer is a char pointer is a char pointer. C or C++.
If you mean the string objects that Visual C provides, for example, those are not available in LabVIEW.

I have successfully used LabVIEW strings in a C++ DLL, connected via the CALL LIBRARY function. It's not difficult.

You do have to convert them, however. You can't just use them as is, with standard C strings (they're not null-terminated).

If you need more specific details, just ask more specific questions.
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

Message 2 of 8
(3,529 Views)
"I know that I can use strings in Labview but when I use the call libraries function, there's no C++ compiler"

I meant that in the calling conventions there is C but no C++ and I wonder if it matters...

"there are no strings in C like in C++"

Here I meant tant I already have functions in a Dll which parameters are chars (and char tables "char[]") and I don't think I can use the table of char in labview so I asked, if I can convert a char table in a string, if il would work in labview even if the calling convention is in C.

Thank you for the answer 🙂
0 Kudos
Message 3 of 8
(3,529 Views)
I don't know of any differences in calling convention between C and C++ - it' doesn't matter.

If your DLL expects a char pointer (or char[ ] = same thing), then there's no DIRECT way to pass a LabVIEW string to it and have it make sense.

If you can change the DLL code to use LabVIEW strings, that would be my first option.

If not, consider writing a "glue" DLL which accepts LabVIEW strings, converts them to what your DLL wants, and calls it.

< UGLY HACK WARNING >

Here's an EXTREMELY ROUNDABOUT, INEFFICIENT, and CONFUSING way to pass a LabVIEW string to a DLL that wants a char pointer:

1... The VI that calls the Library should have a STRING control (which is the string to pass to the DLL).

2... Use STRING TO BYTE AR
RAY (in the STRINGS palette) to convert the string to an array of U8s.

3... Use ARRAY TO CLUSTER (in the ARRAY palette) to convert the array to a cluster). Pop up on the function and SET CLUSTER SIZE to some reasonable maximum, say 100.

4... Wire the cluster from there to the CALL LIBRARY node. Set the PARAMETER TYPE to ADAPT TO TYPE, so it will accept anything. The DATA FORMAT doesn't matter, you're not passing handles.

This will work, because the cluster will be passed as a pointer to the 0th data item, and each item is a U8. The terminating zero will be put there for you, as long as you don't exceed the size you set the ARRAY TO CLUSTER function for.


< /UGLY HACK >
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 4 of 8
(3,529 Views)
Hi,

Thank you for this answer 🙂

I already began the "glue DLL", it seems to be the best solution because I cannot change the company's DLL. It just take the string of Labview, converts it in a char pointer, then passes it in the DLL I have from the company.

I program this DLL with visual C++ so I think it will work correctly.

Thank you very much for your help!
0 Kudos
Message 5 of 8
(3,529 Views)
I want to RETRACT the comments (UGLY HACK, INEFFICIENT) I made about a suggestion I offered.
As it happens, I am using a DLL that requires a pointer to data, along with a length word. (It reads / writes data from / to the shared memory on an RT board).

I now have several routines to write different kinds of structures to the memory, or read them. I don't like having 10 different WRITE vis, and 10 different READ ones.

I had previously investigated flattening everything to a string, converting the string to an array of I32s and writing them one at a time.

Your question, and my suggestion got me thinking in that direction again.

So I did some timing:
a FLATTEN TO STRING, STRING to BYTE ARRAY, and ARRAY to CLUST
ER chain takes 0.82 uSec (P4 @ 1500 MHz, 60-byte cluster) to execute. That's not as shabby as I expected.

So I will rewrite my code to use this. The UGLY is not too bad, the INEFFICIENT is not too bad. The CONFUSING I will leave to the observer.

In your case, it might beat carrying around another DLL.

The downside is you have to establish a maximum string size, I don't know if that's a problem for your app...
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 6 of 8
(3,529 Views)
Hi,

No, it's not a problem (the string size), I know it will never be longer than 32 characters. My char* which will convert the string will be compared with a char[32]. I'll juste have to check if my string is not longer than 32 but that's not a problem.

Thanks
0 Kudos
Message 7 of 8
(3,529 Views)
Poly Meca wrote:
> "I know that I can use strings in Labview but when I use the call
> libraries function, there's no C++ compiler"
>
> I meant that in the calling conventions there is C but no C++ and I
> wonder if it matters...

C calling convention only specifies the order of function parameters
on the stack and if the caller or the called function cleans up the stack.

stdcall (Winapi) is the other calling convention and there is certainly
no stdcall compiler or language 🙂

Basically you define the calling convention of functions either as
command line switch to the C compiler to use as default or specifically
by adding __stdcall or __cdecl as a specifier to the function declaration.

> "there are no strings in C like in C++"
>
> Here I
meant tant I already have functions in a Dll which parameters
> are chars (and char tables "char[]") and I don't think I can use the
> table of char in labview so I asked, if I can convert a char table in
> a string, if il would work in labview even if the calling convention
> is in C.

char[] and char * is physically (although not necessarily syntactically)
the same and just a simple C string pointer. If you mean char *[] or
something like that then yes LabVIEW can't create that directly.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 8 of 8
(3,529 Views)