March 28, 2024, 02:17:02 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Append File

Started by aurelCB, January 29, 2010, 01:22:12 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

Hi...
Is there any example about appending file to another existing file?
And is this works with biniry files to?

REDEBOLT

From the help file:
QuoteOPENFILE function

The OPENFILE function initiates the connection between your program and the outside world. It has as syntax of:

Error =OPENFILE ( FileVariable, filename, flags )

The filevariable must be either of type FILE or BFILE. The flags can either be "R" for reading, "W" for writing or "A" for appending to an existing file.

The function will return 0 if the file was successfully opened. Any other value means that there was a problem and you should not try reading or writing to the file.

Regards,
Bob

aurelCB

January 29, 2010, 03:04:56 AM #2 Last Edit: January 29, 2010, 03:08:19 AM by aurelCB
Yeah,i know that this is in help   ;),but is there any example how work?
Maby someone use this option in your program.
Might be useful especialy for binary files.
I ask this in first place becose i wanna make some sort of binder.
I will study this closer...

aurelCB

This is first attempt.
So i made simple console exe on C:\console.exe which size explore see as 128kB,
i calc 128*1024=131072 and this result i recive from program.
But when i try add few extra bytes on the end of exefile i recive weird results in size something
about 1MB ???

'new exe appended Test1
OPENCONSOLE
DEF exefile:BFILE
DEF exeLOF:INT
DEF newexeLOF:INT
DEF byte:Char

'step1 read exe size-------------------------------------
IF(OPENFILE(exefile,"C:\Bind1.exe","R") = 0)
exeLOF=LEN(exefile)
'IF(READ(exefile,mynumber) = 0)
       'PRINT "This is LEN of Bindexe: ",exeLOF
  'ENDIF
  CLOSEFILE exefile
ENDIF
'------------------------------------------------------
PRINT "This is LEN of Bindexe: ",exeLOF
print
DO:UNTIL INKEY$ <> ""
'-------------------------------------------------------
'step 2
'try read 4 bytes which define size of image
IF(OPENFILE(exefile,"C:\Bind1.exe","R") = 0)

For n= 1 TO 8
GET exefile,n,byte
PRINT hex$(byte)
Next n

CLOSEFILE exefile
ENDIF
'--------------------------------------------------------
PRINT "STOP"
DO:UNTIL INKEY$ <> ""
'---------------------------------------------------
'here i will add loop for add extrabytes
'-------------------------------------------------------
PRINT "Press Any Key To Close..."
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END


aurelCB

Hi people...

So im trying to load exe file (minGUI) into memory and then use memory buffer then write same exe only with
different name.
One guy give me binder written in PureBasic and work fine.
I use for testing just part load to memory & write exe from memory buffer.
But it seems that PB use pointers instead of memory variable.
I try same thing with CBasic and he write exe file but is not valid ,only explorer present same size.
here is PB code:
;LOAD exe to MEMORY
  OpenConsole()
  ;#FILE_ATTRIBUTE_ARCHIVE =
                                    ; First we must open a console
  ConsoleTitle ("PureBasic - Console Example:")  ; Now we can give the opened console a Titlename ;)
  ;entrypoint.l=0
runtimeexe.s="C:\minGUI.exe"
If ReadFile(0,runtimeexe)
runtimeexe.s="C:\minGUI.exe"
entrypoint=FileSize(runtimeexe)
        *mRuntime=AllocateMemory(entrypoint)
        ReadData(0,*mRuntime,entrypoint)
        CloseFile(0)

EndIf


;----------------------------------------------------------------------------
;READ from mwmory and write new exe
;target.s="C:\newGUI"
exename.s="C:\newGUI.exe"
        SetFileAttributes_(exename,#FILE_ATTRIBUTE_ARCHIVE)
       
        If CreateFile(1,exename)
          WriteData(1,*mRuntime,entrypoint)
          ;WriteData(1,*mCompilat,compsize)
          WriteLong(1,entrypoint)
          CloseFile(1)
          ;If exerun
           ; RunProgram(exename,"",GetPathPart(exename))
       EndIf

    CloseConsole()
End


And here is Creative code:
Def w1:window
DECLARE "kernel32",SetFileAttributesA(lpFileName AS STRING,dwFileAttributes AS INT),INT
CONST FILE_ATTRIBUTE_ARCHIVE = 0x20
CONST FILE_ATTRIBUTE_NORMAL = 0x80
CONST FILE_EXECUTE = 0x20
'declare variables
def runtimeExe:BFILE
def NewExe:BFILE
def membuffer:memory
'def membuffer2:Memory
'def pbuffer:pointer
def entrypoint:INT
def zero:char
def sizeMembuffer:INT
def runtimeName:string
'def byte:char



'open window
Window w1,0,0,500,400,@minbox,0,"MainGUI",main
Setwindowcolor w1,rgb(200,200,200)
CONTROL w1,"B,SaveExe,56,100,100,20,0,1"
'--------------------------------------------------------

'Load runtimeExe to memory
'ReadFile(1,runtimeexe)   ---> ';open runtime file
zero=0

IF (Openfile(runtimeExe,"C:\minGUI.exe","R") = 0)
    'entrypoint =0
    entrypoint = Len(runtimeExe) :'entrypoint=FileSize(runtimeexe)
Messagebox 0,"RuntimeLen(bytes):"+str$(entrypoint),"EXE SIZE"
    AllocMem membuffer,entrypoint,1
'pbuffer=NEW (INT,entrypoint)
Writemem membuffer,entrypoint,zero
'ReadData(1,*mRuntime,entrypoint)       :';load file into memory
CloseFile runtimeExe
ENDIF

sizeMembuffer=Len(membuffer)
Messagebox 0,"Memory buffer Len(INT):"+str$(sizeMemBuffer),"MEM BUFFER SIZE"
'Freemem membuffer
SetFileAttributesA("C:\newGUI.exe",FILE_ATTRIBUTE_ARCHIVE )

IF (Openfile(newExe,"C:\newGUI.exe","W") = 0)
AllocMem membuffer,entrypoint,1
Messagebox 0,"Memory buffer Len(INT):"+str$(Len(membuffer)),"MEM BUFFER SIZE"

Write newexe,membuffer
FreeMem membuffer
CloseFile newexe

ENDIF


'---------------------------------------------------------


run=1
waituntil run=0
'stoptimer w1
closewindow w1
end

'-------------------------------------------------------
Sub main
Select @class
Case @idclosewindow
run=0
Case @idcreate
Centerwindow w1
'/////////////////////////////////////////////////////////
case @IDCONTROL
IF @CONTROLID = 1
    writeNewexe()
ENDIF
'////////////////////////////////////////////////////////
Endselect
Return
'-------------------------------------------------------

SUB writeNewexe
SetFileAttributesA("C:\newGUI.exe",FILE_ATTRIBUTE_ARCHIVE )

IF (Openfile(newExe,"C:\newGUI.exe","W") = 0)
AllocMem membuffer,entrypoint,1
Messagebox 0,"Memory buffer Len(INT):"+str$(Len(membuffer)),"MEM BUFFER SIZE"

Write newexe,membuffer
FreeMem membuffer
CloseFile newexe

ENDIF
RETURN


Is anyonee have idea what i do wrong ?
Or maby is imposibile do on this way in CBasic?
Thanks advance
Zlatko

aurelCB

Currently i make program which can load exe to memory and also
load istring or file to memory then write to disk as only one exe.
' load Runtime to memory
SUB LoadRuntime
zero=0

IF (Openfile(runtimeExe,"C:\ABruntime.exe","R") = 0)
    'entrypoint =0
    entrypoint = Len(runtimeExe) :'entrypoint=FileSize(runtimeexe)
Messagebox 0,"RuntimeLen(bytes):"+str$(entrypoint),"EXE SIZE"

    AllocMem membuffer,entrypoint,1
READ(runtimeExe,membuffer)
Writemem membuffer,entrypoint,zero


CloseFile runtimeExe
ENDIF

RETURN

------------------------------------------------------------------------------
'load Source to memory
SUB LoadSource

zero=0

IF (Openfile(sourceCode,"C:\WriteFile.abp","R") = 0)
    'compilat =0
    compilat = Len(sourceCode) :'entrypoint=FileSize(runtimeexe)
Messagebox 0,"SourceLen(bytes):"+str$(compilat),"SOURCE CODE SIZE"

    AllocMem membuffer2,compilat,1
READ(sourceCode,membuffer2)
Writemem membuffer2,compilat,zero

CloseFile sourceCode
ENDIF


RETURN

---------------------------------------------------------------
'Create Exe / Write final exe to disk
SUB CreateExe

IF (Openfile(newExe,"C:\MRuntime.exe","W") = 0)

'AllocMem membuffer,entrypoint,1

Readmem membuffer,entrypoint,zero
Write (newexe,membuffer)

Readmem membuffer2,compilat,zero
Write (newexe,membuffer2)

'SetFileAttributesA("C:\newGUI.exe",FILE_ATTRIBUTE_ARCHIVE )
finalsize=Len(newexe)
CloseFile newexe
FreeMem membuffer
FreeMem membuffer2
ENDIF

Messagebox 0,"FinalLen(bytes):"+str$(finalsize),"BINDED EXE SIZE"
RETURN


So now i have problem .
How get name of currently runing this new exe?
Is there any API function which can get name of currently running exe file?
Of course this must work from inside of this exe,
so when you double click application and program run he must know what is name.

I know how get current directory name :
'Get directory name with dirsize=255(API function)
GetCurrentDirectoryA(255,dirname)

dir=FINDOPEN(dirname+"\*.abp") 

IF dir
filename = FINDNEXT(dir)
FINDCLOSE dir
Endif


Any idea....

Thanks advance
Zlatko

sapero

There is a documented one, returns the full path to given module:
declare "kernel32", GetModuleFileNameA(module:int, path:string, stringsize:int)
def path:istring[260]
GetModuleFileNameA(0, path, 260)


And another one for XP, 2000Pro, returns module name only:
declare "psapi", GetModuleBaseNameA(hProcess:int, module:int, path:string, stringsize:int)
def path:istring[260]
GetModuleBaseNameA(0, 0, path, 260)

aurelCB

Thank you Sapero,it works perfectly :)
I think that might work on this way but i forget right shape of this API function...

aurelCB

