B4J Question Create a Edit Resource

Discussion in 'B4J Questions' started by klingon467, Jun 25, 2015.

  1. klingon467

    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:
    Code:
    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 StringAs 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 StringAs 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)

    Code:
    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 StringAs 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Yes, it is possible.
     
  3. klingon467

    klingon467 Member Licensed User

    do you have any examples? :D
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    What exactly do you need to do?
     
  5. klingon467

    klingon467 Member Licensed User

    open resource as binary
    write data into resource file
    read data and split it as example...
    get file
    put data
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You can use RandomAccessFile to read and write bytes to a file.
     
  7. klingon467

    klingon467 Member Licensed User

    Hi @Erel
    i try it! but i have a exception

    Code:
    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:
    i want write a string into my apk...
    in EOF or in a specific position
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    You need to use WriteBytes instead of WriteByte.
     
    klingon467 likes this.
  9. klingon467

    klingon467 Member Licensed User

    Work it!
    thanks @Erel

    write my 2 strings "c" and "9" in EOF data as example
     

    Attached Files:

  10. klingon467

    klingon467 Member Licensed User

    @Erel sorry i have a small problem on readbytes
    my code:
    Code:
    #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(4As 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
     

    Attached Files:

  11. Erel

    Erel Administrator Staff Member Licensed User

    I'm sorry but I do not understand the question.
     
  12. klingon467

    klingon467 Member Licensed User

    I try to explain better... sorry for my bad english :(

    with this write the on position 8850 from textFiled for example "3580"
    Code:
    raf.WriteBytes(wportB, 0,  wportB.Length, 8850)
    for read bytes on position 8850 from 0 to lenght
    Code:
    Dim lportB(4As 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.
    Code:
    Public Sub lunghezza
    Dim buffer(48As 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: Jul 8, 2015
  13. Erel

    Erel Administrator Staff Member Licensed User

    Are you sure that you should write text there instead of an encoded number? raf.WriteInt for example?
     
  14. klingon467

    klingon467 Member Licensed User

    This works perfectly!
    Code:
    #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(48As 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
     
  15. klingon467

    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
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    You cannot modify an APK file without signing it again (with jarsigner).
     
  17. klingon467

    klingon467 Member Licensed User

    exist a function for write in the bottom file?
    I have tried to manually with hexWorkshop to enter data at the bottom and works APK
     
  18. klingon467

    klingon467 Member Licensed User

    trying to modify the function in this way
    Code:
    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...
     

    Attached Files:

    • azz!.JPG
      azz!.JPG
      File size:
      111.9 KB
      Views:
      15
  19. Erel

    Erel Administrator Staff Member Licensed User

    The last parameter is the position.
    Code:
    raf.CurrentPosition = raf.Size
    raf.WriteBytes(wportB, 
    0, wportB.Length, raf.CurrentPosition)'write the port + split
    raf.WriteBytes(whostB, 0, whostB.Length, raf.CurrentPosition)'write the hostname + split
     
Loading...