开发者

Creating a strongly-named managed (/clr) C++ assembly

I have a managed C++ assembly using the /clr switch that I am trying to sign as per this question with the following post-build step:

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

However this is giving the following开发者_运维百科 error:

C:\Path\Assembly.dll does not represent a strongly named assembly

What is going wrong?


Have you marked the assembly for delay signing in AssemblyInfo.cpp?

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


I figured this one out in the end - as per the linked question I cannot just set the Linker/Advanced/KeyFile option and expect it to work - I need to use sn.exe to sign the assembly, however I also still need the Linker/Advanced/KeyFile option to be set.

In short to sign a /clr assembly you need to both:

  1. Specify a keyfile in the Linker/Advanced/KeyFile properties page
  2. Use sn.exe to sign the assembly as a post-build step

(I believe that using the [assembly:AssemblyKeyFileAttribute("MyKey.snk")] is equivalent to setting the keyfile in the project properties dialog).


The marked answer helped to reach the final solution (so it receives a +1 from me).

However had to spent several frustrating minutes figuring out how to create an AssemblyInfo.cpp in VS2010.

Below is the "more" complete answer to the problem.

#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)];

Then as a post-build step, run sn -Ra YourAssembly.dll YourAssembly.snk

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