Page 1 of 1

Pack Files into Compiled Exe

Posted: Sun May 15, 2011 12:49 pm
by armixeight
Hi was wondering if there is way to somehow include a type of file into a EXE compiled by PureBasic.

The Complete Scenario is:
I have an EXE that is designed to execute certain functions by interpreting a text file example (my basic idea):

Code: Select all

ReadFile( #FILE, "packed_file.txt" )
LINE = ReadLine( #FILE )

If LINE = "hello"
  MessageBox("The file said hello somewhere in it")
Endif

End
The problem is I don't know how to do this and if it is actually possible.
I would imagine that the Include Binary functions work when it comes to including a file and compiling it with the EXE but I'm after a way to externally add a file into the interpreter EXE which will then work like above. I think I need some sort of pointer inside the interpreter's code that will identify the file added and read from it but honestly i don't know.

Code: Select all

[EXTERNAL PROGRAM] == PACK FILE == >> [INTERPRETER] == READ TEXT FROM PACKED TEXT FILE == >> [OUTPUT].
Essentially I need to know how to Pack a file [external program code] and how to interpret the packed file [interpreter program code].

All of your help with be greatly appreciated. Thank you in advance.

~ArmixEIGHT

Re: Pack Files into Compiled Exe

Posted: Sun May 15, 2011 1:45 pm
by ts-soft

Re: Pack Files into Compiled Exe

Posted: Sun May 15, 2011 3:35 pm
by Nituvious
I think this is exactly what I posted about but said in a better way! I am very interested in this topic too.
How do you pack an already compiled program with an external file filled with instructions for an interpreter?

Re: Pack Files into Compiled Exe

Posted: Sun May 15, 2011 10:49 pm
by armixeight
ts-soft
Thank you oh so very much for your beautiful code, that is exactly what I needed!
My project can now continue. :D

Nituvious
Nituvious wrote: I think this is exactly what I posted about but said in a better way! I am very interested in this topic too.
How do you pack an already compiled program with an external file filled with instructions for an interpreter?
I took the liberty of translating the German version into English to help others with this question. I hope you don't mind ts-soft. :D

All credit of course goes to ts-soft and his original post and pre-compiled example can be found here PureBasic German

[EXTERNAL PROGRAM]

Code: Select all

EnableExplicit

DataSection
  stubs:
  IncludeBinary "stubs\stubs.exe"
  stubs_end:
EndDataSection

Define.s TextFile, ExeFile
Define FF, *mem, length

TextFile = OpenFileRequester("Please choose a text file:", "readme.txt", "Text (*.txt)|*.txt", 0)
If TextFile
  FF = ReadFile(#PB_Any, TextFile)
  If FF
    length = Lof(FF)
    *mem = AllocateMemory(length)
    If *mem
      ReadData(FF, *mem, length)
    EndIf
    CloseFile(FF)
  EndIf
  
  ExeFile = SaveFileRequester("Please choose an output file:", "readme.exe", "Exe (*.exe)|*.exe", 0)
  
  If ExeFile
    FF = CreateFile(#PB_Any, ExeFile)
    If FF
      WriteData(FF, ?stubs, ?stubs_end - ?stubs)
      WriteData(FF, *mem, length)
      WriteQuad(FF, length)
      CloseFile(FF)
      
      RunProgram(ExeFile)
    EndIf
  EndIf
EndIf
[INTERPRETER]

Code: Select all

EnableExplicit

Define FF, length.q, *mem, Text.s

FF = ReadFile(#PB_Any, ProgramFilename())

If FF
  FileSeek(FF, Lof(FF) - SizeOf(Quad)) ; am Ende die länge des Angehängten lesen
  length = ReadQuad(FF)
  FileSeek(FF, Lof(FF) - SizeOf(Quad) - length) ; zum start des Angehängten
  If length
    *mem = AllocateMemory(length)
    If *mem
      ReadData(FF, *mem, length) ; Angehängtes in den Speicher
    EndIf
  EndIf
  CloseFile(FF)
  
  If *mem
    Text = PeekS(*mem)
    FreeMemory(*mem)
  EndIf
  
  
  OpenWindow(0, #PB_Ignore, #PB_Ignore, 640, 480, "Text2Exe", #PB_Window_SystemMenu)
  EditorGadget(0, 5, 5, 630, 470, #PB_Editor_ReadOnly)
  SetGadgetText(0, Text)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  
EndIf

Re: Pack Files into Compiled Exe

Posted: Fri Jul 08, 2011 10:36 pm
by ultralazor
use image struct to get offset of resource section, create and entry that will be scanned for and increase size in pe header if needed. This is 'proper' as it'll never break anything even in fragile embedded and .NET PE, unless it has crypto checks on itself..

it takes too much binary scanning and ordering to just whip one of for a post.

Re: Pack Files into Compiled Exe

Posted: Mon Oct 31, 2011 5:22 pm
by ts-soft
This line goes to the end of file - quad (here is the length stored) - length (here begins your stuff).
This line is very simple :wink:

Re: Pack Files into Compiled Exe

Posted: Wed Nov 02, 2011 12:24 pm
by ts-soft
Louise wrote:Thanks.
If I want a non-text files to store and use what parts do I need to change.?
For example :
Exe file or Bmp file.
There is nothing to change. You create a empty File with CreateFile and writes the datas
and close the file.

Re: Pack Files into Compiled Exe

Posted: Thu Nov 03, 2011 3:26 pm
by ultralazor
FYI: The right way to do this, where it won't break if resource section of PE isn't the last or if relocation are handled differently, is to use the PE head information and append a resource struct according to PE spec.

I've done it where I can load inline asm from a resource.