GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-May-2004, 23:53
liesberg liesberg is offline
New Member
 
Join Date: May 2004
Posts: 1
liesberg is on a distinguished road

How to get free disk space in win32


Hi there

I need to know how to get the free disk space for a drive (e.g.: C:\) in C++ and win32 environment. What's the API function and how to use it?

Thanks very much,
liesberg (liesberg@bol.com.br)
  #2  
Old 21-May-2004, 00:24
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by liesberg
Hi there

I need to know how to get the free disk space for a drive (e.g.: C:\) in C++ and win32 environment. What's the API function and how to use it?

Thanks very much,
liesberg (liesberg@bol.com.br)
Look in MSDN.com for an API like GetFreeDiskSpace() or something similar. Or try google.
__________________

Age is unimportant -- except in cheese
  #3  
Old 21-May-2004, 00:47
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Check this out:

CPP / C++ / C Code:
typedef BOOL (WINAPI *PFNGETDISKFREESPACEEX)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);

#include <iostream>

HINSTANCE hModule = ::LoadLibrary("KERNEL32.DLL");
if(hModule)
{
  PFNGETDISKFREESPACEEX pDiskFreeSpaceEx = NULL;

  // Determine function to use
  pDiskFreeSpaceEx = reinterpret_cast<PFNGETDISKFREESPACEEX>(::GetProcAddress(hModule,
                                                                              "GetDiskFreeSpaceExA"));
  if(!pDiskFreeSpaceEx)
  {
    DWORD dwSectorsPerCluster = 0;
    DWORD dwBytesPerSector    = 0; 
    DWORD dwFreeClusters      = 0; 
    DWORD dwClusters          = 0; 

    if(::GetDiskFreeSpace(strRootPath.c_str(),
                          &dwSectorsPerCluster,
                          &dwBytesPerSector,
                          &dwFreeClusters,
                          &dwClusters) == TRUE)
      std::cout << "Available disk space = "
                << dwFreeClusters * dwBytesPerSector * dwSectorsPerCluster << " MB" << std::endl;
    else
      // Error
  }
  else
  {
    ULARGE_INTEGER uliFreeBytesAvailableToCaller;
    ULARGE_INTEGER uliTotalNumberOfBytes;
    ULARGE_INTEGER uliTotalNumberOfFreeBytes;

    if(::GetDiskFreeSpaceEx(strRootPath.c_str(),
                            &uliFreeBytesAvailableToCaller,
                            &uliTotalNumberOfBytes,
                            &uliTotalNumberOfFreeBytes) == TRUE)
      std::cout << "Available disk space = "
                << static_cast<int>(static_cast<__int64>(uliFreeBytesAvailableToCaller.QuadPart /
                                                         (1024 * 1024)))
                << " MB" << std::endl;
    else
      // Error
  }

  // Release library
  ::FreeLibrary(hModule);
}
else
  // Error -> call ::GetLastError()
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #4  
Old 21-May-2004, 14:26
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Wow, dude... do you know a lot of WinAPI or do you just frequent the MSDN?
__________________
-Aaron
  #5  
Old 23-May-2004, 19:38
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Quote:
Originally Posted by aaroncohn
Wow, dude... do you know a lot of WinAPI or do you just frequent the MSDN?

No, I don't know lots of WinAPI, but this one I know and used before, but the codes is directly 'copy & paste' from MSDN!!
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
1000MB space 50gig transfer Free Domain $6.95/mo surfspeedy Web Hosting Advertisements & Offers 0 24-Dec-2003 22:31
Special Reseller plan - 500MB Disk Space, 5GB Bandwidth, Cpanel/WHM - $5.95/Mo. IEAN.net Web Hosting Advertisements & Offers 0 02-Jul-2003 15:01
Get up to 3 months of free web hosting goody Web Hosting Advertisements & Offers 0 07-Jun-2003 07:44
RESELLER Specials - Create your own Resellers 1GB disk space 10GB transfer $10/Month IEAN.net Web Hosting Advertisements & Offers 0 19-May-2003 13:32
Free Hosting 100 Megs Space And More Raptor Free Web Hosting 3 27-Mar-2003 07:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.