Как получить имя тома из GetVolumeInformation в Inno Setup? - PullRequest
0 голосов
/ 16 мая 2018

Я пытаюсь получить имя тома в Inno Setup из Windows API.Серийный номер возвращается правильно, но имя тома пусто.Я использовал код '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.

1 Ответ

0 голосов
/ 16 мая 2018
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;

(Код для Unicode-версия Inno Setup ).

...