call to an unmanaged dll.
Existing code:
//External function declaration
Private Declare Function Uncompress& Lib "COMPR.DLL" (ByVal SrcSt$,
ByVal SrcLen&, Dest As Any, ByVal DestLen&)
//Invocation of external function
lReturn = Uncompress(sCom pressedResult, Len(sCompressed Result),
aResults(ARRAY_ START), ByVal lNewLength)
[where aResults is a double array. The COMPR.Uncompres s function
basically uncompress a string 'sCompressedRes ult' and fills the array
aResults. The array is initialized before calling the external
function]
My understanding is that( Please correct me if I am wrong):
a. The third parameter in declaration 'Dest as Any' means a parameter
of variant type.
b. At invocation, the first element of the double array is passed.
i.e., a pointer to the first element in the double array is passed to
the external function.
c. The Uncompress function expands the compressed data, fills/replaces
the double array with values.
d. The changed values are available to the caller after the Uncompress
function has returned.
Now, I have to call the COMPR.Uncompres s method from Vb.Net.
From browsing the net, I got a lot of information regarding this(
Please correct me if I am wrong):
a. We have to mark the parameter as InAttribute(), Out().
b. We should pin the object so that GC does not change address.
c. We should pass a pointer TO THE FIRST OBJECT OF THE ARRAY to the
native function.
d. Array of type double is non-blittable and so we have to do the
marshaling.
This is the code I wrote:
//External method declaration
<DllImport("COM PR.dll", EntryPoint:="Un Compress",
SetLastError:=T rue, CharSet:=CharSe t.Auto, ExactSpelling:= True,
CallingConventi on:=CallingConv ention.StdCall) > _
Public Shared Function UnCompress(ByVa l compressedResul t As String,
ByVal length As Long, <InAttribute( ), Out(),
MarshalAs(Unman agedType.Struct )> ByRef destinationArra yElement As
Object, ByVal newLength As Long) As Long
End Function
//Invocation
'Pin the object
Dim resultsHandle As GCHandle
resultsHandle = GCHandle.Alloc( aResults(ARRAY_ START),
GCHandleType.Pi nned)
Dim resultsPointer As IntPtr = resultsHandle.A ddrOfPinnedObje ct()
'Invoke
returnValue = UnCompress(sCom pressedResult, Len(sCompressed Result),
resultsPointer, newLength)
My problem is that I get the return value correctly, but the array does
not get changed....I get the same data that is passed to the external
function. I think the changes made by the native function is not
persisting.
There is no problem with any of the parameters - I have checked the
same parameters using the existing vb dll and it works. I would like to
know why I dont get the modified array even though I am passing a
pointer to the first element of the array and am pinning the element.
Is this the correct way to marshal (UnmanagedType. struct) and is this
the correct way to pin object?
Any help would be appreciated,
Jim