如何在Inno Setup中使用GetVolumeInformation? [英] How can I use GetVolumeInformation in Inno Setup?
问题描述
在使用Inno Setup创建的安装过程中,我需要获取驱动器号的卷序列号.我知道可以将DLL函数导入Inno,但我对此还很陌生,并且在使其工作时遇到了一些问题.我知道kernel32中的GetVolumeInformation函数可以满足我的需要.有人可以告诉我如何在Inno脚本中导入和使用该功能来检索卷序列号吗?
I need to get the volume serial number for a drive letter during an installation created with Inno Setup. I know that DLL functions can be imported into Inno, but I'm fairly new to it and having some problems getting it to work. I know that the GetVolumeInformation function in kernel32 can do what I need. Could someone show me how to import and use that functionality in an Inno script to retrieve the volume serial number?
谢谢!
推荐答案
Inno-Setup代码::
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;
在Inno-setup版本5.2.3中进行了测试
在Inno-Setup的Unicode版本中,将PChar
替换为PAnsiChar
Tested with Inno-setup version 5.2.3
In Unicode versions of Inno-Setup replace PChar
with PAnsiChar
这篇关于如何在Inno Setup中使用GetVolumeInformation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!