除了调用命令行为程序集添加强名称之外,是否有任何API可以让您在删除其强名称后重新签名程序集?
答案 0 :(得分:3)
这取决于你被“剥夺了它的强名”的集会的意思。如果程序集没有强名称,则在使用强名称重新构建程序集之前,任何内容(甚至不是sn.exe)都不能重新签名。
但是要回答你的问题:所有强大的命名功能都是通过CLR的unmanaged strong naming API公开的。具体来说,您需要StrongNameSignatureGenerationEx
,正如您将注意到的那样,它在功能上等同于sn -R[a]
命令。
话虽这么说,但是很多更简单,更容易调用sn.exe
本身。访问非托管强名称API并不适合胆小者,因为(从.NET 4开始),您必须首先通过CLR的metahosting API。出于这个原因,你也完全不得不在非托管代码中完全执行此操作。 (我确实找到了managed wrapper from Microsoft on CodePlex,但我无法让StrongNameSignatureGenerationEx
通过它正常工作。)
但是,如果必须,请大致了解如何从非托管代码访问强命名API:
CLRCreateInstance
获取ICLRMetaHost
或ICLRMetaHostPolicy
的实例。ICLRRuntimeInfo
实例)。有很多方法可以做到这一点;有关血腥的详细信息,请参阅MSDN。ICLRRuntimeInfo
的实例后,通过调用ICLRStrongName
的{{3}}方法获取ICLRRuntime
的实例,并传入CLSID_CLRStrongName
和{{ 1}}用于类/接口标识符。现在您拥有IID_ICLRStrongName
的实例,最后可以使用它来调用ICLRStrongName
:
StrongNameSignatureGenerationEx
(可选地,如果您想使用密钥容器而不是// This is the ICLRStrongName instance you'll need. Assume GetStrongNameAPI
// corresponds to an implementation of the rough process I outlined above.
ICLRStrongName *snAPI = GetStrongNameAPI();
// You'll have to load the .snk file into memory yourself using the Win32
// file APIs, the details of which I've omitted for the sake of brevity.
BYTE *keyPairBlob = NULL;
DWORD keyPairBlobSize = 0;
LoadKeyPair(&keyPairBlob, &keyPairBlobSize);
// Once you've got the key pair blob, you can now (re-)sign the assembly
HRESULT hr = snAPI->StrongNameSignatureGenerationEx(
L"path\\to\\your\\assembly.dll",
NULL,
keyPairBlob,
keyPairBlobSize,
NULL,
NULL,
0
);
// Do whatever error handling needs to be done with the given HRESULT
密钥对进行签名,则可以将密钥容器的名称作为第二个参数传递,并将密钥对blob / size参数保留为NULL。)
底线:正如您所看到的,除非您真的必须通过强命名API,否则只需调用sn就可以更轻松地(重新)签署程序集。 exe本身。
答案 1 :(得分:2)
您可以查看mscoree
's strong name APIs,但我不会推荐它。