Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

File Mapping

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • File Mapping

    Small example on File Mapping encapsulation.

    Save the source as mapfile.bas.

    Code:
    #Compiler PBWin 9
    #Compile Exe
    #Dim All
    
    #Include "win32api.inc"
    
    '******************************************************************************
    Class CMapFile
    '******************************************************************************
      Instance pData    As Dword
      Instance hFileMap As Dword
      Instance hFile    As Dword
    
      '****************************************************************************
      Interface IMapFile
      '****************************************************************************
        Inherit  IUnknown
    
        '**************************************************************************
        Method Open(ByRef FileName As String) As Long
        '**************************************************************************
        
          If IsFile(FileName) Then
            hFile = CreateFile (ByVal StrPtr(FileName), %GENERIC_READ, %FILE_SHARE_READ, ByVal %NULL, %OPEN_EXISTING, %FILE_ATTRIBUTE_NORMAL, ByVal %NULL)
            If hFile Then
              hFileMap = CreateFileMapping (hFile, ByVal %NULL, %PAGE_READONLY, ByVal %NULL, ByVal %NULL, ByVal %NULL)
              If hFileMap Then
                pData = MapViewOfFile (hFileMap, %FILE_MAP_READ, ByVal %NULL, ByVal %NULL, ByVal %NULL)
                If pData Then
                  Method = pData
                  Exit Method
                End If
              End If
            End If
          End If
          
          me.Close
          Method = %False
          
        End Method
    
        '**************************************************************************
        Method Close
        '**************************************************************************
    
          If pData Then
            UnmapViewOfFile pData
            pData = 0
          End If
          
          If hFileMap Then
            CloseHandle hFileMap
            hFileMap = 0
          End If
          
          If hFile Then
            CloseHandle hFile
            hFile = 0
          End If
            
        End Method
    
      End Interface
    
    '******************************************************************************
    End Class
    '******************************************************************************
    
    
    'Teste Code
    '******************************************************************************
    Function PBMain As Long
    '******************************************************************************
      Local pMem As Asciiz Ptr
    
      Dim MapFile As IMapFile
      Let MapFile = Class "CMapFile"
      
      pMem = MapFile.Open("mapfile.log")
      
      If pMem Then
        MsgBox "MapFile Class Test" + $CrLf + $CrLf + $CrLf + @pMem
        MapFile.Close
      End If
    
    End Function
    
    '******************************************************************************
    'END  Program
    '******************************************************************************
    Regards,
    RValois.
    Last edited by Roberto Valois; 25 Aug 2008, 06:12 PM. Reason: Sanity check on test code
    http://www.rvalois.com.br/downloads/free/
Working...
X