Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get free disk space C++

Posted on 1998-02-05
3
Medium Priority
?
403 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

670 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