本帖最后由 VisualEleven 于 2014-04-23 10:32:13 编辑

解决方案 »

  1.   

    验证过 hPartition 的值了吗?
      

  2.   

    验证过?hPartition 反正可以用来ReadFile,这样算吗
      

  3.   

    从网上找到的资料说用FSCTL_LOCK_VOLUME 设置一下就可以操作0x2A了,并且因为卡那边必须用0X2A,所以看卡那边操作Flash的代码也确实如此。
    当初有人解决了这个问题,也没人分享一下。
    昨天测试的时候,直接用API,WriteFile在Win7下操作时也是不行的,然后查了下MSDN,也没有发现什么特别的。
    虽然使用FSCTL_LOCK_VOLUME 之后,已经可以正常使用WriteFile了,但为什么这样呢?
    突然想到,我的MSDN是2003版的,这个时候根本没有Win7之类的,所以MSDN根本不会了解到什么。
    所以赶紧去查看MSDN2010,果然有发现的。
    If you write directly to a volume that has a mounted file system, you must first obtain exclusive access to the volume. Otherwise, you risk causing data corruption or system instability, because your application's writes may conflict with other changes coming from the file system and leave the contents of the volume in an inconsistent state. To prevent these problems, the following changes have been made in Windows Vista and later:A write on a volume handle will succeed if the volume does not have a mounted file system, or if one of the following conditions is true:
    The sectors to be written to are boot sectors.
    The sectors to be written to reside outside of file system space.
    You have explicitly locked or dismounted the volume by using FSCTL_LOCK_VOLUME or FSCTL_DISMOUNT_VOLUME.
    The volume has no actual file system. (In other words, it has a RAW file system mounted.)上面这一段MSDN其实已经讲得很明白,在Vista及以后的系统上,要通过盘符操作已经装有文件系统的的卷,必须使用 exclusive access(独占访问),否则可能会失败。
    有两种方法,一种是独占访问,即通过FSCTL_LOCK_VOLUME 来设置独占访问。(有锁,相应的也有FSCTL_UNLOCK_VOLUME 。关掉句柄也能达到同样的效果)。
    另一种,通过FSCTL_DISMOUNT_VOLUME来移除其他进程对卷的访问,但是如果设置了独占访问,那么再调用FSCTL_DISMOUNT_VOLUME就会失败。