Solved

Get free disk space C++

Posted on 1998-02-05
3
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 clear a vector as well as how to detect empty vectors in C++.

622 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