2

I want to parallelize some disk I/O, but this will hurt performance badly if my files are on the same disk.

Note that there may be various filter drivers in between the drivers and the actual disk -- e.g. two files might be on different virtual disks (VHDs), but on the same physical disk.

How do I detect if two HANDLEs refer to files (or partitions, or whatever) on the same physical disk?

If this is not possible -- what is the next-best/closest alternative?

user541686
  • 205,094
  • 128
  • 528
  • 886

1 Answers1

0

This is rather convoluted, but the first step is getting the volume name from the handle. You can do that on Vista or above using the GetFinalPathNameByHandle function. If you need to support earlier Windows versions, you can use this example.

Now that you have the file name, you can parse out the drive letter or volume name, and use the method explained in the accepted answer to this question.

Community
  • 1
  • 1
Carey Gregory
  • 6,836
  • 2
  • 26
  • 47
  • That's the easy part! [I already have a better solution for that](http://stackoverflow.com/a/5286888/541686). :) The trouble is that it gives you the *logical* disk, not the *physical* disk -- there might be some virtualization layer on top of the disk (including, but not limited to VHD files) that might give you a false negative, so unfortunately it doesn't solve the problem in the question. – user541686 Jul 14 '12 at 00:51
  • @Mehrdad: Not sure I understand what's better about your solution for finding the file name from the handle. Seems to be the same approach I suggested (and I agree that's the easy part). But you're right that my answer doesn't solve your problem. It was my (mis)understanding that VHDs would not appear as \\.\PhysicalDriveX, but that's not the case. Digging further, I don't think you're going to find a way to distinguish virtual from physical disks from user mode code since the virtualization happens at such a low level. Good luck with it. It's an interesting problem that's bugging me now. – Carey Gregory Jul 14 '12 at 22:40
  • The only reason I mentioned "better" is that it doesn't need to use `QueryDosDevice` to search the `\DosDevices` namespace -- it gets a *direct* value from the mount manager. Other than that, though, yeah, it's pretty much the same (and yours has the advantage that it works with programs that bypass the mount manager, like ImDisk). :) I'm thinking I might need kernel-mode too... but I'm not sure what functions to call there either. (I know it should be possible, because the functionality would be necessary for ejecting a device safely.) Thanks for trying to help anyway! – user541686 Jul 14 '12 at 23:04
  • You can mount a hard drive as a subdirectory of another drive. So two paths on different physical drives can still have the same drive letter. Using `VOLUME_NAME_NT` rather than `VOLUME_NAME_DOS` will catch those cases, but care must be taken with network drives, which all begin with `"\Device\MUP"`. – Dwedit Jul 30 '22 at 21:39