home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_Arrays_vb________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2001-08-27  |  6.9 KB  |  220 lines

  1. ' Copyright
  2. ' Microsoft Corporation
  3. ' All rights reserved
  4.  
  5. ' Arrays.vb
  6.  
  7. Imports System
  8. Imports Microsoft.VisualBasic
  9. Imports System.Runtime.InteropServices
  10.  
  11. 'typedef struct _MYPOINT
  12. '{
  13. '    int x; 
  14. '    int y; 
  15. '} MYPOINT;
  16.  
  17. < StructLayout( LayoutKind.Sequential )> _
  18. Public Structure MyPoint
  19.     Public x As Integer
  20.     Public y As Integer
  21.     
  22.     Public Sub New( x As Integer, y As Integer )
  23.         Me.x = x
  24.         Me.y = y
  25.     End Sub 'New
  26. End Structure 'MyPoint
  27.  
  28. 'typedef struct _MYPERSON
  29. '{
  30. '    char* first; 
  31. '    char* last; 
  32. '} MYPERSON;
  33.  
  34. < StructLayout( LayoutKind.Sequential, CharSet:=CharSet.Ansi )> _
  35. Public Structure MyPerson
  36.     Public first As String
  37.     Public last As String
  38.     
  39.     Public Sub New( first As String, last As String )
  40.         Me.first = first
  41.         Me.last = last
  42.     End Sub 'New
  43. End Structure 'MyPerson
  44.  
  45.  
  46. Public Class LibWrap
  47.    
  48.     ' this way array size can't be changed and array can be copied back
  49.     'int TestArrayOfInts(int* pArray, int pSize)
  50.         
  51.     Declare Function TestArrayOfInts Lib "..\LIB\PinvokeLib.dll" ( _
  52.         <[In], Out> ByVal myArray() As Integer, ByVal size As Integer ) As Integer
  53.    
  54.     'this way we could change array size, but array can't be copied back
  55.     'since marshaler doesn't know resulting size, we must do this manually
  56.     'int TestRefArrayOfInts(int** ppArray, int* pSize)
  57.        
  58.     Declare Function TestRefArrayOfInts Lib "..\LIB\PinvokeLib.dll" ( _
  59.         ByRef myArray As IntPtr, ByRef size As Integer ) As Integer
  60.    
  61.     'int TestMatrixOfInts(int pMatrix[][COL_DIM], int row)
  62.     
  63.     Declare Function TestMatrixOfInts Lib "..\LIB\PinvokeLib.dll" ( _
  64.         <[In], Out> ByVal matrix(,) As Integer, ByVal row As Integer ) As Integer
  65.    
  66.     'int TestArrayOfStrings(char** ppStrArray, int size)
  67.     
  68.     Declare Function TestArrayOfStrings Lib "..\LIB\PinvokeLib.dll" ( _
  69.         <[In], Out> ByVal strArray() As String, ByVal size As Integer ) As Integer
  70.     
  71.     'int TestArrayOfStructs(MYPOINT* pPointArray, int size)
  72.     
  73.     Declare Function TestArrayOfStructs Lib "..\LIB\PinvokeLib.dll" ( _
  74.         <[In], Out> ByVal pointArray() As MyPoint, ByVal size As Integer ) As Integer
  75.     
  76.     'without [In, Out] strings will not be copied out
  77.     'int TestArrayOfStructs2(MYPERSON* pPersonArray, int size)
  78.     
  79.     Declare Function TestArrayOfStructs2 Lib "..\LIB\PinvokeLib.dll" ( _
  80.         <[In], Out> ByVal personArray() As MyPerson, ByVal size As Integer ) As Integer
  81.         
  82. End Class 'LibWrap
  83.  
  84.  
  85. Public Class App
  86.     Public Shared Sub Main()
  87.     
  88.         ' *************** array ByVal ********************************
  89.         Dim array1(9) As Integer
  90.         
  91.         Console.WriteLine( "Integer array passed ByVal before call:" )
  92.         Dim i As Integer
  93.         
  94.         For i = 0 To array1.Length - 1
  95.             array1(i) = i
  96.             Console.Write( " " & array1(i) )
  97.         Next i
  98.         
  99.         Dim sum1 As Integer = LibWrap.TestArrayOfInts( array1, array1.Length )
  100.         Console.WriteLine( ControlChars.CrLf & "Sum of elements:" & sum1 )
  101.         
  102.         Console.WriteLine( ControlChars.CrLf & "Integer array passed ByVal after call:" )
  103.         
  104.         For Each i In  array1
  105.             Console.Write( " " & i )
  106.         Next i
  107.       
  108.         ' *************** array ByRef ********************************
  109.         Dim array2(9) As Integer
  110.         Dim arraySize As Integer = array2.Length
  111.         
  112.         Console.WriteLine( ControlChars.CrLf & ControlChars.CrLf & _
  113.             "Integer array passed ByRef before call:" )
  114.             
  115.         For i = 0 To array2.Length - 1
  116.             array2(i) = i
  117.             Console.Write( " " & array2(i) )
  118.         Next i
  119.         
  120.         Dim buffer As IntPtr = Marshal.AllocCoTaskMem( Marshal.SizeOf( arraySize ) * array2.Length )
  121.         Marshal.Copy( array2, 0, buffer, array2.Length )
  122.         
  123.         Dim sum2 As Integer = LibWrap.TestRefArrayOfInts( buffer, arraySize )
  124.         Console.WriteLine( ControlChars.CrLf & "Sum of elements:" & sum2 )
  125.         
  126.         If arraySize > 0 Then
  127.             Dim arrayRes( arraySize - 1 ) As Integer
  128.             Marshal.Copy( buffer, arrayRes, 0, arraySize )
  129.             Marshal.FreeCoTaskMem( buffer )
  130.             
  131.             Console.WriteLine( ControlChars.CrLf & "Integer array passed ByRef after call:" )
  132.             For Each i In  arrayRes
  133.                 Console.Write( " " & i )
  134.             Next i
  135.         Else
  136.             Console.WriteLine( ControlChars.CrLf & "Array after call is empty" )
  137.         End If
  138.          
  139.         ' *************** matrix ByVal ********************************
  140.         Const [DIM] As Integer = 4
  141.         Dim matrix([DIM], [DIM]) As Integer
  142.         
  143.         Console.WriteLine( ControlChars.CrLf & ControlChars.CrLf & "Matrix before call:" )
  144.         For i = 0 To [DIM]
  145.             Dim j As Integer
  146.             For j = 0 To [DIM]
  147.                 matrix(i, j) = j
  148.                 Console.Write( " " & matrix(i, j) )
  149.             Next j
  150.             Console.WriteLine( "" )
  151.         Next i
  152.       
  153.         Dim sum3 As Integer = LibWrap.TestMatrixOfInts( matrix, [DIM] + 1 )
  154.         Console.WriteLine( ControlChars.CrLf & "Sum of elements:" & sum3 )
  155.         
  156.         Console.WriteLine( ControlChars.CrLf & "Matrix after call:" )
  157.         For i = 0 To [DIM]
  158.             Dim j As Integer
  159.             For j = 0 To [DIM]
  160.                 Console.Write( " " & matrix(i, j) )
  161.             Next j
  162.             Console.WriteLine( "" )
  163.         Next i
  164.       
  165.         ' *************** string array ByVal ********************************
  166.         Dim strArray As String() =  { "one", "two", "three", "four", "five" }
  167.         
  168.         Console.WriteLine( ControlChars.CrLf & ControlChars.CrLf & "String array before call:" )
  169.         Dim s As String
  170.         For Each s In  strArray
  171.             Console.Write( " " & s )
  172.         Next s 
  173.         
  174.         Dim lenSum As Integer = LibWrap.TestArrayOfStrings( strArray, strArray.Length )
  175.         Console.WriteLine( ControlChars.CrLf & "Sum of string lengths:" & lenSum )
  176.         
  177.         Console.WriteLine( ControlChars.CrLf & "String array after call:" )
  178.         For Each s In  strArray
  179.             Console.Write( " " & s )
  180.         Next s
  181.       
  182.         ' *************** struct array ByVal ********************************
  183.         Dim points As MyPoint() = { New MyPoint(1, 1), New MyPoint(2, 2), New MyPoint(3, 3) }
  184.         
  185.         Console.WriteLine( ControlChars.CrLf & ControlChars.CrLf & "Points array before call:" )
  186.         Dim p As MyPoint
  187.         For Each p In  points
  188.             Console.WriteLine( "x = {0}, y = {1}", p.x, p.y )
  189.         Next p 
  190.         
  191.         Dim allSum As Integer = LibWrap.TestArrayOfStructs( points, points.Length )
  192.         Console.WriteLine( ControlChars.CrLf & "Sum of points:" & allSum )
  193.         
  194.         Console.WriteLine( ControlChars.CrLf & "Points array after call:" )
  195.         For Each p In  points
  196.             Console.WriteLine( "x = {0}, y = {1}", p.x, p.y )
  197.         Next p 
  198.         
  199.       ' *************** struct with strings array ByVal ********************************
  200.         Dim persons As MyPerson() =  { New MyPerson( "Kim", "Akers" ), _
  201.                                         New MyPerson( "Adam", "Barr" ), _
  202.                                         New MyPerson( "Jo", "Brown" )}
  203.         
  204.         Console.WriteLine( ControlChars.CrLf & ControlChars.CrLf & "Persons array before call:" )
  205.         Dim pe As MyPerson
  206.         For Each pe In  persons
  207.             Console.WriteLine( "first = {0}, last = {1}", pe.first, pe.last )
  208.         Next pe 
  209.         
  210.         Dim namesSum As Integer = LibWrap.TestArrayOfStructs2( persons, persons.Length )
  211.         Console.WriteLine( ControlChars.CrLf & "Sum of name lengths:" & namesSum )
  212.         
  213.         Console.WriteLine( ControlChars.CrLf & ControlChars.CrLf & "Persons array after call:" )
  214.         For Each pe In  persons
  215.             Console.WriteLine( "first = {0}, last = {1}", pe.first, pe.last )
  216.         Next pe
  217.         
  218.     End Sub 'Main
  219. End Class 'App 
  220.