procedure TForm1.Button1Click(Sender: TObject);
var
  sec1, byt1, cl1, cl2, M, N:longword;
begin
  GetDiskFreeSpace(Pchar('C:\'), sec1, byt1, cl1, cl2);
  M := cl1*sec1 * byt1;
  N := cl2*sec1 * byt1;
  GetDiskFreeSpace(Pchar('D:\'), sec1, byt1, cl1, cl2);
  M:= M+ cl1*sec1 * byt1;
  N :=N+ cl2*sec1 * byt1;
  GetDiskFreeSpace(Pchar('E:\'), sec1, byt1, cl1, cl2);
  M:= M+ cl1*sec1 * byt1;
  N :=N+ cl2*sec1 * byt1;
  Label1.Caption := '该驱动器总共容量' + Formatfloat('###,###,###',N) + '字节';
  Label2.Caption := '该驱动器可用容量' + Formatfloat('###,###,###',M) + '字节';
end;

解决方案 »

  1.   

    看了一下我的硬盘,20G分区大概是 $494A0C000 字节,使用了35bit,所以你必须用int64来做乘积M、N,在Delphi的有序类型世界,目前只有这个类型最大。Type Range Format
    Integer -2147483648..2147483647 signed 32-bit
    Cardinal 0..4294967295 unsigned 32-bit
    Fundamental integer types include Shortint, Smallint, Longint, Int64, Byte, Word, and Longword.Fundamental integer types  
    Type Range Format
    Shortint -128..127 signed 8-bit
    Smallint -32768..32767 signed 16-bit
    Longint -2147483648..2147483647 signed 32-bit
    Int64 -2^63..2^63-1 signed 64-bit
    Byte 0..255 unsigned 8-bit
    Word 0..65535 unsigned 16-bit
    Longword 0..4294967295 unsigned 32-bit
      

  2.   

    Int64可以描述 9223372036854775807字节,8388607T 的容量,估计你几年内不用考虑修改代码了。
      

  3.   

    var
      sec1, byt1, cl1, cl2, M, N:longword;改为
    var
      sec1, byt1, cl1, cl2, M, N:LONGLONG;或
    var
      sec1, byt1, cl1, cl2, M, N:Int64;
      

  4.   

    LONGLONG 是C语言规范,不是Pascal的,在Delphi里面还是用Int64比较可靠,至少不会因为C的变化导致你重新编码。
      

  5.   

    var
      sec1, byt1, cl1, cl2, M, N:Int64;
    后编译会出现错误Types of actual and formal var parameters must be identical
    形参与实参必须一致是不是要转一下,应该怎么转?
      

  6.   

    函数原型:
    BOOL GetDiskFreeSpace(    LPCTSTR lpRootPathName, // address of root path 
        LPDWORD lpSectorsPerCluster, // address of sectors per cluster 
        LPDWORD lpBytesPerSector, // address of bytes per sector 
        LPDWORD lpNumberOfFreeClusters, // address of number of free clusters  
        LPDWORD lpTotalNumberOfClusters  // address of total number of clusters  
       );可以看到一个是字串、4个DWORD类型,所以你sec1, byt1, cl1, cl2应该使用DWORD、LONGWORD、Cardinal 类型;而作为乘积的M, N采用Int64
      

  7.   

    这个是Pascal语法的函数原型
    function GetDiskFreeSpace(lpRootPathName: PChar;
      var lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters: DWORD): BOOL; stdcall;
    {$EXTERNALSYM GetDiskFreeSpace}
      

  8.   

    按照  快乐老猫(无米下炊)  的说法,还是不能正确显示啊我的D盘为29.6GB;  31,824,461,824字节
    程序运行后显示字节数为1,759,690,752字节procedure TForm1.Button1Click(Sender: TObject);
    var
      sec1, byt1, cl1, cl2: longword;
      M, N:int64;
    begin
      GetDiskFreeSpace(Pchar('D:\'), sec1, byt1, cl1, cl2);
      M := cl1*sec1 * byt1;
      N := cl2*sec1 * byt1;   
      Label1.Caption := '该驱动器总共容量' + inttostr(N) + '字节';
      Label2.Caption := '该驱动器可用容量' + Formatfloat('###,###,###,###',M) + '字节';
    end;