I'm stuck again.
As i say before I load ABruntime.exe in memory then read this memory block and write to disk with differnt name and this
thng work OK.
But when i load source code .abp file into another memory block- this step also work( im not sure,i guess).
So when i finaly try write second block on the already written exe,this dont work.
On the end i get final exe with smaller size insted of biger size (newsize=runtimsize+sourcesize)  ???
I dont understand what i do wrong?

here is code of binder:
Def w1:window
DECLARE "kernel32",SetFileAttributesA(lpFileName AS STRING,dwFileAttributes AS INT),INT
CONST FILE_ATTRIBUTE_ARCHIVE = 0x20
CONST FILE_ATTRIBUTE_NORMAL = 0x80
CONST FILE_EXECUTE = 0x20
'declare variables
def runtimeExe:BFILE
def sourceCode:FILE
def NewExe:BFILE
def membuffer:memory
def membuffer2:memory
'def pbuffer:pointer
def runtimesize:INT
def sourcesize:INT
def newsize:int
def zero:char
def sizeMembuffer:INT
def runtimeName:string
def sourceName:string
def finalSize:INT
'def byte:char



'open window
Window w1,0,0,500,400,@minbox,0,"MainGUI",main
Setwindowcolor w1,rgb(200,200,205)

CONTROL w1,"B,LoadRuntimeExe,56,50,120,20,0,1"
Setfont w1,"MS Sans Serif",8,400,0,1
CONTROL w1,"B,LoadSourceCode,56,100,120,20,0,2"
Setfont w1,"MS Sans Serif",8,400,0,2
CONTROL w1,"B,SaveBindedExe,56,150,120,20,0,3"
Setfont w1,"MS Sans Serif",8,400,0,3
'--------------------------------------------------------



'---------------------------------------------------------


run=1
waituntil run=0
'stoptimer w1
closewindow w1
end

'-------------------------------------------------------
Sub main
Select @class
Case @idclosewindow
run=0

'/////////////////////////////////////////////////////////
case @IDCONTROL

IF @CONTROLID = 1
    LoadRuntime()
ENDIF

IF @CONTROLID = 2
    LoadSource()
ENDIF

IF @CONTROLID = 3
    CreateExe()
ENDIF
'////////////////////////////////////////////////////////
Endselect
Return
'-------------------------------------------------------
' load Runtime to memory
SUB LoadRuntime
zero=0

IF (Openfile(runtimeExe,"C:\ABruntime.exe","R") = 0)
    'entrypoint =0
    runtimesize = Len(runtimeExe) :'runtime=FileSize(runtimeexe)
Messagebox 0,"RuntimeLen(bytes):"+str$(runtimesize),"RUNTIME SIZE"

    AllocMem membuffer,1,runtimesize
READ(runtimeExe,membuffer)
Writemem membuffer,runtimesize,zero

Messagebox 0,"MEMbuffersize(bytes):"+str$(Len(membuffer)),"R-MEMBUFFER SIZE"
CloseFile runtimeExe
ENDIF

RETURN

'-----------------------------------------------------
'load Source to memory
SUB LoadSource

zero=0

IF (Openfile(sourceCode,"C:\CaptionString.abp","R") = 0)
    sourcesize = 0
    sourcesize = Len(sourceCode) :'sourcesize=FileSize(sourcecode)
Messagebox 0,"SourceLen(bytes):"+str$(sourcesize),"SOURCE CODE SIZE"
newsize = runtimesize + sourcesize
Messagebox 0,"CompleteLen(bytes):"+str$(newsize),"COMPLETE-SIZE"

    AllocMem membuffer,1,newsize
READ sourceCode,membuffer,sourcesize
Writemem membuffer,sourcesize,newsize

Messagebox 0,"CompleteLen(bytes):"+str$(LEN(membuffer)),"MEMBUFFER-COMPLETE-SIZE"

CloseFile sourceCode
ENDIF


RETURN

'-----------------------------------------------------
'Create Exe / Write final exe to disk
SUB CreateExe

IF (Openfile(newExe,"C:\MRuntime.exe","W") = 0)

AllocMem membuffer,1,newsize

Readmem membuffer,newsize,zero
Messagebox 0,"CompleteLen(bytes):"+str$(LEN(membuffer)),"READ MEMBUFFER-SIZE"
'Readmem membuffer2,compilat,zero
Write newexe,membuffer,newsize

'Readmem membuffer2,compilat,zero
'SEEK newexe,entrypoint+1
'Write (newexe,membuffer2)

'SetFileAttributesA("C:\newGUI.exe",FILE_ATTRIBUTE_ARCHIVE )
finalsize=Len(newexe)
CloseFile newexe
FreeMem membuffer
'FreeMem membuffer2
ENDIF

Messagebox 0,"FinalLen(bytes):"+str$(finalsize),"BINDED EXE SIZE"
RETURN

Johnny

Hello Aurel,

I think you made it too complicated...
Just read both files in 2 buffers, and then write them both to the new file.
I tried this with 2 random files, and this seems to work out with my example files. Just try it out with your files.

Greetz,
Johnny


