PROBLEM PASSING ARRAY OF STRUCTURE TO VC++ DLL


P

Phoenix

Hi Friends ,

Could anyone please help me to resolve the following issue :

I pass an array of structures to a dll written in VC++ 6.0 whih fills
it with data . The following works well for VB 6.0 but when I wrote
the same piece of code in VB 2005
the array of structures remained empty even after the API call :

I have a structure as follows :

Structure tree_struct
Dim level As Short
Dim branch_id As String
Dim s_desc As String
Dim parent_id As String
Dim access As Short
Dim flag As Short
Dim branch_desc As String
End Structure

My declaration statement looks like following :

Declare Function fetch_tree_hierarchy Lib "mydll.dll" (ByVal
tree_handle As Short, ByVal app_id As String, ByRef l_tree_struct() As
tree_struct) As Short

Following is a sub written in VB 2005 which calls the above function
"fetch_tree_hierarchy " present in a dll "mydll.dll" written in vc++
6.0 :



Dim i As Integer = 0

Dim l_tree_struct() As tree_struct
ReDim l_tree_struct(100)

retcode = vb_get_menu_hierarchy(tree_handle, "app_id",
l_tree_struct)

If retcode = SUCESS Then

Console.WriteLine("Get Tree Hierarchy Succeeded")

Else

GetErrorText(retcode) 'Is a function which maps error
codes to error text
Call terminate_all()
Exit Sub

End If

In above case the Array of structure is coming empty.

Some Progress , Still No Results

During my exploration of various possibilities I went through an
example given at Microsoft website and did the following changes to
my above code :


<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> Public
Structure tree_struct
Dim level As Short
Dim branch_id As String
Dim s_desc As String
Dim parent_id As String
Dim access As Short
Dim flag As Short
Dim branch_desc As String
End Structure


Public Class LibWrap
Declare Function fetch_tree_hierarchy Lib
"tcvbodss.dll" (ByVal mnu_handle As Short, ByVal apl_id As String,
ByRef outArray As IntPtr) As Short
End Class



Dim outArray As IntPtr
LibWrap.vb_get_menu_hierarchy(tree_handle, "app_id",
l_tree_struct)
Dim manArray(100) As vb_menu
Dim current As IntPtr = outArray
Dim i As Integer

For i = 0 To 100 - 1

manArray(i) = New tree_struct

Marshal.PtrToStructure(current, manArray(i))

Marshal.DestroyStructure(current, GetType(tree_struct))

current = IntPtr.op_Explicit(current.ToInt64() +
Marshal.SizeOf(manArray(i)))

Console.WriteLine("Element {0}: {1} {2}", i,
manArray(i).branch_id, manArray(i).branch_desc)

Next i

Marshal.FreeCoTaskMem(outArray)

At line : " Marshal.PtrToStructure(current, manArray(i))" I am getting
following exception :

Message : Value cannot be null.
Parameter name: ptr

My mistake may be quite silly as I am new to VB 2005 , kindly help me
to resolve this problem.

Sincerely,

Sudhansu
 
Ad

Advertisements

M

Mattias Sjögren

I pass an array of structures to a dll written in VC++ 6.0 whih fills
it with data . The following works well for VB 6.0 but when I wrote
the same piece of code in VB 2005
the array of structures remained empty even after the API call :


Can you post the working VB6 code so we can compare?


Mattias
 
Ad

Advertisements

P

Phoenix

Can you post the working VB6 code so we can compare?

Mattias
Hi Mattias ,

Thanks a lot for the reply , following is the working VB 6 code for
your reference :

Type tree_struct
Dim level As Integer
Dim branch_id As String
Dim s_desc As String
Dim parent_id As String
Dim access As Integer
Dim flag As Integer
Dim branch_desc As String
End Type

My declaration statement looks like following :

Declare Function fetch_tree_hierarchy Lib "mydll.dll" (ByVal
tree_handle As Integer, ByVal app_id As String, ByRef l_tree_struct()
As tree_struct) As Integer


Dim retcode As Integer

Dim l_tree_struct(100) As tree_struct

retcode = fetch_tree_hierarchy(tree_handle, "app_id",
l_tree_struct)

Sincerely,

Sudhansu
 

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