How can I use GetVolumeInformation in Inno Setup?

I need to get the volume serial number for a drive letter during an installation created using Inno Setup. I know that DLL functions can be imported into Inno, but I'm pretty new to this and have some problems with it working. I know that the GetVolumeInformation function in kernel32 can do what I need. Can someone show me how to import and use this functionality in an Inno script to get the volume serial number?

Thanks!

+6
source share
2 answers

Inno-Setup Code ::

[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; 

Tested with Inno-setup version 5.2.3
In Inno-Setup Unicode versions replace PChar with PAnsiChar

+7
source

Since InnoSetup does not support pointers, you will need to create an external library to call GetVolumeInformation . The following code examples should work for all combinations of Delphi and InnoSetup (in terms of Unicode support).

Here is the Delphi library code:

 library VolumeInformation; uses Types, Classes, SysUtils, Windows; var SerialNumber: AnsiString; function GetVolumeSerial(Drive: PAnsiChar): PAnsiChar; stdcall; var FileSystemFlags: DWORD; VolumeSerialNumber: DWORD; MaximumComponentLength: DWORD; begin SerialNumber := ''; GetVolumeInformationA(Drive, nil, 0, @VolumeSerialNumber, MaximumComponentLength, FileSystemFlags, nil, 0); SerialNumber := IntToHex(HiWord(VolumeSerialNumber), 4) + ' - ' + IntToHex(LoWord(VolumeSerialNumber), 4); Result := PAnsiChar(SerialNumber); end; exports GetVolumeSerial; end. 

And here is the InnoSetup code:

 [Files] Source: "VolumeInformation.dll"; Flags: dontcopy [Code] function GetVolumeSerial(Drive: PAnsiChar): PAnsiChar; external ' GetVolumeSerial@files :VolumeInformation.dll stdcall setuponly'; procedure ButtonOnClick(Sender: TObject); var S: string; begin S := GetVolumeSerial('c:\'); MsgBox(S, mbInformation, mb_Ok); end; 
+4
source

Source: https://habr.com/ru/post/908400/


All Articles