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.
The number of bytes written are actually 512.. this is my code ... this is just testing code.
SafeFileHandle ptrFile = CreateFile("\\\\.\\u:", DesiredAccess.GENERIC_READ | DesiredAccess.GENERIC_WRITE, ShareMode.FILE_SHARE_READ_AND_WRITE, IntPtr.Zero, CreationDisposition.OPEN_EXISTING, FlagsAndAttributes.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
byte[] buffer = new byte[512];
uint cantleidos = 0;
try
{
bool retor = SetFilePointerEx(ptrFile, 5, IntPtr.Zero, MoveMethod.FILE_BEGIN);
bool b = ReadFile(ptrFile, buffer,512 , out cantleidos, IntPtr.Zero);
}
catch (Exception)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
Buffer.BlockCopy(System.Text.Encoding.UTF8.GetBytes("SOMETEXT".ToCharArray()), 0, buffer,3,"SOMETEXT".ToCharArray().Length);
uint wrote = 0;
bool c = WriteFile(ptrFile, buffer, (uint)buffer.Length, ref wrote, IntPtr.Zero);
int closed = CloseHandle(ptrFile);