Def w1:window
DECLARE "kernel32",SetFileAttributesA(lpFileName AS STRING,dwFileAttributes AS INT),INT
CONST FILE_ATTRIBUTE_ARCHIVE = 0x20
CONST FILE_ATTRIBUTE_NORMAL = 0x80
CONST FILE_EXECUTE = 0x20
'declare variables
def runtimeExe:BFILE
def sourceCode:BFILE
def NewExe:BFILE
def membuffer:memory
def membuffer2:memory
'def pbuffer:pointer
def runtimesize:INT
def sourcesize:INT
def newsize:int
def zero:char
def sizeMembuffer:INT
def runtimeName:string
def sourceName:string
def finalSize:INT
'def byte:char



'open window
Window w1,0,0,500,400,@minbox,0,"MainGUI",main
Setwindowcolor w1,rgb(200,200,205)

CONTROL w1,"B,LoadRuntimeExe,56,50,120,20,0,1"
Setfont w1,"MS Sans Serif",8,400,0,1
CONTROL w1,"B,LoadSourceCode,56,100,120,20,0,2"
Setfont w1,"MS Sans Serif",8,400,0,2
CONTROL w1,"B,SaveBindedExe,56,150,120,20,0,3"
Setfont w1,"MS Sans Serif",8,400,0,3
'--------------------------------------------------------



'---------------------------------------------------------


run=1
waituntil run=0
'stoptimer w1

'Free the memory in case this was allocated before ending the program
IF Len(membuffer)>0 then FreeMem membuffer
IF Len(membuffer2)>0 then FreeMem membuffer2

closewindow w1
end

'-------------------------------------------------------
Sub main
Select @class
Case @idclosewindow
run=0

'/////////////////////////////////////////////////////////
case @IDCONTROL

IF @CONTROLID = 1
    LoadRuntime()
ENDIF

IF @CONTROLID = 2
    LoadSource()
ENDIF

IF @CONTROLID = 3
    CreateExe()
ENDIF
'////////////////////////////////////////////////////////
Endselect
Return
'-------------------------------------------------------
' load Runtime to memory
SUB LoadRuntime
zero=0

IF (Openfile(runtimeExe,"ABruntime.exe","R") = 0)
    runtimesize = Len(runtimeExe) :'runtime=FileSize(runtimeexe)
Messagebox 0,"RuntimeLen(bytes):"+str$(runtimesize),"RUNTIME SIZE"

    AllocMem membuffer,1,runtimesize
READ(runtimeExe,membuffer)

Messagebox 0,"MEMbuffersize(bytes):"+str$(Len(membuffer)),"R-MEMBUFFER SIZE"
CloseFile runtimeExe
ENDIF

RETURN

'-----------------------------------------------------
'load Source to memory
SUB LoadSource
zero=0

IF (Openfile(sourceCode,"CaptionString.abp","R") = 0)
    sourcesize = Len(sourceCode) :'sourcesize=FileSize(sourcecode)
Messagebox 0,"SourceLen(bytes):"+str$(sourcesize),"SOURCE CODE SIZE"

    AllocMem membuffer2,1,sourcesize
READ(sourceCode,membuffer2)

Messagebox 0,"CompleteLen(bytes):"+str$(LEN(membuffer2)),"MEMBUFFER-COMPLETE-SIZE"

CloseFile sourceCode
ENDIF


RETURN

'-----------------------------------------------------
'Create Exe / Write final exe to disk
SUB CreateExe
'Check first that both buffers are already filled with data before writing to a new file
IF runtimesize > 0 & sourcesize > 0
newsize = runtimesize + sourcesize
Messagebox 0,"CompleteLen(bytes):"+str$(newsize),"COMPLETE-SIZE"

IF (Openfile(newExe,"MRuntime.exe","W") = 0)

Write newexe,membuffer,membuffer2
finalsize=Seek(newexe)
CloseFile newexe

Messagebox 0,"FinalLen(bytes):"+str$(finalsize),"BINDED EXE SIZE"
ENDIF
ENDIF
RETURN

aurelCB

Hi Johnny ...
Of course i will try your way... ;)

Aurel

aurelCB

Hi again...
This is Johnny binder:
Def w1:window
DECLARE "kernel32",SetFileAttributesA(lpFileName AS STRING,dwFileAttributes AS INT),INT
CONST FILE_ATTRIBUTE_ARCHIVE = 0x20
CONST FILE_ATTRIBUTE_NORMAL = 0x80
CONST FILE_EXECUTE = 0x20
'declare variables
def runtimeExe:BFILE
def sourceCode:BFILE
def NewExe:BFILE
def membuffer:memory
def membuffer2:memory
'def pbuffer:pointer
def runtimesize:INT
def sourcesize:INT
def newsize:int
def zero:char
def sizeMembuffer:INT
def runtimeName:string
def sourceName:string
def finalSize:INT
def exesize:INT
'def byte:char



'open window
Window w1,0,0,500,400,@minbox,0,"MainGUI",main
Setwindowcolor w1,rgb(200,200,205)

CONTROL w1,"B,LoadRuntimeExe,56,50,120,20,0,1"
Setfont w1,"MS Sans Serif",8,400,0,1
CONTROL w1,"B,LoadSourceCode,56,100,120,20,0,2"
Setfont w1,"MS Sans Serif",8,400,0,2
CONTROL w1,"B,SaveBindedExe,56,150,120,20,0,3"
Setfont w1,"MS Sans Serif",8,400,0,3
'--------------------------------------------------------



