EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Problem to write file

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
#16571
Posted: 06/07/2011 09:28:23
by HILAIRE Nicolas (Standard support level)
Joined: 06/07/2011
Posts: 5

I would like to evaluate your software before buy it, so it try to make a sample application which just read 512 first bytes of a logical drive and write a byte in the first part of the data.

Here is the part of my C# code and attached is the full solution.

The read function which work perfectly :

Code
IntPtr ptr = RawDisk.CRawDisk.Open(driveAccess, unchecked((int)0xC0000000), @"XXX");
            if (ptr != IntPtr.Zero)
            {
                SetFilePointer(ptr, 0, 0, 0);

                textBox1.Text = "";

                byte[] inp = new byte[512];
                uint read;
                ReadFile(ptr, inp, 512, out read, 0);
                for (int i = 0; i < 32; i++)
                {
                    for (int j = 0; j < 16; j++)
                        textBox1.Text += inp[i * 16 + j].ToString("X2") + " ";
                    textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1) + Environment.NewLine;
                }

                CloseHandle(ptr);
            }


The write function which always send an error :
Code

bool failed = false;
            IntPtr ptr = RawDisk.CRawDisk.OpenEx(driveAccess, unchecked((int)0xC0000000), @"XXXX", true, ref failed);
            if (failed)
            {
                MessageBox.Show("Failed to unmount");
            }

            SetFilePointer(ptr, 0xE0, 0, 0);
            byte[] inp = new byte[512];
            inp[0] = 14;
            uint written;
            if (!WriteFile(ptr, inp, 1, out written, 0))
            {
                MessageBox.Show("Error n°" + Marshal.GetLastWin32Error());
            }

            CloseHandle(ptr);


Could you explain me why the second part don't work ?
#16572
Posted: 06/07/2011 09:59:18
by Volodymyr Zinin (EldoS Corp.)

The rules for ReadFile and WriteFile with the handle which is opened by means of CRawDisk.Open/CRawDisk.OpenEx are the following:
- File access must begin at byte offsets within a file that are integer multiples of the volume sector size.
- File access must be for numbers of bytes that are integer multiples of the volume sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1024, 1536, or 2048 bytes, but not of 335, 981, or 7171 bytes.
- Buffer addresses for read and write operations should be sector aligned, which means aligned on addresses in memory that are integer multiples of the volume sector size. Depending on the disk, this requirement may not be enforced.

Actually they are the same as specified for the system CreateFile API with the flag FILE_FLAG_NO_BUFFERING.
#16578
Posted: 06/08/2011 02:19:04
by HILAIRE Nicolas (Standard support level)
Joined: 06/07/2011
Posts: 5

Thank you for your answer. I follow your recommandations and I read the first 512 bytes of the card, make a littel modification and rewrite this sector.
The write function return success !
But the card hasn't been changed at all !

Here is my new write function
Code
            bool failed = false;
            IntPtr ptr = RawDisk.CRawDisk.OpenEx(driveAccess, unchecked((int)0xC0000000), @"XXXX", true, ref failed);
            if (failed)
            {
                MessageBox.Show("Failed to unmount");
            }

            SetFilePointer(ptr, 0, 0, 0);
            byte[] inp = new byte[512];
            uint read;
            ReadFile(ptr, inp, 512, out read, 0);
            if (read == 512)
            {
                inp[0xE0] = 14;
                uint written;
                if (!WriteFile(ptr, inp, 512, out written, 0))
                {

                    MessageBox.Show("Error n°" + Marshal.GetLastWin32Error());
                }
            }

            CloseHandle(ptr);
#16579
Posted: 06/08/2011 03:09:18
by Volodymyr Zinin (EldoS Corp.)

Please specify the device name you are opening.
Thanks.
#16580
Posted: 06/08/2011 04:15:21
by HILAIRE Nicolas (Standard support level)
Joined: 06/07/2011
Posts: 5

Sorry I forgot the const :
Code
private string driveAccess = @"\??\F:";
#16581
Posted: 06/08/2011 04:54:26
by Eugene Mayevski (EldoS Corp.)

Quote
HILAIRE Nicolas wrote:
The write function return success ! But the card hasn't been changed at all !


How do you know this? You need to re-mount the device by removing it and adding it back to have the system re-read first sectors (where partition or file system information is stored).


Sincerely yours
Eugene Mayevski
#16582
Posted: 06/08/2011 05:02:38
by HILAIRE Nicolas (Standard support level)
Joined: 06/07/2011
Posts: 5

I effectively re-mount my device by disconnecting the drive with Windows, unplug and replug my SD card.
And i relaunch my first method to read first sector.
I also read it with a software which can read a drive : 010 Editor.
And the card hasn't been modifyed.
#16583
Posted: 06/08/2011 05:29:39
by Volodymyr Zinin (EldoS Corp.)

Try to call SetFilePointer(ptr, 0, 0, 0) before the write call.
#16584
Posted: 06/08/2011 05:56:37
by HILAIRE Nicolas (Standard support level)
Joined: 06/07/2011
Posts: 5

Thank you effectively I forgot to reset my pointer !!
Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.

Reply

Statistics

Topic viewed 6806 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!