首页 > 解决方案 > 如何从 Inno Setup 中的 GetVolumeInformation 获取卷名?

问题描述

我尝试从 Windows API 获取 Inno Setup 中的卷名。序列号返回正确,但卷名为空。我在这个线程中使用了“kobik”的代码:

如何GetVolumeInformation在 Inno Setup 中使用?

这是我在 Inno Setup 中的功能:

function FindVolumeName(const Drive: string): string;
var
  FileSystemFlags: DWORD;
  VolumeSerialNumber: DWORD;
  MaximumComponentLength: DWORD;
  ErrorCode: integer;
  VolumeLabel: PChar;

begin
  Result := '';

  { Note on passing PChars using RemObjects Pascal Script: }
  { '' pass a nil PChar }
  { #0 pass an empty PChar }
  if (GetVolumeInformation(pchar(drive), volumeLabel, MAX_LENGTH, VolumeSerialNumber, MaximumComponentLength, FileSystemFlags, '', 0)) then
  begin
    Result := WordToHex(HiWord(VolumeSerialNumber)) + '-' + WordToHex(LoWord(VolumeSerialNumber));
  end
  else
  begin
    errorCode:= GetLastError();
    MsgBox (SysErrorMessage (errorCode), mbError, MB_OK);
  end;

  MsgBox('VolumeLabel: ' +volumeLabel, mbInformation, MB_OK);
end;

我不确定如何使用该PChar类型。

标签: winapiinstallationinno-setuppascalscript

解决方案


function GetVolumeInformation(
  lpRootPathName: string; lpVolumeNameBuffer: string; nVolumeNameSize: DWORD;
  var lpVolumeSerialNumber: DWORD; var lpMaximumComponentLength: DWORD;
  var lpFileSystemFlags: DWORD; lpFileSystemNameBuffer: string;
  nFileSystemNameSize: DWORD): BOOL;
  external 'GetVolumeInformationW@kernel32.dll stdcall';

const
  MAX_PATH = 260;

function FindVolumeName(const Drive: string): string;
var
  FileSystemFlags: DWORD;
  VolumeSerialNumber: DWORD;
  MaximumComponentLength: DWORD;
begin
  SetLength(Result, MAX_PATH)
  if GetVolumeInformation(
       Drive, Result, Length(Result), VolumeSerialNumber, MaximumComponentLength,
       FileSystemFlags, '', 0) then
  begin
    SetLength(Result, Pos(#0, Result) - 1);
  end
    else
  begin
    RaiseException(SysErrorMessage(DLLGetLastError()));
  end
end;

(代码适用于Inno Setup 的Unicode 版本– Inno Setup 6 的唯一版本)。


推荐阅读