'---------------------------------------------------------


run=1
waituntil run=0
'stoptimer w1

'Free the memory in case this was allocated before ending the program
IF Len(membuffer)>0 then FreeMem membuffer
IF Len(membuffer2)>0 then FreeMem membuffer2

closewindow w1
end

'-------------------------------------------------------
Sub main
Select @class
Case @idclosewindow
run=0

'/////////////////////////////////////////////////////////
case @IDCONTROL

IF @CONTROLID = 1
    LoadRuntime()
ENDIF

IF @CONTROLID = 2
    LoadSource()
ENDIF

IF @CONTROLID = 3
    CreateExe()
ENDIF
'////////////////////////////////////////////////////////
Endselect
Return
'-------------------------------------------------------
' load Runtime to memory
SUB LoadRuntime
zero=0

IF (Openfile(runtimeExe,"c:\minGUI.exe","R") = 0)
    runtimesize = Len(runtimeExe) :'runtime=FileSize(runtimeexe)
Messagebox 0,"RuntimeLen(bytes):"+str$(runtimesize),"RUNTIME SIZE"

    AllocMem membuffer,1,runtimesize
READ(runtimeExe,membuffer)

Messagebox 0,"MEMbuffersize(bytes):"+str$(len(membuffer)),"R-MEMBUFFER SIZE"
exesize=seek(runtimeExe)
Messagebox 0,"Runtime(bytes):"+str$(exesize),"R-MEMBUFFER SIZE"
CloseFile runtimeExe
ENDIF

RETURN

'-----------------------------------------------------
'load Source to memory
SUB LoadSource
zero=0

IF (Openfile(sourceCode,"c:\AWriteFile.abp","R") = 0)
    sourcesize = Len(sourceCode) :'sourcesize=FileSize(sourcecode)
Messagebox 0,"SourceLen(bytes):"+str$(sourcesize),"SOURCE CODE SIZE"

    AllocMem membuffer2,1,sourcesize
READ(sourceCode,membuffer2)

Messagebox 0,"CompleteLen(bytes):"+str$(LEN(membuffer2)),"MEMBUFFER-COMPLETE-SIZE"

CloseFile sourceCode
ENDIF


RETURN

'-----------------------------------------------------
'Create Exe / Write final exe to disk
SUB CreateExe
'Check first that both buffers are already filled with data before writing to a new file
IF runtimesize > 0 & sourcesize > 0
newsize = runtimesize + sourcesize
Messagebox 0,"CompleteLen(bytes):"+str$(newsize),"COMPLETE-SIZE"

IF (Openfile(newExe,"C:\MRuntime.exe","W") = 0)

Write newexe,membuffer,membuffer2
finalsize=Seek(newexe)
'Write newexe,membuffer2,finalsize
CloseFile newexe

Messagebox 0,"FinalLen(bytes):"+str$(finalsize),"BINDED EXE SIZE"
ENDIF
ENDIF
RETURN


And here is my testing app called 'minGUI' with richedit control.

'minimum GUI in Creative
DEF win:window
CONST ES_SUNKEN = &H4000
declare "kernel32", GetModuleFileNameA(module:int, path:string, stringsize:int)
Declare "user32", SetDlgItemTextA(win:int,id:int,text:memory)
def path[254]:string
'GetModuleFileNameA(0, path, 260)
def sourcesize:INT
def runtimesize:INT
def runtime:INT
runtime = 385024
def position:INT
def runtimeexe:BFILE
def sourcecode:INT
def membuffer2:memory
def zero:CHAR
Window win,0,0,500,400,@minbox,0,"miniGUI",main
Setwindowcolor win,rgb(220,220,230)

'open binded exe
IF (Openfile(runtimeexe,"c:\minGUI.exe","R") = 0)
    runtimesize = Len(runtimeexe) :'sourcesize=FileSize(sourcecode)
Messagebox 0,"EXELen(bytes):"+str$(runtimesize),"COMPLETE EXE SIZE"
'position = runtimesize - runtime
'Messagebox 0,"POSITION):"+str$(position),"POSITION"
position = runtime + 1
Messagebox 0,"POSITION+1:"+str$(position),"POSITION+1"
sourcesize = runtimesize - position
Messagebox 0,"SourceSize:"+str$(sourcesize),"SOURCE SIZE"

IF position > 385024
    AllocMem membuffer2,1,sourcesize
zero=0
Writemem membuffer,sourcesize,zero
READ(runtimeexe,membuffer2)

Messagebox 0,"CompleteLen(bytes):"+str$(LEN(membuffer2)),"MEMBUFFER-COMPLETE-SIZE"
SetDlgItemTextA(win,2024+1,membuffer2)
ENDIF

CloseFile runtimeexe
ENDIF

waituntil win=0
END
Sub main
select @class
case @idclosewindow
closewindow win
CASE @IDCREATE
CONTROL win,"RE,,10,20,400,300,@CTEDITMULTI|@CTEDITAUTOH|@hscroll|@vscroll|ES_SUNKEN,1"
endselect
Return


