Solved

Get free disk space C++

Posted on 1998-02-05
3
395 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

707 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

16 Experts available now in Live!

Get 1:1 Help Now