By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,107 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,107 IT Pros & Developers. It's quick & easy.

dynamic array as a byref parameter

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"xinsir" <ze*********@newtouch.com.cn> schrieb
dynamic array as a byref parameter by used in function and
have a Marshal error ,what is the matter?thanks

source like as this .
------------------------------------------------------
[source]


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

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2

P: n/a
In article <2e****************************@phx.gbl>, xinsir wrote:
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 :)

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.