01-29-2015 06:37 AM
Can anybody point me to a resource relating to writing a DLL in LabVIEW for use in Excel?
I have written a simple LabVIEW vi which takes 2 'Doubles' A and B and multiplies them together and displays the result in Product (also a double).
In the application builder I have selected DLL and included the vi. In 'Define VI Prototype' I have passed A and B by value and the return value has been linked to 'Product' which is passed back by value (no choice as it is greyed out). The resulting Function prototype is :-
double Product(double A, double B)
In excel I define its use as
Declare PtrSafe Function Product Lib "C:\Database DLL\KensFirst.dll" (ByVal A As Double, ByVal B As Double) As Double
And the short test subroutine shows that it works
Sub test()
A = 311
B = 28
Debug.Print Product(A, B)
End Sub
So Far So Good!!
Now I want to do a similar thing with strings.
Inputs are 'Given' and 'Family' (both strings) which I concatenate (with a space) to another string called 'Full'
When I go to Defive VI Prototype I get a different set of behaviors
First - the return value is defaulted to (none)
Second - Ful and a parameter called len has appeared in the parametes box
Third - Can now only pass by pointer be it String Handle, C String or Pascal String
The (default) function prototype is
void StringTest(char Given[], char Family[], char Full[], int32_t len)
How can I use this in excel to declare my function and return 'Full'
I have tried several variants of teh declare statment with results ranging from Bad DLL calling convention complie error to crashing Excel!!
Ideally I would like to use it as follows
Full = Product(Given,Family)
Any help would be greatly appreciated
Ken
01-30-2015 12:39 PM
I haven't tried this, and don't have time to right now, but returning a string doesn't appear to be simple:
"... some types are more easily returned by modifying arguments in place: strings, arrays, ..." (from https://msdn.microsoft.com/en-us/library/office/bb687915%28v=office.15%29.aspx) Since you're trying to return a new string, you can't operate on it in place, and this means you need some way of clarifying which code (Excel or LabVIEW) should allocate and release the memory for the new string, which gets complicated. Also, you can't easily return a string (even in a pure C environment returning a string from a function isn't a good idea).
The easiest option is to pass in an additional parameter, a string long enough to hold the concatenated result. Anything else will be complicated.
02-01-2015 08:17 PM