And here is .abp /ABasic file/-sourcecode, (ordinary text file with extension .abp) which is appended(binded) to minGUI.exe in new exe MRuntime.You can use any other text file. This file if all work will be written as text in richedit control in new exe.
' test file variable...
DEFFILE file1!
DEFSTR text$ filename$ flag$
LET text$ = This is a test
LET filename$ = C:\ABasicTest.txt
LET flag$ = W
WIN 0 0 400 300 NewWindow
WINCOLOR 230 230 235
WINFONT Microsoft 10

BUTTON 200 10 100 20 SYS 55
CTSETTEXT 55 Create New Folder

'save txt file on C disk with name ABasicTest
IFOPENFILE ( file1! filename$ flag$ ) = 0
WRITEFILE file1! text$
CLOSEFILE file1!

WAIT 0

SUBB 55
CreateFolder C:\AB_NewFolder
ENDSUB


Problem is in different size in memory and size on disk.
It seems that binder work becose new exe file is larger then first.
When you compile first minGUI.exe i click on property on this exe and sizes are:
Size : 385024
Size on disk: 385024
This result looks oK Roll Eyes
Then i run binder program and press buttons one by one.
After this i open C disk and there is new exe with name MRuntime.exe.
I click on property of this new exe and results are different:
Size : 385758    -is size which binder show as final size of new exe
Size on disk: 389120 bytes  - which is bigger for 3362  Huh
So is there a way to calculate real size on disk ?


Johnny

Hi Aurel,

This one proofs again that we can only see the problem when you send the whole (both) sources, only now I realize what the intension of the program is...  :-[
And don't bother about the difference between the file size and the size on disk, this is normal, because a hard disk is divided in sectors, and a file will occupy a number of sectors, plus a certain part of the last sector, unless the program by coincidence fills up that last sector completely, so there will be a difference between both sizes, and this is not really impotent.

OK, now that I have seen what your intension is, this one will do what you like:


'Binder.cba
'Sorry, I made the paths again relative in stead of absolute, else it doesn't work in my maps here...  ;o)

Def w1:window
DECLARE "kernel32",SetFileAttributesA(lpFileName AS STRING,dwFileAttributes AS INT),INT

CONST FILE_ATTRIBUTE_ARCHIVE = 0x20
CONST FILE_ATTRIBUTE_NORMAL = 0x80
CONST FILE_EXECUTE = 0x20
'declare variables
def runtimeExe:BFILE
def sourceCode:BFILE
def NewExe:BFILE
def membuffer:memory
def membuffer2:memory
'def pbuffer:pointer
def runtimesize:INT
def sourcesize:INT
def newsize:int
def zero:char
def sizeMembuffer:INT
def runtimeName:string
def sourceName:string
def finalSize:INT
def exesize:INT
'def byte:char



'open window
Window w1,0,0,500,400,@minbox,0,"MainGUI",main
Setwindowcolor w1,rgb(200,200,205)

CONTROL w1,"B,LoadRuntimeExe,56,50,120,20,0,1"
Setfont w1,"MS Sans Serif",8,400,0,1
CONTROL w1,"B,LoadSourceCode,56,100,120,20,0,2"
Setfont w1,"MS Sans Serif",8,400,0,2
CONTROL w1,"B,SaveBindedExe,56,150,120,20,0,3"
Setfont w1,"MS Sans Serif",8,400,0,3
'--------------------------------------------------------



'---------------------------------------------------------


run=1
waituntil run=0
'stoptimer w1

'Free the memory in case this was allocated before ending the program
IF Len(membuffer)>0 then FreeMem membuffer
IF Len(membuffer2)>0 then FreeMem membuffer2

closewindow w1
end

'-------------------------------------------------------
Sub main
Select @class
Case @idclosewindow
run=0

'/////////////////////////////////////////////////////////
case @IDCONTROL

IF @CONTROLID = 1
    LoadRuntime()
ENDIF

IF @CONTROLID = 2
    LoadSource()
ENDIF

IF @CONTROLID = 3
    CreateExe()
ENDIF
'////////////////////////////////////////////////////////
Endselect
Return
'-------------------------------------------------------
' load Runtime to memory
SUB LoadRuntime
zero=0

IF (Openfile(runtimeExe,"minGUI.exe","R") = 0)
    runtimesize = Len(runtimeExe) :'runtime=FileSize(runtimeexe)
Messagebox 0,"RuntimeLen(bytes):"+str$(runtimesize),"RUNTIME SIZE"

    AllocMem membuffer,1,runtimesize
READ(runtimeExe,membuffer)

Messagebox 0,"MEMbuffersize(bytes):"+str$(len(membuffer)),"R-MEMBUFFER SIZE"
exesize=seek(runtimeExe)
Messagebox 0,"Runtime(bytes):"+str$(exesize),"R-MEMBUFFER SIZE"
CloseFile runtimeExe
ENDIF

RETURN

'-----------------------------------------------------
'load Source to memory
SUB LoadSource
zero=0

IF (Openfile(sourceCode,"AWriteFile.abp","R") = 0)
    sourcesize = Len(sourceCode) :'sourcesize=FileSize(sourcecode)
Messagebox 0,"SourceLen(bytes):"+str$(sourcesize),"SOURCE CODE SIZE"

    AllocMem membuffer2,1,sourcesize
READ(sourceCode,membuffer2)

Messagebox 0,"CompleteLen(bytes):"+str$(LEN(membuffer2)),"MEMBUFFER-COMPLETE-SIZE"

