[Win32utils-devel] Playing with NtQueryInformationFile

Berger, Daniel Daniel.Berger at qwest.com
Mon Aug 18 09:56:46 EDT 2008

 > -----Original Message-----
> From: dlibrik at wolfram.com [mailto:dlibrik at wolfram.com] 
> Sent: Monday, August 18, 2008 12:26 AM
> To: Berger, Daniel
> Subject: Re: [Win32utils-devel] Playing with NtQueryInformationFile
> Hello Daniel,
> I apologize for writing to you out of nowhere, but you posted 
> on the Ruby Win32utils-devel list a while back and I think 
> you might have the answer to a question I'm working on.

Hi David,

No problem. I've cc'd the win32utils-devel list for future Googlers.
Hope that's ok.
> You wrote:
> > I'm trying to get the allocation size of a file via a file handle 
> > (rather than its name). The example below works for 
> > FileNameInformation but I can't get it to work as expected 
> for FileStandardInformation.
> >
> > I'm trying to get File::Stat#blksize working. It's easy 
> enough when I 
> > have the file _name_, but I'd like to get it from the file 
> _handle_ so 
> > I can make it work properly.
> I'm looking for the allocation size of a file given its name, 
> actually.
> What "easy enough" solution am I blindly missing?

Use GetDiskFreeSpace, then multiply the sectors by the bytes.

My email is a bit misleading, since it's not really the file name itself
that matters, but the root path of the file, since all files on the same
filesystem have the same blocksize. For relative paths, just pass nil as
the file name, and it will default to your system's root path. A pretty
good guess is 4096, since that's the default on the vast majority of
Windows systems. The ones I've come across anyway.

Here's some sample code using windows-pr:

require 'windows/filesystem'
require 'windows/path'
include Windows::FileSystem
include Windows::Path

file = "C:\\test.txt"

sectors = [0].pack('L')
bytes   = [0].pack('L')
free    = [0].pack('L')
total   = [0].pack('L')

if PathStripToRoot(file)
   file += "\\" unless file[-1].chr == "\\" # Add a trailing slash
   file = nil # Default to relative root path

if GetDiskFreeSpace(file, sectors, bytes, free, total)
   size = sectors.unpack('L').first * bytes.unpack('L').first
   size = "Unknown"

puts "Size: #{size}"

This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly 
prohibited and may be unlawful.  If you have received this communication 
in error, please immediately notify the sender by reply e-mail and destroy 
all copies of the communication and any attachments.

More information about the win32utils-devel mailing list