To load a binary file, you need to first read a binary file
, so open it like you would when reading/writing any other file in Visual Basic.
CODE
Dim FreeF as Long
FreeF = FreeFile
Open App.Path & "\LookABinaryFile.file" for Binary as #FreeF
Now that you have the file open, there are two functions you can use. Since you are reading, you probably only need one, so that's the only one I'll go over.
CODE
Get Filenumber, Byteposition, Variable
What this code segment does, is it grabs some data in the file and sticks in Variable. The length of how much you want is dependent on the type of variable you use. For example:
CODE
Dim LongVar as Long
Get #FreeF, 1, LongVar
This code segment would take the first 4 bytes of the file we opened above, and put it in the vairable LongVar. Since LongVar is a long, the value is automatically converted to a DWORD. For the variable length, you can use arrays as well.
To read a string, you first must identify how long of a string you want to grab.
CODE
Dim MyString as string
MyString = Space(10)
Use MyString in the Get function, and it would read 10 bytes into it.
Frankly, I find that using byte arrays are the best way to go. If you're trying to read a Starcraft scenario file, your best way would be to loop through each section, and sticking the section inside a corresponding array. An example would be something like this.
CODE
Private sType() as Byte
Dim SectionName as String
Dim SectionLength as Long
SectionName = Space$(4)
Get #FreeF, Counter1, SectionName
Counter1 = Counter1 + 4
Get #FreeF, Counter1, SectionLength
Counter1 = Counter1 + 4
Select Case SectionName
Case = "TYPE"
ReDim sType(SectionLength + 7)
Get #FreeF, Counter1 - 8, sType
... repeat for all sections
End Select
Counter1 = Counter1 + SectionLength
Explanation time
. Basically, this would be part of a loop throughout the scenario file. Since there is a bunch of sections, we can find the section name and section length by using the Get function. We then create a new array, and make it - 7 because our arrays are 0 based. If we made it - 8 we would have an extra empty byte at the end.
We then use the Get function to stick the entire section in our TYPE array.
If you ARE trying to read scenario files, I recommend taking a look at the OSMAP source.
If you are trying to read a different type of Binary file, if you give me more information I can probably give you some more help based on your actual scenario.
Good luck