我不认为,在 Inno Setup Pascal 脚本中检索强名称的本地方法。
但是,您可以使用简单的 PowerShell 命令来检索强名称。
([Reflection.Assembly]::ReflectionOnlyLoadFrom('My.dll')).FullName
结合这两个问题:
你会得到如下代码:
function GetAssemblyStrongName(FileName: string): string;
var
TmpFileName: string;
Args: string;
StrongName: AnsiString;
ResultCode: Integer;
begin
TmpFileName := ExpandConstant('{tmp}') + '\strong_name.txt';
Args :=
Format('-ExecutionPolicy Unrestricted -Command "Set-Content -Path "%s" -NoNewline ' +
'-Value ([Reflection.Assembly]::ReflectionOnlyLoadFrom(''%s'')).FullName"', [
TmpFileName, FileName]);
if (not Exec('powershell.exe', Args, '', SW_HIDE, ewWaitUntilTerminated, ResultCode)) or
(ResultCode <> 0) or
(not LoadStringFromFile(TmpFileName, StrongName)) then
begin
RaiseException(Format('Error retrieving strong name of "%s"', [FileName]));
end;
Result := StrongName;
DeleteFile(TmpFileName);
end;
虽然实际上,您不能使用 Pascal 脚本,因为您需要在编译时使用强名称,而 Pascal 脚本仅在安装时执行。在某些情况下,您可以使用scripted constant,但StrongAssemblyName parameter 不支持它。
同样,您必须使用preprocessor。
Pascal 脚本代码转换为预处理器,如:
#define GetAssemblyStrongName(FileName) \
Local[0] = AddBackslash(GetEnv("TEMP")) + "strong_name.txt", \
Local[1] = \
"-ExecutionPolicy Unrestricted -Command """ + \
"Set-Content -Path '" + Local[0] + "' -NoNewline -Value " + \
"([Reflection.Assembly]::ReflectionOnlyLoadFrom('" + FileName + "')).FullName" + \
"""", \
Exec("powershell.exe", Local[1], SourcePath, , SW_HIDE), \
Local[2] = FileOpen(Local[0]), \
Local[3] = FileRead(Local[2]), \
FileClose(Local[2]), \
DeleteFileNow(Local[0]), \
Local[3]
您可以在StrongAssemblyName 参数中使用它,例如:
[Files]
Source: "My.dll"; DestDir: "{app}"; \
StrongAssemblyName: "{#GetAssemblyStrongName('My.dll')}"
这会被预处理为:
[Files]
Source: "My.dll"; DestDir: "{app}"; \
StrongAssemblyName: "My, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2271ec4a3c56d0bf"
尽管您generate the [Files] section completely by a preprocessor,您当前的评论代码现在可以工作,因为您使用的语法实际上调用GetAssemblyStrongName 预处理器宏而不是GetAssemblyStrongName Pascal 脚本函数。
请注意,上面的宏使用 C 风格的字符串,所以它必须在 Pascal 风格的pragma 指令之外:
; here
#pragma parseroption -p-
; not here
#pragma parseroption -p+
; or here