B4J Question Create a Edit Resource

klingon467

Member
Licensed User
Hi at all,
i want to create a editor for my resource....
it is possible with B4J?

I write a simple example to understand better in vb6

very simple editor:
B4X:
Private Sub Command1_Click()
Dim myStub As String
Dim ResourceFile As String
Dim ResultFile As String
Dim message As String

message = "Hello Word!"


myStub = App.Path & "\stub.exe" 'my resource
ResourceFile = LoadFile(myStub) 'read my resource


ResultFile = App.Path & "\result.exe" 'final results

'write data into resource file
Call WriteFile(ResultFile, ResourceFile & "####" & message & "####")

MsgBox ("Done")
End Sub
Function LoadFile(PathToFile As String) As String 'simple Function to Read the Data of the File
Open PathToFile For Binary As #1
LoadFile = Space(LOF(1))
Get #1, , LoadFile
Close #1
End Function
Function WriteFile(PathToWFile As String, WriteData As String) As String 'simple Function to write the Data in a File
Open PathToWFile For Binary As #2
Put #2, , WriteData
Close #2
End Function
Resource File (Stub)

B4X:
Private Sub Form_Load()
Dim Datos As String
Dim sData() As String

Datos = LoadFile(App.Path & "\" & App.EXEName & ".exe")

sData = Split(Datos, "####") 'split data

MsgBox (sData(1))
Unload Me

End Sub
Function LoadFile(PathToFile As String) As String 'simple Function to Read the Data of the File
Open PathToFile For Binary As #1
LoadFile = Space(LOF(1))
Get #1, , LoadFile
Close #1
End Function
This is possible in B4J???

thanks
 

klingon467

Member
Licensed User
You can use RandomAccessFile to read and write bytes to a file.
Hi @Erel
i try it! but i have a exception

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim raf As RandomAccessFile
    Private btnWrite As Button
    Dim txtHost As TextField
    Dim txtPort As TextField

   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show
    raf.Initialize(File.DirApp, "myar.apk", False)
End Sub
Public Sub Write (host As String, port As String)
Dim hostB() As Byte
Dim portB() As Byte
hostB = host.GetBytes("UTF8")
portB = port.GetBytes("UTF8")
raf.WriteByte(hostB, 326290)
raf.WriteByte(portB, 326291)
End Sub

Sub btnWrite_Action
Try
    Dim host As String = "c"
    Dim port As String = "9"
    Write(host, port)
    Catch
    Log(LastException.Message)
    End Try
End Sub
not run debug i receive this:
B4J version 2.80
Parsing code. 0.00
Compiling code. 0.02
Compiling generated Java code. Error
B4J line: 28
raf.WriteByte(hostB, 326290)
javac 1.7.0_67
src\b4j\example\main.java:126: error: inconvertible types
_raf.WriteByte((byte)(_hostb),(long) (326290));
^
required: byte
found: byte[]
1 error
i want write a string into my apk...
in EOF or in a specific position
 

klingon467

Member
Licensed User
@Erel sorry i have a small problem on readbytes
my code:
B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim raf As RandomAccessFile
    Private btnWrite As Button
    Private btnRead As Button
    Dim txtHost As TextField
    Dim txtPort As TextField
Dim MsgBox As Msgboxes
    Private btnRead As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show
    raf.Initialize(File.DirApp, "myar.apk", False)
End Sub
Public Sub Write (whost As String, wport As String)
Dim whostB() As Byte
Dim wportB() As Byte

whostB = whost.GetBytes("UTF8")
wportB = wport.GetBytes("UTF8")

raf.WriteBytes(wportB, 0,  wportB.Length, 8850)'write the port + split on position 8850
raf.WriteBytes(whostB, 0, whostB.Length, 8858)'write the hostname + split on position 8858

End Sub
Public Sub Read
Try
Dim lportB(4) As Byte
Dim lport As String
raf.ReadBytes(lportB, 0, lportB.Length, 8850)'read bytes on position 8850 from 0 to lenght
lport = BytesToString(lportB, 0, lportB.Length, "UTF8")
Log(raf.CurrentPosition)
Log(lport)
    Catch
    Log(LastException.Message)
    End Try
End Sub

Sub btnWrite_Action
Try
Dim host As String
Dim port As String
host = txtHost.Text & "SPLT"
port = txtPort.Text & "SPLT"
    Write(host, port)
    MsgBox.Show("Done!","Info")
    Catch
    Log(LastException.Message)
    End Try
End Sub
Sub btnRead_Action
    Read
End Sub
now if use Dim lportB(4) As Byte i read port value (because are 4 byte)
but if i have value varible i must use regex.Split?

attach a screenshot on write offset
thanks
 

Attachments

klingon467

Member
Licensed User
I'm sorry but I do not understand the question.
I try to explain better... sorry for my bad english :(

with this write the on position 8850 from textFiled for example "3580"
B4X:
raf.WriteBytes(wportB, 0,  wportB.Length, 8850)
for read bytes on position 8850 from 0 to lenght
B4X:
Dim lportB(4) As Byte
Dim lport As String
raf.ReadBytes(lportB, 0, lportB.Length, 8850)
lport = BytesToString(lportB, 0, lportB.Length, "UTF8")
log(lport)
and is ok! work perfect!
but if i insert a value longer or shorter can not read well the value because i use 4 byte as lenght....
You should first check the length of bytes to be read and then pass it to the event raf.ReadBytes
how could I do?

----------------------------------
my approach.
B4X:
Public Sub lunghezza
Dim buffer(48) As Byte
Dim msg As String
Dim opt() As String

For i = 0 To buffer.Length
raf.ReadBytes(buffer, 0, buffer.Length, 8850)
msg = BytesToString(buffer, 0, buffer.Length, "UTF8")
opt = Regex.Split("SPLT", msg)

Next
Log(opt(1))
End Sub
ok works well! @Erel
 
Last edited:

klingon467

Member
Licensed User
Are you sure that you should write text there instead of an encoded number? raf.WriteInt for example?
This works perfectly!
B4X:
#Region  Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim raf As RandomAccessFile
    Private btnWrite As Button
    Private btnRead As Button
    Dim txtHost As TextField
    Dim txtPort As TextField
Dim MsgBox As Msgboxes
    Private btnRead As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show
    raf.Initialize(File.DirApp, "myar.apk", False)
End Sub
Public Sub Write (whost As String, wport As String)
Dim whostB() As Byte
Dim wportB() As Byte

whostB = whost.GetBytes("UTF8")
wportB = wport.GetBytes("UTF8")

raf.WriteBytes(wportB, 0,  wportB.Length, 8850)'write the port + split on position 8850
raf.WriteBytes(whostB, 0, whostB.Length, 8858)'write the hostname + split on position 8858

End Sub
Sub btnWrite_Action
Try
Dim host As String
Dim port As String
host = txtHost.Text & "SPLT"
port = txtPort.Text & "SPLT"
    Write(host, port)
    MsgBox.Show("Done!","Info")
    Catch
    Log(LastException.Message)
    End Try
End Sub
Sub btnRead_Action
    Read
End Sub
Public Sub Read
Dim buffer(48) As Byte
Dim msg As String
Dim opt() As String

For i = 0 To buffer.Length
raf.ReadBytes(buffer, 0, buffer.Length, 8850)
msg = BytesToString(buffer, 0, buffer.Length, "UTF8")
opt = Regex.Split("SPLT", msg)

Next
Log(opt(0)) 'read port
Log(opt(1)) 'read host
End Sub
 

klingon467

Member
Licensed User
hi @Erel
I tried to write in the file apk but is corrupt......:(
how can I write in the bottom of the apk file? in practice I should add bytes in the bottom (EOF) I noticed that the apk in this way worked fine
 

klingon467

Member
Licensed User
trying to modify the function in this way
B4X:
Public Sub Write (whost As String, wport As String)
Dim whostB() As Byte
Dim wportB() As Byte

whostB = whost.GetBytes("UTF8")
wportB = wport.GetBytes("UTF8")

raf.WriteBytes(wportB, 0,  wportB.Length, wportB.Length)'write the port + split
raf.WriteBytes(whostB, 0, whostB.Length, wportB.Length + 8)'write the hostname + split
End Sub
He writes the head of the file...
 

Attachments

Top