簡體   English   中英

如何從 Inno Setup 中的 GetVolumeInformation 獲取卷名?

[英]How to get volume name from GetVolumeInformation in Inno Setup?

我嘗試從 Windows API 獲取 Inno Setup 中的卷名。 序列號返回正確,但卷名為空。 我在這個線程中使用了“kobik”的代碼:

如何在 Inno Setup 中使用GetVolumeInformation

這是我在 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類型。

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 的唯一版本)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM