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

  1. // Copyright
  2. // Microsoft Corporation
  3. // All rights reserved
  4.  
  5. // Structs.cs
  6.  
  7. using System;
  8. using System.Runtime.InteropServices;
  9.  
  10. /*
  11. typedef struct _MYPERSON
  12. {
  13.     char* first; 
  14.     char* last; 
  15. } MYPERSON, *LP_MYPERSON;
  16. */
  17.  
  18. [ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
  19. public struct MyPerson 
  20. {
  21.     public String first; 
  22.     public String last;
  23. }
  24.  
  25. /*
  26. typedef struct _MYPERSON2
  27. {
  28.     MYPERSON* person;
  29.     int age; 
  30. } MYPERSON2, *LP_MYPERSON2;
  31. */
  32.  
  33. [ StructLayout( LayoutKind.Sequential )]
  34. public struct MyPerson2 
  35. {
  36.     public IntPtr person;
  37.     public int age;
  38. }
  39.  
  40. /*
  41. typedef struct _MYPERSON3
  42. {
  43.     MYPERSON person;
  44.     int age; 
  45. } MYPERSON3;
  46. */
  47.  
  48. [ StructLayout( LayoutKind.Sequential )]
  49. public struct MyPerson3 
  50. {
  51.     public MyPerson person;
  52.     public int age;
  53. }
  54.  
  55. /*
  56. typedef struct _MYARRAYSTRUCT
  57. {
  58.     bool flag;
  59.     int vals[ 3 ]; 
  60. } MYARRAYSTRUCT;
  61. */
  62.  
  63. [ StructLayout( LayoutKind.Sequential )]
  64. public struct MyArrayStruct 
  65. {
  66.     public bool flag;
  67.     [ MarshalAs( UnmanagedType.ByValArray, SizeConst=3 )] 
  68.     public int[] vals;
  69. }
  70.  
  71. public class LibWrap
  72. {
  73.     // int TestStructInStruct(MYPERSON2* pPerson2);
  74.     
  75.     [ DllImport( "..\\LIB\\PinvokeLib.dll" )]
  76.     public static extern int TestStructInStruct( ref MyPerson2 person2 );
  77.     
  78.     // void TestStructInStruct3(MYPERSON3 person3)
  79.     
  80.     [ DllImport( "..\\LIB\\PinvokeLib.dll" )]
  81.     public static extern int TestStructInStruct3( MyPerson3 person3 );    
  82.     
  83.     // void TestArrayInStruct( MYARRAYSTRUCT* pStruct );
  84.     
  85.     [ DllImport( "..\\LIB\\PinvokeLib.dll" )]
  86.     public static extern int TestArrayInStruct( ref MyArrayStruct myStruct );    
  87. }
  88.  
  89. public class App
  90. {
  91.     public static void Main()
  92.     {
  93.         // ******************* structure with pointer to other structure ************
  94.         MyPerson personName;
  95.         personName.first = "Mark";
  96.         personName.last = "Lee";
  97.         
  98.         MyPerson2 personAll;
  99.         personAll.age = 30;
  100.         
  101.         IntPtr buffer = Marshal.AllocCoTaskMem( Marshal.SizeOf( personName ));
  102.         Marshal.StructureToPtr( personName, buffer, false );
  103.         
  104.         personAll.person = buffer;
  105.         
  106.         Console.WriteLine( "\nPerson before call:" );
  107.         Console.WriteLine( "first = {0}, last = {1}, age = {2}", 
  108.             personName.first, personName.last, personAll.age ); 
  109.         
  110.         int res = LibWrap.TestStructInStruct( ref personAll );
  111.         
  112.         MyPerson personRes = 
  113.             (MyPerson)Marshal.PtrToStructure( personAll.person, typeof( MyPerson ));
  114.         
  115.         Marshal.FreeCoTaskMem( buffer );
  116.         
  117.         Console.WriteLine( "Person after call:" );
  118.         Console.WriteLine( "first = {0}, last = {1}, age = {2}", 
  119.             personRes.first, personRes.last, personAll.age );
  120.         
  121.         // ******************* structure with embedded structure ************    
  122.         MyPerson3 person3 = new MyPerson3();
  123.         person3.person.first = "John";
  124.         person3.person.last = "Evans";
  125.         person3.age = 27;
  126.         
  127.         LibWrap.TestStructInStruct3( person3 );
  128.         
  129.         // ******************* structure with embedded array ************    
  130.         MyArrayStruct myStruct = new MyArrayStruct();
  131.         
  132.         myStruct.flag = false;
  133.         myStruct.vals = new int[ 3 ];
  134.         myStruct.vals[ 0 ] = 1;
  135.         myStruct.vals[ 1 ] = 4;
  136.         myStruct.vals[ 2 ] = 9;
  137.         
  138.         Console.WriteLine( "\nStructure with array before call:" );
  139.         Console.WriteLine( myStruct.flag );
  140.         Console.WriteLine( "{0} {1} {2}", myStruct.vals[ 0 ], 
  141.             myStruct.vals[ 1 ], myStruct.vals[ 2 ] );
  142.         
  143.         LibWrap.TestArrayInStruct( ref myStruct );
  144.         
  145.         Console.WriteLine( "\nStructure with array after call:" );
  146.         Console.WriteLine( myStruct.flag );
  147.         Console.WriteLine( "{0} {1} {2}", myStruct.vals[ 0 ], 
  148.             myStruct.vals[ 1 ], myStruct.vals[ 2 ] );        
  149.     }
  150. }
  151.  
  152.  
  153.