K
K
KeeVeeG2020-05-15 18:11:56
C++ / C#
KeeVeeG, 2020-05-15 18:11:56

Why does it throw an error when getting the volume volume?

Hello, I'm doing lab work. You need to display the name of the volumes and their volume. For some reason, when calling GetDiskFreeSpaceEx(), the program stops and does not even give out the names of the next volumes. Even the last output "End" does not work. Tried running as administrator.

#include <windows.h>
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    setlocale(LC_ALL, "Russian");

    int WinVersion1 = (LOBYTE(LOWORD(GetVersion())));
    int WinVersion2 = (HIBYTE(LOWORD(GetVersion())));
    cout <<"Версия операционной системы: "<<WinVersion1<<"."<<WinVersion2;

    char SystemDir[256];
    GetSystemDirectory(SystemDir, 256);
    cout <<"\nСистемный каталог: "<<SystemDir;

    char PC_Name[256];
    DWORD PC_NameSize;
  PC_NameSize=sizeof(PC_Name);
  GetComputerName(PC_Name,&PC_NameSize);
    cout <<"\nНазвание компьютера: "<<PC_Name;

    char UserName[256];
    DWORD UserNameSize;
  UserNameSize=sizeof(UserName);
  GetUserName(UserName,&UserNameSize);
    cout <<"\nИмя пользователя: "<<UserName;

    char Volume[256];
    char Path[256];
    PULARGE_INTEGER Free;
    PULARGE_INTEGER Total;
    PULARGE_INTEGER TotalBytes;

  cout<<"\n\nСписок томов:";
HANDLE search = FindFirstVolume(Volume, sizeof(Volume));
do {
    cout <<"\n"<<Volume;
    GetDiskFreeSpaceEx(Volume, Free, Total, TotalBytes); //
    cout<<"\Общий объяем:\n"<<Total<<" Байт\nСвободного места:\n"<<Free<<" Байт";
}
while (FindNextVolume(search, Volume, sizeof(Volume)));

cout << "Конец";
}

6puXTlxO.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-05-15
@KeeVeeG

For some reason, when calling GetDiskFreeSpaceEx(), the program stops
PULARGE_INTEGER Free;
    PULARGE_INTEGER Total;
    PULARGE_INTEGER TotalBytes;

  cout<<"\n\nСписок томов:";
HANDLE search = FindFirstVolume(Volume, sizeof(Volume));
do {
    cout <<"\n"<<Volume;
    GetDiskFreeSpaceEx(Volume, Free, Total, TotalBytes); //

Because PULARGE_INTEGER are pointers (which is pointed to by P at the beginning of the type name according to Hungarian notation ). You call a function with uninitialized pointers, it writes to no one knows where, the application crashes.
It would be correct to do this:
ULARGE_INTEGER Free;
    ULARGE_INTEGER Total;
    ULARGE_INTEGER TotalBytes;

  cout<<"\n\nСписок томов:";
HANDLE search = FindFirstVolume(Volume, sizeof(Volume));
do {
    cout <<"\n"<<Volume;
    GetDiskFreeSpaceEx(Volume, &Free, &Total, &TotalBytes); //

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question