Link to home
Start Free TrialLog in
Avatar of Stephen Kairys
Stephen KairysFlag for United States of America

asked on

C: GetDiskFreeSpaceEx() for default drive

Hey Everyone,

A while back, per this thread: some of you experts were very helpful in guiding me through upgrading our handling of determining freespace on a certain drive to handle values exceeding the largest 32-bit integer. Per this thread, involving the use of GetFreeSpaceEx().

https://www.experts-exchange.com/questions/28269833/C-function-to-determine-freespace-has-issues-with-large-amounts-of-freespace.html

So we call it like this:
GetDiskFreeSpaceEx(DriveSpec, &freeSpace, &totalSpace, &totalFreeSpace );
e.g.
GetDiskFreeSpaceEx("C:\", &freeSpace, &totalSpace, &totalFreeSpace );

Thing is, I now need to leverage this methodology in a program that needs to get the free space from the DEFAULT drive (the drive on which I am running the EXE).  Is there a way to do this?  I guess there would be two possibilities:
1) Pass the appropriate parameter. (I tried passing the empty string, but got a meaningless (very large) value as the result.)

2) Pass some value, available somehow, that already contains the letter of the default drive e.g. 'C'.

Thanks in advance for your help
Steve
Avatar of ktaczala
ktaczala
Flag of United States of America image

I assume you've written a standalone(no need to install) application to do this.

Look at passing it the drive letter in a parameter i.e myapptogetfreespace.exe D
The code to get parameters from a command line startup is:
My.Application.CommandLineArgs
here's a link to the explanation of commandlineargs:
http://msdn.microsoft.com/en-us/library/z2d603cy%28v=vs.90%29.aspx
Or open an input box on startup to request the driver letter from the user.
Avatar of Stephen Kairys

ASKER

Hi Ktaczala,

1. That's the whole point.. this EXE (call it NEWDISKFREE.EXE) needs to run without specifically passing in a drive letter (to indicate that it should use the default (current) drive , as well as with passing in a specific drive letter.  It worked that way when using the legacy _dos_getdiskfree() for which the numeric parameter for the drive was 0. By the same token, 1=A, 2=B, 3=C etc. although I'm not sure it even worked for the A and B diskette drives.

2. The program runs unattended as part of overnight processing, so user input is not an option here.

Thanks
Steve
If the app is physically located on the drive to check, you could get the applications startup path then parse it for the drive letter.
see:
http://www.dotnetheaven.com/article/get-application-startup-and-executable-path-in-vb.net
Hold on, isn't the above for VB.NET. I'm using C.
That said:
I was able to use your concept in C by looking at
argv[0].
Which is (we are running on the K: drive)
K:\TEST\newdiskfree.exe

So looks like I can take atgv[0][ then.

I guess the only remaining question is if argv[0] can always be relied on to contain the drive letter as the first character. That is, that it would NEVER contain just
the program name e.g. "NEWDISKFREE.EXE"

Thank you for this...a clever idea indeed. :)
Steve
Actually I mean to say argv[0][0]
DefaultDrive = argv[0][0]

Of course this works only b/c I know my EXE will always reside on the default drive. :)
All,
I will be off until Monday and will be unable to respond further on this thread should anyone else post during that time.

Thanks for the help, and have a good weekend!
Steve
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the samples jkr has posted are the right solutions.

however, you could spare some code and enhance the usage to any folder path, unc paths and relative paths  by calling shell function SHGetDiskFreeSpaceEx.

the function would accept a root path like "C:\\", or any folder path like "D:\\temp\\whatever", or any valid unc path like "\\server\share\folder". as it also supports relative paths the code could be as simple as

#include <shellapi.h>
...

ULARGE_INTEGER  x, y, z;
int ret = SHGetDiskFreeSpaceEx(".\\", &x, &y, &z);   // use L".\\" if UNICODE

Open in new window


Sara
OK I tried jkr's solution and I got this compiler error per this include:
#include <shlwapi.h>

testfree64-default.c(10): Error! E1055: Unable to open 'shlwapi.h'

That said, at this point, I think that I can safely assume that the EXE will be on the "default" drive  in which case I will continue simply looking at argv[0][0].

Thanks,
Steve
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jkr,
Thank you for the above. That said, the solutions provided are a bit more involved for the scope of this project...since it's unlikely that the EXE will not be on the default drive.  So as above, I am going to go with the program name for now.

That said, I will try your solution when I get a chance. You deserve some points for all your effort, and I just want to confirm it works before officially assigning them.

Thanks again.
Steve
Hi and sorry for the above. I am no longer working at the company for whom I was working when I needed to leverage this function.

Accordingly, since I no longer have access to my source code and it's been almost a year,  I'll defer to the MOD's judgment per closing and crediting with points. Do what you need to do. :)

Thanks,
Steve