Attribute VB_Name = "Fichiers"
'API Fichiers
Const FILE_BEGIN = 0
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_NEW = 1
Const CREATE_ALWAYS = 2
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000

Private Declare Function ReadFile Lib "kernel32" _
(ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long

Private Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Private Declare Function SetFilePointer Lib "kernel32" _
(ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long

Private Declare Function GetFileSize Lib "kernel32" _
(ByVal hFile As Long, lpFileSizeHigh As Long) As Long


'#############################################
'#                                            #
'# Retourne le contenu du fichier "FileName"  #
'#                                            #
'#############################################

Public Function OpenFileAPI(ByRef FileName As String) As String
    Dim hOrgFile As Long
    Dim nSize As Long
    Dim Ret As Long
    
    On Error GoTo ErrorHandler
    
    'Récupère un Handle pour manipuler le flux mémoire du fichier
    hOrgFile = CreateFile(FileName, _
                            GENERIC_READ, _
                            FILE_SHARE_READ Or FILE_SHARE_WRITE, _
                            ByVal 0&, OPEN_EXISTING, 0, 0)

    'Taille du fichier
    nSize = GetFileSize(hOrgFile, 0)

    DoEvents

    'Initialise le pointeur sur le fichier
    SetFilePointer hOrgFile, 0, 0, FILE_BEGIN

    OpenFileAPI = Space(nSize)

    'Charge le contenu du fichier dans la variable OpenFileAPI
    ReadFile hOrgFile, ByVal OpenFileAPI, nSize, Ret, ByVal 0&
     
    DoEvents
     
    'Ferme le fichier
    CloseHandle hOrgFile
    
    Exit Function
ErrorHandler:
    OpenFileAPI = ""
    CloseHandle hOrgFile
End Function


'#########################################################
'#                                                       #
'# Ecrit dans le fichier "FileName" la String strStream. #
'#                                                       #
'#########################################################

Public Function SaveFileAPI(ByRef FileName As String, ByRef strStream As String) As Boolean
    Dim hNewFile As Long
    Dim nSize As Long
    Dim Ret As Long
    
    On Error GoTo ErrorHandler
    
    'Récupère un Handle pour manipuler le flux mémoire du fichier
    hNewFile = CreateFile(FileName, _
                        GENERIC_WRITE, _
                        FILE_SHARE_READ Or FILE_SHARE_WRITE, _
                        ByVal 0&, CREATE_ALWAYS, 0, 0)
                            
    'Taille du flux à écrire dans le fichier
    nSize = Len(strStream)

    'Initialise le pointeur sur le fichier
    Call SetFilePointer(hNewFile, 0, 0, FILE_BEGIN)
    
    'Ecrit le flux 'strStream' dans le fichier
    Call WriteFile(hNewFile, ByVal strStream, nSize, Ret, ByVal 0&)

    DoEvents
     
    'Ferme le fichier
    CloseHandle hNewFile
    
    SaveFileAPI = (Ret <> 0)
    
    Exit Function
ErrorHandler:
    SaveFileAPI = False
    'Ferme le fichier
    CloseHandle hNewFile
End Function


