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

  1. ' Copyright
  2. ' Microsoft Corporation
  3. ' All rights reserved
  4.  
  5. ' HandleRef.vb
  6.  
  7. Imports System
  8. Imports System.IO
  9. Imports System.Text
  10. Imports System.Runtime.InteropServices
  11.  
  12. 'typedef struct _OVERLAPPED { 
  13. '    ULONG_PTR  Internal; 
  14. '    ULONG_PTR  InternalHigh; 
  15. '    DWORD  Offset; 
  16. '    DWORD  OffsetHigh; 
  17. '    HANDLE hEvent; 
  18. '} OVERLAPPED; 
  19.  
  20. ' declared as structure
  21. < StructLayout( LayoutKind.Sequential )> _
  22. Public Structure Overlapped
  23.     Public intrnal As IntPtr
  24.     Public internalHigh As IntPtr
  25.     Public offset As Integer
  26.     Public offsetHigh As Integer
  27.     Public hEvent As IntPtr
  28. End Structure 'Overlapped
  29.  
  30. ' declared as class
  31. < StructLayout( LayoutKind.Sequential )> _
  32. Public Class  Overlapped2
  33.     Public intrnal As IntPtr
  34.     Public internalHigh As IntPtr
  35.     Public offset As Integer
  36.     Public offsetHigh As Integer
  37.     Public hEvent As IntPtr
  38. End Class 'Overlapped2
  39.  
  40.  
  41. Public Class LibWrap
  42.    
  43.     ' to prevent FileStream to be GC-ed before call ends, 
  44.     ' its handle should be wrapped in HandleRef
  45.     
  46.     ' since Overlapped is struct, Nothing can't be passed instead, 
  47.     ' we must declare overload method if we will use this    
  48.     
  49.     ' BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
  50.     '                LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped);    
  51.    
  52.     Overloads Declare Ansi Function ReadFile Lib "Kernel32.dll" ( _
  53.         ByVal hndRef As HandleRef, _
  54.         ByVal buffer As StringBuilder, _
  55.         ByVal numberOfBytesToRead As Integer, _
  56.         ByRef numberOfBytesRead As Integer, _
  57.         ByRef flag As Overlapped ) _
  58.     As Boolean
  59.    
  60.     Overloads Declare Ansi Function ReadFile Lib "Kernel32.dll" ( _
  61.         ByVal hndRef As HandleRef, _
  62.         ByVal buffer As StringBuilder, _
  63.         ByVal numberOfBytesToRead As Integer, _
  64.         ByRef numberOfBytesRead As Integer, _
  65.         ByVal flag As Integer ) _  
  66.     As Boolean    ' int instead of structure reference
  67.    
  68.    ' since Overlapped2 is class, we can pass Nothing as parameter  
  69.    ' no overload is needed        
  70.                                    
  71.     Declare Ansi Function ReadFile2 Lib "Kernel32.dll" Alias "ReadFile" ( _
  72.         ByVal hndRef As HandleRef, _
  73.         ByVal buffer As StringBuilder, _
  74.         ByVal numberOfBytesToRead As Integer, _
  75.         ByRef numberOfBytesRead As Integer, _
  76.         <[In], Out> ByVal flag As Overlapped2 ) _ 
  77.     As Boolean
  78.    
  79. End Class 'LibWrap
  80.  
  81. Public Class App
  82.    Public Shared Sub Main()
  83.    
  84.       Dim fs As New FileStream( "HandleRef.txt", FileMode.Open )
  85.       Dim hr As New HandleRef( fs, fs.Handle )
  86.       Dim buffer As New StringBuilder( 5 )
  87.       Dim read As Integer = 0
  88.       
  89.       ' platform invoke will hold reference to HandleRef until call ends
  90.       
  91.       LibWrap.ReadFile( hr, buffer, 5, read, 0 )
  92.       Console.WriteLine( "Read with struct parameter: {0}", buffer )
  93.       
  94.       LibWrap.ReadFile2( hr, buffer, 5, read, Nothing )
  95.       Console.WriteLine( "Read with class parameter: {0}", buffer )
  96.       
  97.    End Sub 'Main
  98. End Class 'App
  99.  
  100.