本文介绍了在WriteFile之后保留更改,为什么不发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写入我的闪存,我已经读取了512个字节,修改了该数组,为其他数组更改了一些字节,然后使用WriteFile API函数,再次写入(或尝试了)这512个字节字节再次放入我的内存中.此WriteFile API函数返回1,之后我使用CloseHandle API函数关闭用于与内存交互的句柄...但是,当我看到使用WinHex(hex编辑器)的内存时,那里的字节在我的程序尝试编写之前是相同的.如果我什至要关闭句柄,然后将其关闭,则缓冲区(从内存中读取的字节数组)似乎已修改,这是为什么呢?

写入的字节数实际上是512.这是我的代码...这只是测试代码.

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);

推荐答案



这篇关于在WriteFile之后保留更改,为什么不发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 13:45