Solved

Get free disk space C++

Posted on 1998-02-05
3
396 Views
Last Modified: 2008-02-01
I have a function that calculates the available space on a disk drive.  It works fine for NT and for WIN 95 older version.  It does not work for newer release of WIN 95 (service pack 2+).  The problem is that I can't
get the adress of the 'GetDiskFreeSpaceEx' in KERNEL32.DLL.

This is the code:

/********************************************************************
procedure:            GetDriveFreeSpace
input:                  CString directory - Source directory
return:                  double
description:      Return the amount of free space on the drive of the
                        given directory (in bytes)
********************************************************************/

typedef BOOL (*FREE_SPACE_FUNCTION_DEFINITION) (LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);

double GetFreeDriveSpace(CString directory)
{
      OSVERSIONINFO version_information;
      CString drive;
      short position;
      
      ASSERT(directory.GetLength() >= 0);

      // Get drive name
      position = (short) directory.Find(_TEXT(":"));

      if (position >= 0) {
            if (position == 0) {
                  // The colon is the first character
                  ASSERT(FALSE);
                  return (0);
            }
            drive = directory.Left(position + 1);
      }
      else {
            // Assume entire directory string is name
            drive = directory + _TEXT(":");
      }

      // Initialize structure
      version_information.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

      // Get version of Windows
      if (GetVersionEx(&version_information) == FALSE) {
            // Couldn't get version number
            ASSERT(FALSE);
            return (0);
      }

      if (version_information.dwPlatformId == VER_PLATFORM_WIN32_NT) {
            /// I deleted this part since it works for NT
      }
      else if (version_information.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
            // Windows 95 is running

            // Check if service pack 2+ has been installed
            if ((version_information.dwBuildNumber & 0xffff) <= 1000) {
                  // Service pack not installed
                  /// I deleted this part since it works for WIN 95 prior to service pack 2+
            }
            else {
                  // Service pack installed

                  // Use function from DLL
                  HINSTANCE instance;
                  FREE_SPACE_FUNCTION_DEFINITION procedure_address;
                  ULARGE_INTEGER total_free_space_for_user;
                  ULARGE_INTEGER total_free_space;
                  ULARGE_INTEGER total_space;

                  // Load DLL into memory
                  instance = LoadLibrary("KERNEL32.DLL");

                  // Check load
                  if (instance == NULL) {
                        ASSERT(FALSE);
                        return (0);
                  }

                  // Get function address
                  // THE NEXT LINE DOES NOT WORK
                  // PROBLEM: procedure_adress is allways NULL
                  procedure_address = (FREE_SPACE_FUNCTION_DEFINITION) GetProcAddress(instance,
                        "GetDiskFreeSpaceEx");

                  if (procedure_address == NULL) {
                        ASSERT(FALSE);
                        return (0);
                  }

                  // Get free disk space
                  if (procedure_address(drive, &total_free_space_for_user, &total_space,
                        &total_free_space) == FALSE) {
                        return (0);
                  }
                  else {
                        return (((double) FOUR_GIGABYTES * (double) total_free_space_for_user.HighPart) +
                              (double) total_free_space_for_user.LowPart);
                  }
            }
      }
      else {
            // Windows 3.1 is running
            ASSERT(FALSE);
            return (0);
      }
}
0
Comment
Question by:fhoule
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 200 total points
ID: 1180874
This function has two version. One is ANSI GetDriveFreeSpaceExA and another one is UNICODE GetDriveFreeSpaceExW. So you need either to define GetDriveFreeSpaceEx depending on copiling(UNICODe or ANSI) and use #define precompiler statement or to explicity get address of appropriate version of this function.
0
 

Author Comment

by:fhoule
ID: 1180875
Galkin, Thanks for the EXTRA quick response !
0
 

Author Comment

by:fhoule
ID: 1180876
I was a bit quick to provide the points....

The GetProcAddress still return NULL

I got the error code with GetLastError and got error 126
"The specified module could not be found. "

galkin, do you have an idea ???
Thanks in advance.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now