Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Include binaries...
#1
Found a little tool that can compile a *.res file into a *.bin file.

Is it possible to include binaries into the exe, and access them?
(not as normal resources...)

If so could somebody provide a little example?

Thanks
Reply
#2
It can be done with resources, but then it becomes Windows-dependent and FindResource/LoadResource will be needed, i never used 'em thought, nor i know how the resource compiler handles raw data.
Reply
#3
Quote:It can be done with resources, but then it becomes Windows-dependent and FindResource/LoadResource will be needed, i never used 'em thought, nor i know how the resource compiler handles raw data.

Resources are nice, but they can be changed afterwards...

It should be possible to do it without resources, and that's what I'm after...

I'm a "xxxxBasic" fugitive and there is a command called "IncludeBinary".
The IncludeBinary command should be placed into the data section like:
Code:
Data
    Label1:
    IncludeBinary "MyData.bin" 'could be a image
    Label2:
    IncludeBinary "YourData.bin" ' could be something else
    Label3:
  End Data

Now the data resides between these labels (are LABELS available in FreeBASIC?) and can be accessed through different commands.

------
Example:
If you look at some programs the icons for the toolbar are sitting on the hd as normal files and loaded by start.
But what if the icons are missing?
A program like fbide (the fb version with wx-c.dll) starts without icons and the toolbar buttons are empty.

It would be nice to be able to add binary data into the program and access it if needed.
(without hd write/read)
Reply
#4
I haven't dome this with FB, but I have with Delphi. In one of my programs I append data to the end of the exe with a little utility program I wrote. The utility opens the exe, seeks t the end of the file and then appends data to it. It also adds the offset of the data start and a signature tag.

When the program runs, it opens the exe in Read Only mode, checks for a tag, and if it finds it, loads the offset to the start of the data and reads it in.

It is quite straight forward, and I would think you could do the same thing with FB.
Reply
#5
You could also write a little utility to generate DATA statements from the binary file, insert them into your source code, and then READ them at run time.
Reply
#6
Quote:You could also write a little utility to generate DATA statements from the binary file, insert them into your source code, and then READ them at run time.
This sounds like a good idea Big Grin
Thanks

Didn't find an example of using data statements with fb, where can I find one?
If you have several data statements, how can you access the right one?

Thanks in advance...
Reply
#7
It would be better to use intialized arrays, ala in C:

Code:
''
'' bin2bas -- usage: bin2bas binaryfile.ext
''

declare function hStripExt( byval filename as string ) as string

    dim as string filename
    dim as integer filelen
    
    filename = command$(1)
    
    ''
    ''
    ''
    if( open( filename, for binary, access read, as #1 ) <> 0 ) then
        print "Error: Binary file not found"
        end 1
    end if
    
    filename = hStripExt( filename )
    filelen = lof( 1 )
    
    if( filelen = 0 ) then
        print "Error: Empty file"
        end 1
    end if
    
    ''
    ''
    ''
    if( open( filename + ".bi", for output, as #2 ) <> 0 ) then
        print "Error: Cannot create the inc file"
        end 1
    end if
    
    print #2, "dim shared " + filename + "_data(0 to " + str$( filelen ) + "-1) as ubyte = { _"
    
    ''
    ''
    ''
    dim as ubyte ub
    dim as integer bytes, cnt = 0
    
    bytes = 0
    cnt = 0
    do while( not eof( 1 ) )
        
        get #1, , ub
        bytes += 1
        
        if( cnt > 30 ) then
            cnt = 0
            print #2, " _"
        end if
        
        print #2, "&h" + hex( ub );        
        cnt += 1
        
        if( bytes < filelen ) then
            print #2, ",";
        end if
    loop
    
    print #2, " }"
    
    close #2
    
    close #1
    
    
'':::::
function hStripExt( byval filename as string ) as string static
    dim p as integer, lp as integer

    lp = 0
    do
        p = instr( lp+1, filename, "." )
        if( p = 0 ) then
            exit do
        end if
        lp = p
    loop

    if( lp > 0 ) then
        function = left$( filename, lp-1 )
    else
        function = filename
    end if

end function

An include file will be created, the array will have the same name as in the input file plus a _data suffix.

DATA's are converted to string's, what take a lot of space.. not taking READ into account, that can be quite slow to load..
Reply
#8
Thanks Vic.

Quote:DATA's are converted to string's, what take a lot of space.. not taking READ into account, that can be quite slow to load..
Ok, scrap this... :wink:

Quote:It would be better to use extern's and an intialized array, ala in C, with a small tool to convert binary data to .bas it could be done as:

mybmp.bi:
extern mybmp_data(0 to filelen-1) as ubyte

mybmp.bas
#include mybmp.bi
dim mybmp_data(0 to filelen-1) as ubyte = { ... }

test.bas
#include mybmp.bi

dim mybmp as ubyte ptr = @mybmp_data(0)
...

fbc test.bas mybmp.bas
how is the data from the bmp file loaded and transfered into ?
here?:
Code:
dim mybmp_data(0 to filelen-1) as ubyte = {... hex data of bmp? like: 00, 4D, ...}
hmm, never used EXTERN before...
why is it needed?

Somehow you lost me here :???:
Sorry, sometimes I'm a real pain...

Quote:Btw, the lastest testing version is needed, before 0.14 extern's didn't support static arrays.. testing version can be download from FB's site.
downloaded :wink:

Almost got it :oops:

EDIT
You are fast :king:
went away for awhile and searched for examples of extern etc.

and you already coded an example. :bounce:

Thanks, will look into it...
Reply
#9
Converted as a test the fblogo icon.

Used the "hello" windows gui example and added:
Code:
'...
#include once "fblogo.bi"
'...
'change app icon
dim mybmp as ubyte ptr = @fblogo_data(0)
SendMessage (hWnd, WM_SETICON, NULL, mybmp)
'...
also tried:
Code:
'change app icon
SendMessage (hWnd, WM_SETICON, NULL, @fblogo_data(0))
but the icon doesn't show up...

any thoughts?

BTW: fbc v14 pre-beta used...
Reply
#10
Last SendMessage()'s argument needs a BYVAL, it's declared as AS ANY.. byval @... should do it.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)