I'm trying to Write to my flash memory,, I've read the firts 512 bytes, modified that array changing some bytes for others and then using WriteFile API function, wrote (or tried) again those 512 first bytes into my memory again.. this WriteFile API function return 1 , after that I use CloseHandle API function closing the handle I used to interact with the memory...but, when I see my memory using WinHex(hex editor) the bytes there are the same before my program tries to write. why is this if I am even closing the handle and after close it my buffer(array of bytes read from my memory) seems modified.
My code:
[code=c#]
SafeFileHandle ptrFile = CreateFile("\\\ \.\\u:", DesiredAccess.G ENERIC_READ | DesiredAccess.G ENERIC_WRITE, ShareMode.FILE_ SHARE_READ_AND_ WRITE, IntPtr.Zero, CreationDisposi tion.OPEN_EXIST ING, FlagsAndAttribu tes.FILE_ATTRIB UTE_NORMAL, IntPtr.Zero);
byte[] buffer = new byte[512];
uint cantleidos = 0;
try
{
bool retor = SetFilePointerE x(ptrFile, 5, IntPtr.Zero, MoveMethod.FILE _BEGIN);
bool b = ReadFile(ptrFil e, buffer,512 , out cantleidos, IntPtr.Zero);
}
catch (Exception)
{
throw new Win32Exception( Marshal.GetLast Win32Error());
}
Buffer.BlockCop y(System.Text.E ncoding.UTF8.Ge tBytes("SOMETEX T".ToCharArray( )), 0, buffer,3,"SOMET EXT".ToCharArra y().Length);
uint wrote = 0;
bool c = WriteFile(ptrFi le, buffer, (uint)buffer.Le ngth, ref wrote, IntPtr.Zero);
int closed = CloseHandle(ptr File);
[/code]
My code:
[code=c#]
SafeFileHandle ptrFile = CreateFile("\\\ \.\\u:", DesiredAccess.G ENERIC_READ | DesiredAccess.G ENERIC_WRITE, ShareMode.FILE_ SHARE_READ_AND_ WRITE, IntPtr.Zero, CreationDisposi tion.OPEN_EXIST ING, FlagsAndAttribu tes.FILE_ATTRIB UTE_NORMAL, IntPtr.Zero);
byte[] buffer = new byte[512];
uint cantleidos = 0;
try
{
bool retor = SetFilePointerE x(ptrFile, 5, IntPtr.Zero, MoveMethod.FILE _BEGIN);
bool b = ReadFile(ptrFil e, buffer,512 , out cantleidos, IntPtr.Zero);
}
catch (Exception)
{
throw new Win32Exception( Marshal.GetLast Win32Error());
}
Buffer.BlockCop y(System.Text.E ncoding.UTF8.Ge tBytes("SOMETEX T".ToCharArray( )), 0, buffer,3,"SOMET EXT".ToCharArra y().Length);
uint wrote = 0;
bool c = WriteFile(ptrFi le, buffer, (uint)buffer.Le ngth, ref wrote, IntPtr.Zero);
int closed = CloseHandle(ptr File);
[/code]
Comment