在Inno Setup中检索.NET程序集的强名称 [英] Retrieve strong name of .NET assembly in Inno Setup
问题描述
我需要将这些DLL文件安装到GAC。
I need to install these some DLL files to the GAC.
我使用预处理器为这些DLL生成 [Files]
节条目。我需要为 StrongAssemblyName
参数提供一个值。
I use preprocessor to generate [Files]
section entries for these DLLs. And I need to provide a value for the StrongAssemblyName
parameter.
问题
- 是否可以从Pascal脚本中自动检索DLL的
StrongAssemblyName
? - 如果没有,可以创建一个字典,这样我就可以使用程序集名称作为关键字在字典中查找,然后使用硬编码的值字符串,该字符串将在自动建立行
来源:路径; DestDir
- Can I automatically retrieve the
StrongAssemblyName
of a DLL from a Pascal Script? - If not, can one create a dictionary so I could look up in the dictionary using the assembly name as key, and then have hardcoded value strings that would get used when automatically building up the line
Source: path; DestDir
如果可能,我希望使用第一种解决方案。
I would prefer the first solution, if possible.
推荐答案
我不认为,有一种本地方法可以在Inno Setup Pascal脚本中检索强名称。
I do not think, there's a native way to retrieve a strong name in Inno Setup Pascal Script.
但是,您可以使用简单的PowerShell命令来检索强名称。
But, you can use a simple PowerShell command to retrieve the strong name.
([Reflection.Assembly]::ReflectionOnlyLoadFrom('My.dll')).FullName
结合以下两个问题:
- How can I see the strong name of my assembly?
- How to get an output of an Exec'ed program in Inno Setup?
您得到的代码如下:
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脚本仅在安装时执行。在某些情况下,您可以使用脚本常量,但是在 StrongAssemblyName
参数。
Though actually, you cannot use Pascal Scripting, as you need the strong name on compile-time, and the Pascal Scripting is executed on install-time only. In some cases, you can use a scripted constant, but it's not supported in the StrongAssemblyName
parameter.
再次,您必须使用预处理器。
Pascal脚本代码可以转换为预处理器,例如:
The Pascal Script code translates to the preprocessor like:
#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
中使用它像这样的参数:
You can use it in the StrongAssemblyName
parameter like:
[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"
不过,当您生成 [文件]
部分完全由预处理器完成,您注释中的当前代码现在可以正常工作了,因为您所使用的语法实际上调用了 GetAssemblyStrongName
预处理程序宏,而不是 GetAssemblyStrongName
Pascal脚本函数。
Though, as you generate the [Files]
section completely by a preprocessor, your current code from your comment will now work, as the syntax, that you have used, actually calls GetAssemblyStrongName
preprocessor macro and not GetAssemblyStrongName
Pascal Script function.
请注意,上面的宏使用C样式的字符串,因此它必须是外部 Pascal样式的 pragma
指令:
Note that the macro above uses C-style strings, so it has to be outside the Pascal-style pragma
directives:
; here
#pragma parseroption -p-
; not here
#pragma parseroption -p+
; or here
这篇关于在Inno Setup中检索.NET程序集的强名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!