News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

GetDiskFreeSpaceEx help

Started by maruf10, April 09, 2010, 04:00:11 AM

Previous topic - Next topic

maruf10

hello all ...

I am trying to get the free and used disk space of a  specific drive like D:\
This is form MSDN

Quote

BOOL GetDiskFreeSpaceEx(
  LPCWSTR lpDirectoryName,
  PULARGE_INTEGER lpFreeBytesAvailableToCaller,
  PULARGE_INTEGER lpTotalNumberOfBytes,
  PULARGE_INTEGER lpTotalNumberOfFreeBytes
);

The values obtained by this function are of type ULARGE_INTEGER. Be sure not to truncate these values to 32 bits.


how can i get free and used bytes ??
need 64-bit !!??

dedndave

Hutch had a great llittle program for that a few months back
let me see if i can find it with the forum search tool...


Ghandi

You might need to write a sticky note giving a detailed explanation with pictures Dave, its magic that the search button would yield results!!! o0

Sorry, i couldn't help that. Don't mind me, its all tongue in cheek.

HR,
Ghandi

jj2007

Here is a working snippet (only the Print Str$ part requires the MB lib, replace with crt printf or similar if needed).

include \masm32\MasmBasic\MasmBasic.inc

.data
FreeBytesAvailableToCaller dq 0
TotalNumberOfBytes dq 0
TotalNumberOfFreeBytes dq 0
DirectoryName db "D:\", 0
OneGiga = 1024*1024*1024

.code
start: invoke GetDiskFreeSpaceEx,
offset DirectoryName,
offset FreeBytesAvailableToCaller,
offset TotalNumberOfBytes,
offset TotalNumberOfFreeBytes

Print Str$("FreeBytesAvailable:\t%4f GB\n", q:FreeBytesAvailableToCaller/OneGiga)
Print Str$("TotalNumberOfBytes:\t%4f GB\n", q:TotalNumberOfBytes/OneGiga)
Print Str$("TotalNumberOfFreeBytes:\t%4f GB\n", q:TotalNumberOfFreeBytes/OneGiga)

Inkey "bye"
Exit
end start


FreeBytesAvailable:     141.3 GB
TotalNumberOfBytes:     149.0 GB
TotalNumberOfFreeBytes: 141.3 GB

maruf10

Trying to get the used space of drive K: ...
may b the subtraction operatio is wrong
it shows correct output for total and free space .
i am trying to calculate the used space by (total space - free space)

here is the code:
Quote
.data
freeSpace dd ?,?          ;GetDiskFreeSpaceEx expects this to be 64-bits
totalSpace dd ?,?         ;GetDiskFreeSpaceEx expects this to be 64-bits
usedSpace dd ?,?
outputbuffer db 256 dup(?)
totalbuffer db 256 dup(?)
usedbuffer  db 256 dup(?)
freebuffer  db 256 dup(?)
report db  "Total Space ............ %s",13,10,"Used Space ............ %s",13,10,"Free Space ............ %s"

.code
start:

