创建一个强名称的托管(/clr)C++程序集

9

我有一个使用/clr开关的托管C++程序集,我正在尝试按照这个问题的要求签名,使用以下后期构建步骤:

sn -Ra "$(TargetPath)" MyKey.snk

然而,这会产生以下错误:
C:\Path\Assembly.dll does not represent a strongly named assembly

出了什么问题?
3个回答

11

你是否在 AssemblyInfo.cpp 中标记了程序集以进行延迟签名?

[assembly:AssemblyKeyFileAttribute("MyKey.snk")];
[assembly:AssemblyDelaySignAttribute(true)];

1
这个在托管 C++ 程序集中放在哪里? - Justin
3
这里是一个关于托管 C++ 程序集属性的问题。 - adrianm
真是太烦人了,这个没有被记录下来。花了两天多的时间试图弄清楚为什么在切换到32位配置时这不起作用,但在64位上却不需要。 - bbqchickenrobot

7
我最终搞定了这个问题 - 正如链接的问题所述,我不能仅设置Linker/Advanced/KeyFile选项并期望它起作用 - 我需要使用sn.exe来签名程序集,但是我还需要设置Linker/Advanced/KeyFile选项。
简而言之,要签署/clr程序集,您需要同时执行以下两个步骤:
  1. Linker/Advanced/KeyFile属性页面中指定密钥文件
  2. 使用sn.exe作为后置构建步骤来签署程序集
(我认为使用[assembly:AssemblyKeyFileAttribute("MyKey.snk")]等同于在项目属性对话框中设置密钥文件)。

它应该可以工作,但有时候它不行,你必须使用属性。 - surfen

6
标记的答案帮助我找到了最终解决方案(所以我给它点了个赞)。然而,我不得不花费几分钟的时间才能弄清如何在VS2010中创建AssemblyInfo.cpp。以下是更完整的解决方案。
#include "stdafx.h"

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security::Permissions;

[assembly:AssemblyKeyFileAttribute("YourAssembly.snk")];
[assembly:AssemblyDelaySignAttribute(true)];

作为一个后置构建步骤,然后运行 sn -Ra YourAssembly.dll YourAssembly.snk

我需要在 [assembly] 属性后面加上分号。 - Anton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接