CloseFile sourceCode
ENDIF


RETURN

'-----------------------------------------------------
'Create Exe / Write final exe to disk
SUB CreateExe
'Check first that both buffers are already filled with data before writing to a new file
IF runtimesize > 0 & sourcesize > 0
newsize = runtimesize + sourcesize
Messagebox 0,"CompleteLen(bytes):"+str$(newsize),"COMPLETE-SIZE"

IF (Openfile(newExe,"MRuntime.exe","W") = 0)

Write newexe,membuffer,membuffer2,sourcesize
finalsize=Seek(newexe)
'Write newexe,membuffer2,finalsize
CloseFile newexe

Messagebox 0,"FinalLen(bytes):"+str$(finalsize),"BINDED EXE SIZE"
ENDIF
ENDIF
RETURN



'minGui.cba - minimum GUI in Creative
'This version finds it's own name back, as suggested by Sapero

DEF win:window
CONST ES_SUNKEN = &H4000
declare "kernel32", GetModuleFileNameA(module:int, path:string, stringsize:int)
Declare "user32", SetDlgItemTextA(win:int,id:int,text:memory)
def path[500]:istring
'GetModuleFileNameA(0, path, 260)
def sourcesize:INT
def runtimesize:INT
def runtime:INT
runtime = 385024
def position:INT
def runtimeexe:BFILE
def sourcecode:INT
def membuffer2:memory
def zero:CHAR
Window win,0,0,500,400,@minbox,0,"miniGUI",main
Setwindowcolor win,rgb(220,220,230)

'open binded exe
GetModuleFileNameA(0, path, 260)
Messagebox 0,"Path to file:"+path,"File path"


IF (Openfile(runtimeexe,path,"R") = 0)
    runtimesize = Len(runtimeexe) :'sourcesize=FileSize(sourcecode)
Seek runtimeexe,runtimesize - 4
read runtimeexe,sourcesize

Messagebox 0,"EXELen(bytes):"+str$(runtimesize),"COMPLETE EXE SIZE"
Messagebox 0,"SourceSize:"+str$(sourcesize),"SOURCE SIZE"
position = runtimesize - sourcesize - 4
Messagebox 0,"POSITION:"+str$(position),"POSITION"

IF position > 0
    AllocMem membuffer2,1,sourcesize
' zero=0
' Writemem membuffer,sourcesize,zero
Seek runtimeexe,position

READ(runtimeexe,membuffer2)

Messagebox 0,"CompleteLen(bytes):"+str$(LEN(membuffer2)),"MEMBUFFER-COMPLETE-SIZE"
SetDlgItemTextA(win,2024+1,membuffer2)
ENDIF

CloseFile runtimeexe
ENDIF

waituntil win=0
END
Sub main
select @class
case @idclosewindow
closewindow win
CASE @IDCREATE
CONTROL win,"RE,,10,20,400,300,@CTEDITMULTI|@CTEDITAUTOH|@hscroll|@vscroll|ES_SUNKEN,1"
endselect
Return



aurelCB

Thanks you again Johnny :)
You put me in right direction,I almost mean that is not posibile!
I will try your example of course.
After study all this things i find little bit different solution then your but finaly work.
Yes you right about 'size' and 'size on disk',important is 'size'.
Im stuck becose i dont use properly position of source.
And yes your binder works fine and is simpliest then mine.
I see that you increse size of path and its ok becose file can be anywhere in folder with subfolder etc...
So this is your binder:

'Exe binder ,Thanks to Johnny ....
Def w1:window
DECLARE "kernel32",SetFileAttributesA(lpFileName AS STRING,dwFileAttributes AS INT),INT
CONST FILE_ATTRIBUTE_ARCHIVE = 0x20
CONST FILE_ATTRIBUTE_NORMAL = 0x80
CONST FILE_EXECUTE = 0x20
'declare variables
def runtimeExe:BFILE
def sourceCode:BFILE
def NewExe:BFILE
def membuffer:memory
def membuffer2:memory
def runtimesize:INT
def sourcesize:INT
def newsize:int
def zero:char
def sizeMembuffer:INT
def runtimeName:string
def sourceName:string
def finalSize:INT

'open window
Window w1,0,0,500,400,@minbox,0,"MainGUI",main
Setwindowcolor w1,rgb(200,200,205)

CONTROL w1,"B,LoadRuntimeExe,56,50,120,20,0,1"
Setfont w1,"MS Sans Serif",8,400,0,1
CONTROL w1,"B,LoadSourceCode,56,100,120,20,0,2"
Setfont w1,"MS Sans Serif",8,400,0,2
CONTROL w1,"B,SaveBindedExe,56,150,120,20,0,3"
Setfont w1,"MS Sans Serif",8,400,0,3
'--------------------------------------------------------
'--------------------------------------------------------
run=1
waituntil run=0
'stoptimer w1

'Free the memory in case this was allocated before ending the program
IF Len(membuffer)>0 then FreeMem membuffer
IF Len(membuffer2)>0 then FreeMem membuffer2

closewindow w1
end

'-------------------------------------------------------
Sub main
Select @class
Case @idclosewindow
run=0

'/////////////////////////////////////////////////////////
case @IDCONTROL

IF @CONTROLID = 1
    LoadRuntime()
