Working With Resource File

Lesson 2
Tutorials - Page 1 - Page 2 - Page 3 - Page 4 - Page 5

How the LoadDataIntoFile Function works?


Public Sub LoadDataIntoFile(DataName As Integer, FileName As String

Declaring myArray and myFile variables

    Dim myArray() As Byte
    Dim
myFile As Long
   

If Dir("D:\Myfile.Exe") = ""  Return True if the file exist and False if doesn't.
If the file exist we don't start the copying process,
to avoid overwriting an existing file.


   If Dir(FileName) = "" Then 

LoadResData loads file from Resource file that founds under the CUSTOM "Folder",
like LoadResPicture loads Picture from resource file that found under all other
Folders (Icon, Cursor and Bitmap)
In the line below, we load the File that found under the CUSTOM "Folder"
with the ID that the DataName variable holds, to the variable myArray
 

        myArray = LoadResData(DataName, "CUSTOM")

FreeFile is a function that returns the next Free File Number
If we wouldn't use FreeFile, and instead we were using:
Open FileName For Binary Access Write As #1
And In our code there was before similar line like:
Open "Autoexec.Bat" For Binary Access Write As #1
There was a collision because they were both opened As #1
The FreeFile function prevents that and in the example above would open
The second file as #2

 
        myFile = FreeFile

Opens new file with the name that founds in the FileName variable.

       
Open FileName For Binary Access Write As #myFile

Put the myArray variable data into the new file.
Remember that the myArray variable holds right now the data of the file
we want to copy


        Put #myFile, , myArray

Close the file

       
Close #myFile
    End If
End Sub

Back  Forward