Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get a per disk ( or per partition ) usage report, aka read/write bytes
i know i can use performance counters for this, or even WMI, but i would like to achieve this same result by using native WinAPI with p/invoke

does anyone can point me to the right functions or libraries to query those details?
i assume i would need a function to return the current partitions and then other that returns total bytes read/write or something like that?

This is to be used in vb.net code, but guess this doesn't matter at this stage.

Regards,
Posted

creizlein wrote:
you are right, i know that its a dirty road that is not supposed to be taken, using the managed libraries is much better, but i need to avoid them for some other specific reasons, that's the reason i asked for the api functions, and yes, those will be invoked from VB.net, that's why i added the tag, but maybe i should remove it. i apologize if inappropriate..
As I say, you could find appropriate raw Windows API yourself.

Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364417%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363798%28v=vs.85%29.aspx[^].

See also: http://msdn.microsoft.com/en-us/library/windows/desktop/aa364407%28v=vs.85%29.aspx[^].

—SA
 
Share this answer
 
Comments
creizlein 24-Apr-14 17:02pm    
there we go, that's more like what i was looking for, that is a good starting point Sergey, thanks once again, i guess i was a bit lazy and didn't googled properly.

I still think that there has to be some other functions that return more global data, total bytes written/read per partition or per volume or whatsoever instead of having to subscribe to the whole volume files and monitor each change individually, but well.

Regards
Sergey Alexandrovich Kryukov 24-Apr-14 17:34pm    
Great.
I never heard about "total bytes" and other similar metrics. I guess they are not so important, so it's left to the developer.

(By the way, didn't you vote the lowest vote of 1 for the answer, maybe by mistake?)
—SA
creizlein 24-Apr-14 17:42pm    
nope, clearly it wasnt me, i was not able to vote till now i confirmed my addy and gave it 5... odd, maybe someone else not giving much credit :/
Sergey Alexandrovich Kryukov 24-Apr-14 17:47pm    
No problem, just checking... Thank you for telling me though.
—SA
Sure. You will need to use low-level disk access, the following raw Windows API:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364939%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365541%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365468%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365748%28v=vs.85%29.aspx[^].

The P/Invoke equivalents are already written for you:
http://www.pinvoke.net/default.aspx/kernel32.createfile[^],
http://www.pinvoke.net/default.aspx/kernel32.setfilepointer[^],
http://www.pinvoke.net/default.aspx/kernel32.readfile[^],
http://www.pinvoke.net/default.aspx/kernel32/ReadFileEx.html[^],
http://www.pinvoke.net/default.aspx/kernel32.writefile[^],
http://www.pinvoke.net/default.aspx/kernel32/WriteFileEx.html[^].

For the file names representing the whole disk, you can use the drive names as they are registered in the system, which you can obtain from WMI (the names started with '\\').

If you want to work not with physical drives but with partitions, you will have do delve into all those partitioning systems and parsing of the partition tables, boot records, all that stuff. This is quite a big topic itself. You can start here:
http://en.wikipedia.org/wiki/Disk_partitioning[^],
http://en.wikipedia.org/wiki/Partition_table[^],
http://en.wikipedia.org/wiki/Master_Boot_Record[^],
http://en.wikipedia.org/wiki/GUID_Partition_Table[^],
http://en.wikipedia.org/wiki/Boot_Engineering_Extension_Record#BEER[^].

Now, most or all of the operations you need may require elevated privilege for your application. This is how you do it manually:
http://4sysops.com/archives/vista%E2%80%99s-uac-8-ways-how-to-elevate-an-application-to-run-it-with-administrator-rights[^],
http://www.sevenforums.com/tutorials/11841-run-administrator.html[^].

In practice, instead, you will need to add an application manifest and embed it in your assembly to request elevation from the very beginning. This is explained here:
http://msdn.microsoft.com/en-us/library/bb756929.aspx[^].

—SA
 
Share this answer
 
v2
Comments
creizlein 24-Apr-14 15:59pm    
Hello Sergey,

First off, thank you very much for your reply, it did give me some good information, but maybe i expressed bad myself, I am not looking into ways of writing and reading files to the disks but instead to get the amounts of bytes that are being read/written to them, pretty much like a disk monitor usage.
Sergey Alexandrovich Kryukov 24-Apr-14 16:07pm    
Please see Solution 2.
—SA
creizlein wrote:
Hello Sergey, First off, thank you very much for your reply, it did give me some good information, but maybe i expressed bad myself, I am not looking into ways of writing and reading files to the disks but instead to get the amounts of bytes that are being read/written to them, pretty much like a disk monitor usage.
Sorry for not answering to this request, but it really was pretty unclear, in first place.

For such things, you need the class System.IO.FileSystemWatcher:
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx[^].

You need to be aware of certain problems though. Please see these CodeProject articles:
FileSystemWatcher - Pure Chaos (Part 1 of 2)[^],
FileSystemWatcher - Pure Chaos (Part 2 of 2)[^].

—SA
 
Share this answer
 
Comments
creizlein 24-Apr-14 16:10pm    
That is using dotNet code, that is pretty much the same as using the performance counters i mentioned before, the idea is to get that information using native API calls not a managed class.
Sergey Alexandrovich Kryukov 24-Apr-14 16:13pm    
Why? Your tags say: "VB.NET". In practice, it's the best to avoid P/Invoke by all means whenever possible, because it breaks that wide platform compatibility provided with CLI.
Anyway, on Windows, this API is based on raw Windows API you can easily find. :-)
—SA
creizlein 24-Apr-14 16:23pm    
you are right, i know that its a dirty road that is not supposed to be taken, using the managed libraries is much better, but i need to avoid them for some other specific reasons, that's the reason i asked for the api functions, and yes, those will be invoked from VB.net, that's why i added the tag, but maybe i should remove it. i apologize if inappropriate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900