winapi - 如何从 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
类型。
解决方案
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 的唯一版本)。
推荐阅读
- c# - 根据游戏对象的旋转向单位添加力
- java - 如何在片段中生成和使用 DialogFragment 的文本视图、按钮和图像?
- python - 如何从 Flask 模板中获取表单数据并传递给 HTML 页面
- python - 纸浆求解器约束
- html - 将页脚 div 固定到屏幕底部
- elasticsearch - 将弃用的 elasticsearch 查询转换为 7.12
- angular - 错误:NotYetImplemented - HTMLCanvasElement.Wo4J.exports.nyi
- python - 如何绘制移动的水平轴,python
- php - 如何使用 laravel 连接两个不同的数据库表
- php - 如何根据对另一个数组的排序方式对数组进行排序