这是一段在好多地方都可以见到的取硬盘序列号的函数,这个函数执行起来完全正确:
function GetHDSerialNumber(Drv : String): String; 
var
   VolumeSerialNumber : DWORD;
   MaximumComponentLength : DWORD;
   FileSystemFlags : DWORD;begin
if Drv[Length(Drv)] =':' then Drv := Drv + '\';
GetVolumeInformation(pChar(Drv),nil,0,@VolumeSerialNumber,MaximumComponentLength,FileSystemFlags,nil,0);
Result := IntToHex(HiWord(VolumeSerialNumber), 4)+'-'+IntToHex(LoWord(VolumeSerialNumber), 4);
end;
可是令我困惑的是GetVolumnInformation在MSDN中是这样的:
BOOL GetVolumeInformation(
  LPCTSTR lpRootPathName,        // address of root directory of the 
                                 // file system
  LPTSTR lpVolumeNameBuffer,     // address of name of the volume
  DWORD nVolumeNameSize,         // length of lpVolumeNameBuffer
  LPDWORD lpVolumeSerialNumber,  // address of volume serial number
  LPDWORD lpMaximumComponentLength,
                                 // address of system's maximum 
                                 // filename length
  LPDWORD lpFileSystemFlags,     // address of file system flags
  LPTSTR lpFileSystemNameBuffer, // address of name of file system
  DWORD nFileSystemNameSize      // length of lpFileSystemNameBuffer
);
 
既然lpVolumeSerialNumber、lpMaximumComponentLength、lpFileSystemFlags都是LPDWORD类型的,为什么下面这种用法不对呢?
GetVolumeInformation(pChar(Drv),nil,0,@VolumeSerialNumber,@MaximumComponentLength,@FileSystemFlags,nil,0);也就是说为什么用@VolumeSerialNumber的形式而后二个参数确不用“@”?

解决方案 »

  1.   

    因为这个API函数转到Delphi里参数类型有些变化了
    在delphi中是这样定义的,和 VC中的不尽相同
    Unit
    Windows.PasSyntax
    GetVolumeInformation(
    lpRootPathName: PChar; {the path to the root directory}
    lpVolumeNameBuffer: PChar; {the buffer receiving the volume name}
    nVolumeNameSize: DWORD; {the maximum size of the buffer}
    lpVolumeSerialNumber: PDWORD; {a pointer to the volume serial number}
    var lpMaximumComponentLength: DWORD; {maximum file component name}var lpFileSystemFlags: DWORD; {file system flags}
    lpFileSystemNameBuffer: PChar;  {the buffer receiving the file system name}
    nFileSystemNameSize: DWORD {the maximum size of the file system name}
    ): BOOL; {returns TRUE or FALSE}