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

  1. // Copyright
  2. // Microsoft Corporation
  3. // All rights reserved
  4.  
  5. // OutArrayOfStructs.cs
  6.  
  7. using System;
  8. using System.Runtime.InteropServices;
  9.  
  10. /*
  11. typedef struct _MYSTRSTRUCT2
  12. {
  13.     char* buffer;
  14.     UINT size; 
  15. } MYSTRSTRUCT2;
  16. */
  17. [ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
  18. public class MyStruct 
  19. {
  20.     public String buffer;
  21.     public int size;
  22. }
  23.  
  24. [ StructLayout( LayoutKind.Sequential )]
  25. public struct MyUnsafeStruct 
  26. {
  27.     public IntPtr buffer;
  28.     public int size;
  29. }
  30.  
  31. public unsafe class LibWrap
  32. {
  33.     // void TestOutArrayOfStructs(int* pSize, MYSTRSTRUCT2** ppStruct);    
  34.     
  35.     [ DllImport( "..\\LIB\\PinvokeLib.dll" )]
  36.     public static extern void TestOutArrayOfStructs( out int size, out IntPtr outArray );
  37.     
  38.     [ DllImport( "..\\LIB\\PinvokeLib.dll" )]
  39.     public static extern void TestOutArrayOfStructs( out int size, MyUnsafeStruct** outArray );    
  40.     
  41. }
  42.  
  43. public class App
  44. {
  45.     public static void Main()
  46.     {
  47.         Console.WriteLine( "\nUsing marshal class\n" );
  48.         UsingMarshal();
  49.         
  50.         Console.WriteLine( "\nUsing unsafe code\n" );
  51.         UsingUnsafe();
  52.     }
  53.     
  54.     public static void UsingMarshal()    
  55.     {
  56.         int size;
  57.         IntPtr outArray;
  58.         
  59.         LibWrap.TestOutArrayOfStructs( out size, out outArray );
  60.         
  61.         MyStruct[] manArray = new MyStruct[ size ];
  62.         
  63.         IntPtr current = outArray;
  64.         for( int i = 0; i < size; i++ )
  65.         {
  66.             manArray[ i ] = new MyStruct();
  67.             Marshal.PtrToStructure( current, manArray[ i ]);
  68.             
  69.             //Marshal.FreeCoTaskMem( (IntPtr)Marshal.ReadInt32( current ));
  70.             Marshal.DestroyStructure( current, typeof(MyStruct) );
  71.             current = (IntPtr)((int)current + Marshal.SizeOf( manArray[ i ] ));
  72.             
  73.             Console.WriteLine( "Element {0}: {1} {2}", i, manArray[ i ].buffer, manArray[ i ].size );
  74.         }
  75.         Marshal.FreeCoTaskMem( outArray );    
  76.     }
  77.     
  78.     public static unsafe void UsingUnsafe()
  79.     {
  80.         int size;
  81.         MyUnsafeStruct* pResult;        
  82.         LibWrap.TestOutArrayOfStructs( out size, &pResult );
  83.         
  84.         MyUnsafeStruct* pCurrent = pResult;
  85.         for( int i = 0; i < size; i++, pCurrent++ )
  86.         {
  87.             Console.WriteLine( "Element {0}: {1} {2}", i, 
  88.                 Marshal.PtrToStringAnsi( pCurrent->buffer ), pCurrent->size );
  89.             Marshal.FreeCoTaskMem( pCurrent->buffer );
  90.         }
  91.         
  92.         Marshal.FreeCoTaskMem( (IntPtr)pResult );        
  93.     }
  94. }
  95.  
  96.  
  97.