Penulis Topik: Lock File VB6  (Dibaca 16938 kali)

Offline habibihza

  • Pro3
  • **
  • Tulisan: 9
  • Reputation: 43
  • Jenis kelamin: Pria
    • Lihat Profil
    • FLASH Antivirus
Lock File VB6
« pada: Maret 19, 2014, 08:07:52 PM »
Para mastah,,,  ;) Gimana sih codenya lock file, seperti smadav waktu rtpnya muncul, semua file virus langsung dilock...

Kutip
Open "example.txt" For Input Lock Read Write As #intFile

itu codenya bukan?? waktu ane lock pake command button biasa bisa,,,,,,  ^-^
tapi waktu ane gabungin ke engine kagak bisa... yah,,,  ~x(

Mohon bimbingannya mastah,,,,  ;)
\\\\\\\"apa yang kita tanam itulah yang kita petik\\\\\\\"

Offline maulanalways

  • Pro10
  • ***
  • Tulisan: 20
  • Reputation: 48
  • Jenis kelamin: Pria
  • 404 not found
    • Lihat Profil
    • Mysoft-id
Re: Lock File VB6
« Jawab #1 pada: Oktober 02, 2014, 03:42:22 PM »
klo menurut ane sih pke fungsi API ReadFile, CreateFile, etc.  :-\

Contoh yg ane buat :D
filenya di buka tapi gk langsung di tutup(CloseHandle) jadi aplikasi lain yg ingin mengakses file tsb tdk bisa karna filenya sedang di gunakan :D
taruh code di bawah ini dalam sebuah module  :P
Kode: [Pilih]
'udah Support UniCode :D
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 ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long
Private Declare Function CreateFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long

Private Const  CREATE_NEW = 1
Private Const  CREATE_ALWAYS = 2
Private Const  OPEN_EXISTING = 3
Private Const  OPEN_ALWAYS = 4

Private Const GENERIC_ALL = &H10000000
Private Const GENERIC_EXECUTE = &H20000000
Private Const GENERIC_WRITE = &H40000000
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_READ_WRITE = &HC0000000

Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const FILE_SHARE_READ_WRITE = &H3
Private Const FILE_SHARE_DELETE = &H4

Private Const FILE_ATTRIBUTE_NORMAL = &H80

public hFile As Long 'handle file

Public Function OpenFileW(ByVal FileName As String)
    On Error GoTo ErrHandle 'jika terjadi error langsung exit function cari aman aja biar gk hang :v

      hFile=CreateFileW (StrPtr(FileName),  GENERIC_READ, FILE_SHARE_WRITE Or FILE_SHARE_DELETE, ByVal 0&, 0 , FILE_ATTRIBUTE_NORMAL, 0)

    If hFile > 0 Then 'jika nilai hFile= 1 berhasil membuka file :)
        OpenFileW = hFile
    Else
        GoTo ErrHandle 'tidak berhasil membuka file :(
    End If

ErrHandle:
    On Error GoTo 0
end function

Public Function CloseFile(ByVal hFile As Long) As Long 'untuk menutup file yg terbuka
    On Error Resume Next
    If hFile > 0 Then
        CloseFile = CloseHandle(hFile)
    End If
    On Error GoTo 0
End Function


untuk menggunakanya tinggal panggil fungsi OpenFileW(Path & Filename)
example : OpenFileW("C:\Test.txt")

CloseFile(hFile) << fungsi untuk menutup file
yg di atas cuma untuk 1 file aja :D tinggal di kembangkan
Malsoft. :-bd mysoft-id|Droid-Bot ^_^