winapi – 如何在Inno Setup中使用GetVolumeInformation?

前端之家收集整理的这篇文章主要介绍了winapi – 如何在Inno Setup中使用GetVolumeInformation?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在使用Inno Setup创建的安装过程中,我需要获取驱动器号的卷序列号.我知道DLL函数可以导入Inno,但我对它很新,并且在使它工作时遇到一些问题.我知道kernel32中的GetVolumeInformation函数可以做我需要的.有人可以告诉我如何在Inno脚本中导入和使用该功能来检索卷序列号吗?

谢谢!

解决方法

Inno-Setup代码::
[Code]
function GetVolumeInformation(
  lpRootPathName: PChar;
  lpVolumeNameBuffer: PChar;
  nVolumeNameSize: DWORD;
  var lpVolumeSerialNumber: DWORD;
  var lpMaximumComponentLength: DWORD;
  var lpFileSystemFlags: DWORD;
  lpFileSystemNameBuffer: PChar;
  nFileSystemNameSize: DWORD
  ): BOOL;
  external 'GetVolumeInformationA@kernel32.dll stdcall';


function LoWord(dw: DWORD): WORD;
begin
  Result := WORD(dw);
end;

function HiWord(dw: DWORD): WORD;
begin
  Result := WORD((dw shr 16) and $FFFF);
end;

function WordToHex(w: WORD): string;
begin
  Result := Format('%.4x',[w]);
end;

function FindVolumeSerial(const Drive: string): string;
var
  FileSystemFlags: DWORD;
  VolumeSerialNumber: DWORD;
  MaximumComponentLength: DWORD;
begin
  Result := '';
  // Note on passing PChars using RemObjects Pascal Script:
  // '' pass a nil PChar  
  // #0 pass an empty PChar    
  if GetVolumeInformation(
    PChar(Drive),'',// nil
    0,VolumeSerialNumber,MaximumComponentLength,FileSystemFlags,// nil
    0)
  then
    Result := WordToHex(HiWord(VolumeSerialNumber)) + '-' + WordToHex(LoWord(VolumeSerialNumber));
end;

function InitializeSetup(): Boolean;
begin
  MsgBox(FindVolumeSerial('c:\'),mbInformation,mb_Ok);
end;

使用Inno-setup版本5.2.3进行测试在Inno-Setup的Unicode版本中,用PAnsiChar替换PChar

原文链接:https://www.f2er.com/delphi/101201.html

猜你在找的Delphi相关文章