invoke GetDiskFreeSpaceEx, CTEXT("K:\"),addr freeSpace,addr totalSpace,0

invoke StrFormatByteSize64, totalSpace, totalSpace+4, ADDR totalbuffer, SIZEOF totalbuffer
invoke StrFormatByteSize64, freeSpace, freeSpace+4, ADDR freebuffer, SIZEOF freebuffer

mov eax , totalSpace
mov ebx , freeSpace
sub eax , ebx
mov usedSpace , eax

invoke StrFormatByteSize64, usedSpace, usedSpace+4, ADDR usedbuffer, SIZEOF usedbuffer

invoke wsprintf , addr outputbuffer , addr report , addr totalbuffer , addr usedbuffer , addr freebuffer
invoke MessageBox, NULL,addr outputbuffer,CTEXT("K:\ Drive Info"), MB_OK
invoke ExitProcess,NULL
end start



it also shows some extra character after last line ...
here is the output copied from MessageBox.
Quote

Total Space ......... 7.46 GB
Used Space ......... 502 MB
Free Space .......... 2.97 GBK:\

what is the solution ??

qWord

Quotereport db  "Total Space ............ %s",13,10,"Used Space ............ %s",13,10,"Free Space ............ %s",0
...
mov eax,totalSpace
mov edx,totalSpace+4
sub eax,freeSpace
sbb edx,freeSpace+4
mov usedSpace,eax
mov usedSpace+4,edx
FPU in a trice: SmplMath
It's that simple!

maruf10

what is the reason for adding 4 ??
actually i didn't understand it  :red

qWord

you need 64Bit arithmetic because file/directory sizes are 64 bit width. The '+4' is used to access the high order DWORD of a QWORD. (4 is added to the addres of variable, not to the content)
FPU in a trice: SmplMath
It's that simple!

dedndave

Ghandi
this is a case where i knew what to look for - i also knew who the author was, which helped me narrow it down a bit   :P

maruf10

to qWord:

what is the reason for using both sub and sbb??

sub eax,freeSpace
sbb edx,freeSpace+4

what is the reason for using 4 parameters in StrFormatByteSize64 ??
there should be three parameters ..

donkey

Quote from: maruf10 on April 10, 2010, 05:54:04 AM
to qWord:

what is the reason for using both sub and sbb??

sub eax,freeSpace
sbb edx,freeSpace+4

It is used to do 64 bit subtraction (subtract then subtract with borrow).

Quotewhat is the reason for using 4 parameters in StrFormatByteSize64 ??
there should be three parameters ..

Because you are pushing 32bit numbers and the first parameter is a 64 bit number, you have to push it in 2 parts hence the 4 parameters instead of 3.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

ragdog

Here is a Example from Florian Mücke



.data

    lpRootPathName db "\",0
    lpSPerClus dd 0
    lpBPerSec dd 0
    lpNOfFreeClus dd 0
    lpTotalNClus dd 0
ConvertStr db "%lu kB available to caller",13,10,"%lu k total",0
String db 255 dup (0)

caption db "GetDiskFreeSpace",0
lpDir db "\",0
lpFBAvailableQ dq 0
lpTotalBytesQ dq 0
lpFBAvailableD dd 0
lpTotalBytesD dd 0

String1 db 255 dup (0)

OneK REAL10 1024.0

.code
start:
invoke GetDiskFreeSpaceEx, ADDR lpDir, ADDR lpFBAvailableQ, ADDR lpTotalBytesQ, NULL
finit ;initialisiert FPU
fld OneK ;laed eine Realzahl (1024,0) in den TOS
fild lpFBAvailableQ ;laed lpFB... in den TOS
fdiv ST(0),ST(1) ;dividiert zwei Realzahlen
fistp lpFBAvailableD ;speichert Integer aus dem TOS und poppt
fild lpTotalBytesQ
fdiv ST(0),ST(1)
fistp lpTotalBytesD
invoke wsprintf, ADDR String1, ADDR ConvertStr,lpFBAvailableD, lpTotalBytesD
invoke MessageBox,0,ADDR String1,ADDR caption,MB_OK
push 0
call ExitProcess          
end start



sinsi

  mov eax,totalSpace
  mov edx,totalSpace+4
  shrd eax,edx,10   ;10 for KB, 20 for MB, eax has value

I prefer to see "4096 Meg" instead of "4294967296 bytes" (or even "4 Gig" but accuracy is lost with too many shifts (3.99 Gig will show as "3 Gig")).
Light travels faster than sound, that's why some people seem bright until you hear them.

clive

Quote from: sinsi
I prefer to see "4096 Meg" instead of "4294967296 bytes" (or even "4 Gig" but accuracy is lost with too many shifts (3.99 Gig will show as "3 Gig")).

The trick of course it to display them in a magnitude appropriate manner, especially when the number may be a few thousand bytes, up to a couple of terabytes.

-Clive
It could be a random act of randomness. Those happen a lot as well.