dynamic array as a byref parameter

X

xinsir

dynamic array as a byref parameter by used in function and
have a Marshal error ,what is the matter?thanks

source like as this .
------------------------------------------------------
Declare Function finit Lib "DllCap.dll" _
(ByRef rdAcnt As ACNTINF2) As Integer

<StructLayout(LayoutKind.Sequential)> Structure ACNTINF
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=16)> Dim
subid As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=40)> Dim
subname As String
End Structure

<StructLayout(LayoutKind.Sequential)> Structure ACNTINF2
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=10)> Dim
acntnum As String
<MarshalAs(UnmanagedType.LPStruct)> Dim actInfo() As
ACNTCNT
End Structure

Private Sub FORM1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim rdAcnt2 As ACNTINF2
Dim iRtn As Integer
iRtn = finit(rdAcnt2)
End Sub
 
A

Armin Zingler

xinsir said:
dynamic array as a byref parameter by used in function and
have a Marshal error ,what is the matter?thanks

source like as this .

If you won't get an answer here, there is also
microsoft.public.dotnet.framework.interop
 
T

Tom Shelton

dynamic array as a byref parameter by used in function and
have a Marshal error ,what is the matter?thanks

source like as this .
------------------------------------------------------
Declare Function finit Lib "DllCap.dll" _
(ByRef rdAcnt As ACNTINF2) As Integer

<StructLayout(LayoutKind.Sequential)> Structure ACNTINF
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=16)> Dim
subid As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=40)> Dim
subname As String
End Structure

<StructLayout(LayoutKind.Sequential)> Structure ACNTINF2
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=10)> Dim
acntnum As String
<MarshalAs(UnmanagedType.LPStruct)> Dim actInfo() As
ACNTCNT
End Structure

Private Sub FORM1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim rdAcnt2 As ACNTINF2
Dim iRtn As Integer
iRtn = finit(rdAcnt2)
End Sub

You didn't specify the error, but I your main problem is that your
trying to pass an array of structs inside of a structure. The current
marshaler dose not support that. You will want to change the ACNTINF2
structure to look like this:

<StructLayout(LayoutKind.Sequential)> _
Structure ACNTINF2
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=10)> _
Dim acntnum As String
Dim actInfo As IntPtr
End Structure

And then use the properties of the Marshal class to actually access the
array. The methods you would most likely be needing are Marshal.SizeOf
and Marshal.PtrToStructure.

This is air code... But the process would look something like:

dim st as acntinf
dim ptr as intptr = acnt2.actInfo

for i as integer = 0 to numberofelements
' get the current structure
st = ctype(marshal.ptrtostructure(ptr, GetType(ACNTINF)), ACNTINF)

' process st

' increment the pointer to the next value
ptr = new IntPtr(ptr.ToInt32() + Marshal.SizeOf(GetType(ACNTINF)))
next i

You may have to do things differently, since I'm not sure how you tell
with this particular function the size of the array. I could be more
specific if I knew more about the actual call :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top