Re: Net/Link: Problem with DLL (1)
- To: mathgroup at smc.vnet.net
- Subject: [mg48871] Re: [mg48855] Net/Link: Problem with DLL (1)
- From: Todd Gayley <tgayley at wolfram.com>
- Date: Mon, 21 Jun 2004 03:49:01 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
At 03:31 AM 6/19/2004, psa wrote:
>My C type DLL (written in Compaq Visual Fortran) returns a length 2 double
>array, and I cannot get all two of the results.
>I am confused as to when and how to supply an object in .Net/Link. I tried:
>
> In[] =
>TestDLL2=DefineDLLFunction["testDLL2","maths0000.dll","Void",{"Double","Doub
>le","Double","Double","out Double[]"}];
>
>With what I believe is the required and it fails.
>
>In[] = res=MakeNETObject[{0.0,0.0}]
> «NETObject[System.Double[]]»
>In[] = TestDLL2 [1,2,3,4,res]
> NET::netexcptn: A .NET exception occurred: System.NullReferenceException:
>Object reference not set to an instance of an
>obj.espace.DLLWrapper8.testDLL2(Double , Double , Double , Double ,
>Double[]& ).
> $Failed
>
>Ditto, if it is it is passed res={0.0,0.0}.
>
>By pretending it is just a scalar for output array I get the first element
>only (not surprisingly?)
>In =
>TestDLL2=DefineDLLFunction["testDLL2","maths0000.dll","Void",{"Double","Doub
>le","Double","Double","out Double"}];
>In[] = TestDLL2 [1,2,3,4,res]
>In[] = res
> -0.0229672
>
>If I am NOT doing something wrong, Is there a Windows API that returns an
>array so I can see it working?
Peter,
This can be tricky, and you are close, but the Mathematica declaration of
your function is not correct. I presume that the C declaration looks like this:
void testDLL2(double, double, double, double, double*);
The last parameter is an array that is filled in by the function call. You
are declaring this as an "out Double[]" in Mathematica, but "out Double[]"
is effectively a double**, not a double* (remember that adding "out" or
"ref" to an argument is like adding an extra level of indirection). The
correct declaration is:
TestDLL2 = DefineDLLFunction["testDLL2", "maths0000.dll", "Void",
{"Double","Double","Double","Double","out Double"}];
You can also use "Double[]" as the type, and that might be clearer:
TestDLL2 = DefineDLLFunction["testDLL2", "maths0000.dll", "Void",
{"Double","Double","Double","Double","Double[]"}];
You are calling it correctly, using an array object reference so that you
have a handle to the modified array contents after the call:
res = MakeNETObject[{0.0, 0.0}]
TestDLL2 [1, 2, 3, 4, res]
Then NETObjectToExpression[res] will give you the result as a list.
Your function is similar to the ReverseArray example in the section on
array arguments to DLLs in the .NET/Link User Guide.
--Todd