Overwrite bytes?

  • david

    david - 2011-01-07

    Is there any method to overwrite existing bytes when using the insert byte method? For example, if I use this:

    hexBox.ByteProvider.InsertBytes(address, _byte);

    Instead of overwriting bytes, it will append more bytes. How can I overwrite them instead? Is there any function for that?

  • david

    david - 2011-01-07

    Also, I didn't want to make a new discussion for this, but I noticed a small problem:

    If you open a file in this hex editor and a file in some other program, you can't save changes to the other program. When you close the hex-editor, you can read and write to that program.

    Many hex-editors don't do this, but I just noticed the problem is with this one. Would you know of a good way to fix this problem? Please suggest a way to fix it, thanks.

  • Bernhard Elbl

    Bernhard Elbl - 2011-01-07

    Overwrite a byte
    Use ByteProvider.WriteByte method to (over)write a byte. To update the view of the control call HexBox.Invalidate method

    File Lock
    In the DynamicByteProvider class change the constructor. Changing the FileShare argument in File.Open line to ReadWrite should solve this.


  • david

    david - 2011-01-08

    For overwriting bytes, I used a loop with the WriteByte method which you said and called the Invalidate method, it works great, thanks.

    I also tried what you said about FileShare, but it didn't work. Also, the File.Open method was not in the DynamicByteProvider class, it was in the DynamicFileByteProvider and FileByteProvider class. So I changed them to this:

                if (!readOnly)
                    _stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                    _stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

    But it still doesn't work. Did I miss out anything or do something wrong? Could you look into this please?


  • david

    david - 2011-01-08

    Hm, actually it seems to work if I first open the file in the other program and then in Be.HexEditor. So it might be a problem with the other program. I guess it's still alright.

    Thanks for your help!

  • david

    david - 2011-01-09

    OK, so apparently not.

    It only seems to work when FileShare is set to read. When I set it to write, I still couldn't edit the file with the other program, but with Read only, it works. ReadWrite also doesn't work.  Why is this?

    Can you please look into it? In the meantime I am trying to find the problem..

  • Bernhard Elbl

    Bernhard Elbl - 2011-01-10

    The difference with other editor is that Be.HexEditor is using DynamicFileByteProvider and does not fully load the file into the memory. It does not release the file handle until the file is closed. This is the reason why my editor can show every file size. So to solve this, there might be two options:
    1. Add an options to open the file in read only mode
    2. Add an options to fully load the file into memory (Using DynamicByteProvider and not DynamicFileByteProvider)

    I really don't know why other software is ignoring the FileShare.ReadWrite setting.

  • david

    david - 2011-01-15

    Sorry If I'm bugging you, but have you tested this? I can't release my program because of this bug and it's been 5 days since I'm trying to diagnose the problem :P

    "1. Add an options to open the file in read only mode"
    In Be.HexEditor? If so, how can I save the file?

    "2. Add an options to fully load the file into memory (Using DynamicByteProvider and not DynamicFileByteProvider)"
    The problem with this is that it uses more RAM, requires more unnecessary I/O and wipes out any changes you've made in the other program once you've saved it.

    I asked on Stack Overflow, but I still couldn't find a fix for this. Please help..

  • Bernhard Elbl

    Bernhard Elbl - 2011-01-17

    I'm sorry. Editing a file with Be.HexEditor and another tool at the same time is not supported. Maybe this will damage the file, especially when using DynamicFileByteProvider.

    Reason: DynamicFileByteProvider is collecting changes in memory and reflect the changes to the file on a file save event. If another tool is changes the file in the meantime reflecting the changes could fail and damage the file.

  • david

    david - 2011-01-18

    Oh, I see. Thanks for the info.

    Do you think there is a way to overcome this problem?

  • Bernhard Elbl

    Bernhard Elbl - 2011-01-18

    Maybe using FileByteProvider could work with some limitations:
    - FileByteProvider can not Insert or Delete bytes
    - The locking message rised in other tools will also happen. Wheras using .NET Code you can open read and write a file twice with FileShare.ReadWrite setting. Other tools will show the "process can not access the file" message. I have no solution to this and I think it's a problem of the other tool.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks