1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
'Constantes
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const OPEN_ALWAYS = 4
Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
'Fonctions privées
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) 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 Long) As Long
Private Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function SetEndOfFile Lib "kernel32" (ByVal hFile As Long) As Long
'Fonction publique
Public Function EcrireFichier(Fichier As String, ByVal Donnees As String, Optional EcraseFichier As Boolean = False) As Boolean
Dim hFichier As Long
Dim Succes As Long
Dim lBytesWritten As Long
Dim AEcrire As Long
If EcraseFichier = True And Dir(Fichier) <> "" Then Exit Function
AEcrire = Len(Donnees)
hFichier = CreateFile(Fichier, GENERIC_WRITE Or GENERIC_READ, _
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
If hFichier <> INVALID_HANDLE_VALUE Then
Succes = WriteFile(hFichier, ByVal Donnees, _
AEcrire, lBytesWritten, 0) <> 0
Succes = SetEndOfFile(hFichier)
If Succes <> 0 Then
Succes = FlushFileBuffers(hFichier)
Succes = CloseHandle(hFichier)
End If
End If
ErrorHandler:
EcrireFichier = Succes
End Function |
Partager