ENDIF

IF @CONTROLID = 2
    LoadSource()
ENDIF

IF @CONTROLID = 3
    CreateExe()
ENDIF
'////////////////////////////////////////////////////////
Endselect
Return
'-------------------------------------------------------
' load Runtime to memory
SUB LoadRuntime
zero=0

IF (Openfile(runtimeExe,"C:\minGUI.exe","R") = 0)
    runtimesize = Len(runtimeExe) :'runtime=FileSize(runtimeexe)
Messagebox 0,"RuntimeLen(bytes):"+str$(runtimesize),"RUNTIME SIZE"

    AllocMem membuffer,1,runtimesize
READ(runtimeExe,membuffer)

Messagebox 0,"MEMbuffersize(bytes):"+str$(Len(membuffer)),"R-MEMBUFFER SIZE"
CloseFile runtimeExe
ENDIF

RETURN

'-----------------------------------------------------
'load Source to memory
SUB LoadSource
zero=0

IF (Openfile(sourceCode,"C:\CaptionString.abp","R") = 0)
    sourcesize = Len(sourceCode) :'sourcesize=FileSize(sourcecode)
Messagebox 0,"SourceLen(bytes):"+str$(sourcesize),"SOURCE CODE SIZE"

    AllocMem membuffer2,1,sourcesize
READ(sourceCode,membuffer2)

Messagebox 0,"CompleteLen(bytes):"+str$(LEN(membuffer2)),"MEMBUFFER-COMPLETE-SIZE"

CloseFile sourceCode
ENDIF


RETURN

'-----------------------------------------------------
'Create Exe / Write final exe to disk
SUB CreateExe
'Check first that both buffers are already filled with data before writing to a new file
IF runtimesize > 0 & sourcesize > 0
newsize = runtimesize + sourcesize
Messagebox 0,"CompleteLen(bytes):"+str$(newsize),"COMPLETE-SIZE"

IF (Openfile(newExe,"C:\MRuntime.exe","W") = 0)

Write newexe,membuffer,membuffer2
finalsize=Seek(newexe)
CloseFile newexe

Messagebox 0,"FinalLen(bytes):"+str$(finalsize),"BINDED EXE SIZE"
ENDIF
ENDIF
RETURN


And here is mine runtime.exe(minGUI.exe):

'runtime program
Def w1:window
declare "kernel32", GetModuleFileNameA(module:int, path:string, stringsize:int)
def path[254]:string
Declare "user32", SetDlgItemTextA(win:int,id:int,text:memory)
def sourcecode:BFILE
def execode:BFILE
def sourcesize:INT
def exesize:INT
def runtimesize:INT
runtimesize=385024
def position:INT
def membuffer2:memory
def zero:char
'open window
Window w1,0,0,500,400,@minbox,0,"MainGUI",main
Setwindowcolor w1,rgb(200,200,200)
'--------------------------------------------------------
GetModuleFileNameA(0, path, 254)

IF (Openfile(exeCode,path,"R") = 0)
    exesize = LEN(exeCode)
Messagebox 0,"RuntimeLen(bytes):"+str$(exesize),"EXE SIZE"

position=runtimesize+1

sourcesize = exesize - runtimesize
Messagebox 0,"SOURCELen(bytes):"+str$(sourcesize),"SOURCE SIZE"

IF  sourcesize > 0
'seek position of source in exe
SEEK execode,position
    AllocMem membuffer2,1,sourcesize
READ execode,membuffer2

Messagebox 0,"CompleteLen(bytes):"+str$(LEN(membuffer2)),"MEMBUFFER-COMPLETE-SIZE"
'load code to richedit
SetDlgItemTextA(w1,2024+1,membuffer2)
ELSE
Messagebox 0,"Source code not exists->"+str$(sourcesize),"SOURCE SIZE"
ENDIF

CloseFile exeCode
IF Len(membuffer2)>0 Then Freemem membuffer2
ENDIF
'--------------------------------------------------------
run=1
waituntil run=0
'stoptimer w1
closewindow w1
end

'-------------------------------------------------------
Sub main
Select @class
Case @idclosewindow
run=0
Case @idcreate
Centerwindow w1
CONTROL w1,"RE,,10,20,400,300,@CTEDITMULTI|@CTEDITAUTOH|@hscroll|@vscroll,1"
Setfont w1,"courier new",10,40,0,1
Endselect
Return

//Compile this code to C:\minGUI.exe.

There is ABasic file:
'window caption as string
DEFSTR win$
LET win$ = Window caption written as string variable

' create window
WIN 0 0 450 400 win$
' set window color
WINCOLOR 230 230 0
' set window font
WINFONT Verdana 14

' create button
BUTTON 10 10 100 24 SYS 51
'set button text
CTSETTEXT 51 Show caption string

'wait for events
WAIT 0

SUBB 51
COLOR 200 0 0
PRINT 10 50 win$
COLOR 0 0 200
TEXT 10 100 Caption name written as string
ENDSUB


//Save this ABasic file on C as C:\CaptionString.abp

Then...
Johnny binder you can run from CB IDE.
After this look in C disk.
New exe is created - MRuntime.exe.
Run this new exe.
After few messages you will see source code in richedit control.
Once again thank you guys... :)

Aurel