Page 1 of 1

Including a binary file to a compiled executeable

Posted: Tue May 27, 2003 6:43 pm
by Johan_Haegg
I have written a program and found the address for a lable where i want to insert a binary file that the program is able to read.
I am 100% sure that the address is the correct, and the data that will be inserted is taged with the size of the data.
But how the )#% do i convert the position to the position in the file?
I found 4200545, so it is not in bits, and the file is 20 000byte large, so it is not a position in bytes.
Working on a self-extracting package if anyone is interested in what it will be used for.

Posted: Tue May 27, 2003 7:25 pm
by darklordz
How to include a File and the Extract it....

Code: Select all

Procedure Expand()
    If CreateFile(1, "REALEXEFILENAMEHERE")
        UseFile(1)
        L1= ?ExitProc-?Inc
        WriteData(?Inc,L1) 
        CloseFile(1)
    EndIf
    ProcedureReturn 1
Inc:
    IncludeBinary "REALEXEFILENAMEHERE"
ExitProc:
EndProcedure

Posted: Tue May 27, 2003 7:56 pm
by Johan_Haegg
The File is inserted after the program is compiled, so IncludeBinary wont work.

Code: Select all

; PureBasic Visual Designer v3.62


;- Window Constants
;
#Window_0 = 0

;- Gadget Constants
;
#txtpath = 0
#path = 1
#ext = 2
#txt = 3

#size = 4200545

