Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get free disk space C++

Posted on 1998-02-05
3
Medium Priority
?
409 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 800 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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

971 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