Procedure UnPak(outdir.s, infile.s)
  OpenPack(infile.s)
  CreateDirectory(outdir.s)
  Mem = NextPackFile()
  
  CreateFile(#1, outdir.s+"list.lst")
  WriteData(Mem, PackFileSize())
  CloseFile(#1)
  
  ReadFile(#1, outdir.s+"list.lst")
  Repeat
    procname.s = ReadString()
    If Right(Procname.s,1) = "\"
      PrintN("Created directory: " + outdir.s + procname.s)
      CreateDirectory(outdir.s+procname.s)
    Else
      Mem = NextPackFile()
      PrintN("Unpacking: " + outdir.s + procname.s)
      CreateFile(#2, outdir.s + procname.s)
      WriteData(Mem, PackFileSize())
      CloseFile(#2)
      UseFile(#1)
    EndIf
  Until Eof(#1)
  CloseFile(#1)
  ClosePack()
EndProcedure


Procedure Open_Window_0()
  Delay(1)
  If OpenWindow(#Window_0, 215, 0, 290, 73,  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar , "SmartPak Selfextractor")
    If CreateGadgetList(WindowID())
      StringGadget(#txtpath, 0, 0, 270, 20, "")
      ButtonGadget(#path, 270, 0, 20, 20, "...")
      ButtonGadget(#ext, 200, 30, 90, 40, "Extract")
      TextGadget(#txt, 5, 30, 190, 40, "First public beta of SmartPak, dont expect something extraordinary or completly bug-free.")
      
    EndIf
  EndIf
EndProcedure
If ReadFile(#1, "d:\omgunoob.txt")
  CloseFile(#1)
  CreateFile(#1, "d:\temp.txt")
  WriteString(Str(?LB))
  CloseFile(#1)
EndIf
Open_Window_0()
SetGadgetText(#txtpath,PeekS(#size))
Repeat
  Event = WaitWindowEvent()
  If Event = #PB_EventGadget
    Select EventGadgetID()
      
      Case #path
        SetGadgetText(#txtpath, PathRequester("Select Path...", GetGadgetText(#txtPath)))
      Case #ext
      CreateDirectory(GetGadgetText(#txtPath))
      CreateFile(#1, GetGadgetText(#txtPath) + "smartpak.tmp")
      WriteData(#size+259, PeekL(#size+255))
      CloseFile(#1)
      UnPak(GetGadgetText(#txtPath), GetGadgetText(#txtPath) + "smartpak.tmp")
      DeleteFile(GetGadgetText(#txtPath) + "smartpak.tmp")
      Quit = 1
    EndSelect
  EndIf
  If Event = #PB_EventCloseWindow
    Quit = 1
  EndIf
Until Quit=1
Delay(1000)
End
LB:
Lots of tweaking to do, but its the result of an hours work and loads of hours of technical problems with this one thing.

A string is inserted with the default path and a long is inserted with total filesize, then the package made with CreatePackage() is inserted into the executeable.

So, no IncludeBinary is possible due to the fact that the final product will not be shipped with PureBasic compiler and libraries.

Hope it clear what needs to be done.

Posted: Tue May 27, 2003 8:13 pm
by Pupil
I think the way to go is to append the data to the exe and not try to binary include it somehow. I.e. you create the extract tool exe and then just writes the data to the end of this file. To find the data you just open the exe that's running and check for the occurence of a ID-tag at the start of the data...

Posted: Tue May 27, 2003 10:49 pm
by Johan_Haegg
i have tried to append the exe with the data, but i always get an error saying that the file will not fit into the memory. And the appended file is only 680kb...

[EDIT]
Found one error, i used an id twice when allocateing memory in the procedure that combines the files... one problem solved ;)

Posted: Tue May 27, 2003 10:54 pm
by Pupil
Johan_Haegg wrote:i have tried to append the exe with the data, but i always get an error saying that the file will not fit into the memory. And the appended file is only 680kb...
That's weird, i've appended data > 20 MBytes to an exe and have had no problem...

Posted: Tue May 27, 2003 11:33 pm
by Johan_Haegg
The problem was that i somehow mixed the buffers containing the program and some other buffer when appending the exe, its solved now, but i still cant address the begining of the data correctly...
#size = 4194304 min
#size = 4227072 max

even if the package is added, it still wont work with higher values. This is makeing me insane! Is there a way for the program to know its own name and location?

btw, the "This program cannot..." starts at 4194382 and it seems to be counting in bytes. If i just had my good old hexeditor here.

Posted: Wed May 28, 2003 10:25 am
by Johan_Haegg
Well, found out that it starts counting from H400000, but after that, no idea. H4BD8 position in the exe(found my good old hexedit) is located at H404BB8
when the program itself is running. So it lost 32byte somewhere.
And i still cant get it to read the data inserted witj

Code: Select all

Procedure AddToSFX(pakfile.s, path.s, outfile.s)
  outpath = AllocateMemory(#1, 255)
  PokeS(outpath, path.s)

  ReadFile(#2, "d:\smartpak.exe")
  smartpak = AllocateMemory(#2, Lof())
  ReadData(smartpak, Lof())
  paklen = Lof()
  CloseFile(#2)

  ReadFile(#2, pakfile.s)
  pak = AllocateMemory(#3, Lof())
  ReadData(pak, Lof())
  length = AllocateMemory(#4, 4)
  PokeL(length, Lof())
  CloseFile(#2)
  
  CreateFile(#2, outfile.s)
  WriteData(smartpak, paklen)
  WriteData(outpath, 255)
  WriteData(length, 4)
  WriteData(pak, PeekL(length))
  CloseFile(#2)

EndProcedure
Used this code as a replacement for the old ?LB-output:

Code: Select all

If ReadFile(#1, "d:\omgunoob.txt")
  a = #size
  Repeat
    If PeekS(a) <> ""
      Length = Len(PeekS(a))
      answer = MessageRequester("Debug Mode", PeekS(a) + " @ " + Str(a) + " Len:" + Str(Length), #PB_MessageRequester_YesNo)
      a = a + Length
    EndIf
    a = a + 1
  Until Answer=7
EndIf
When the data and program is combined, it still crashes when i reach a position over H404BB8. Well well, i might as well just start with the packet distrubution system i was planning and skip the self-extracting until i get some inspiration or something ;)

[EDIT]
1. Allways read FAQ when you get stuck
2. See 1.
3. You dont get it, do you?

Found how to get the app to know its own name and path in the FAQ, ill use that instead of the non-working tech i am using right now.

Posted: Wed May 28, 2003 11:53 am
by Froggerprogger
I haven't read all text here, but how about of making a copy of your exe in the system's-temp-path at programstart ?

Code: Select all

temppath.s = Space(1000)
GetEnvironmentVariable_("TEMP",temppath.s,1000)
Then you could change the binarypart inside it and afterwards:
a) start it for one-time-use or
b) start it with a programparameter, that copies it back to the program's directory and overwrites the old one.

But perhaps this is not what you were looking for ?!$%

Posted: Wed May 28, 2003 12:59 pm
by Johan_Haegg
I finaly managed to get it to work!
If someone want the full source for the packer and the self-extracting exe, feel free to download it from:
smartpak.rar

You might have to right-click and save as, i am to lazy to configure apache correctly ;)

The Example is not 100% complete, ran out of time...

Link Broken

Posted: Tue Mar 09, 2004 12:51 am
by kruddick
This is exactly what I am looking for... but the link is broken. Can you post the code?

Thanks
Kent

Posted: Tue Jul 06, 2004 4:54 pm
by Bonne_den_kule
Can you post the code?

I need help i too!