如何使用ICLRStrongName :: StrongNameSignatureVerificationEx方法来标识延迟签名的程序集

时间:2014-02-13 12:59:44

标签: c# .net .net-assembly

有人可以建议如何使用 ICLRStrongName :: StrongNameSignatureVerificationEx 方法来识别延迟签名的程序集。我在互联网上找不到任何例子。我不明白如何使用这种方法。

请不要向我推荐任何链接,我对网上提供的不同建议和不同链接感到沮丧。任何人都可以为此提供代码示例。

1 个答案:

答案 0 :(得分:11)

我遇到了同样的问题,终于找到了调查的时间。我真的不明白为什么微软没有花时间写一份足够的文档。

以下链接非常有用,并且还包含一个示例项目,即使它也没有回答所有问题:http://clractivation.codeplex.com/

我将尝试用简短的代码示例回答所有问题。您可以使用类似的东西来验证程序集的强名称:

public bool VerifyStrongName(string assemblyPath, bool force)
{
  if (string.IsNullOrEmpty(assemblyPath))
    throw new ArgumentException(string.Empty, "assemblyPath");

  var host = HostingInteropHelper.GetClrMetaHost<IClrMetaHost>();

  var bufferSize = 100;
  var version = new StringBuilder(bufferSize);
  var result = host.GetVersionFromFile(Assembly.GetExecutingAssembly().Location, version, ref bufferSize);
  if ((HResult)result != HResult.S_OK)
    throw new COMException("Error", result);

  IClrRuntimeInfo info = host.GetRuntime(version.ToString(), new Guid(IID.IClrRuntimeInfo)) as IClrRuntimeInfo;
  ICLRStrongName sn = info.GetInterface(new Guid(CLSID.ClrStrongName), new Guid(IID.IClrStrongName)) as ICLRStrongName;

  var verResult = sn.StrongNameSignatureVerificationEx(assemblyPath, Convert.ToByte(force));
  return Convert.ToBoolean(verResult);
}

您可以在 ClrActivation 示例项目中找到此示例中使用的所有分类和接口。

另外,我将 HRESULT 类更改为枚举...

  /// <summary>
  /// A set of common, useful HRESULTS, and related functionality
  /// </summary>
  public enum HResult
  {
    /// <summary>
    /// OK/true/Success
    /// </summary>
    S_OK = 0,
    /// <summary>
    /// False
    /// </summary>
    S_FALSE = 1,
    /// <summary>
    /// The method is not implemented
    /// </summary>
    E_NOTIMPL = unchecked((int)0x80004001),
    /// <summary>
    /// The interface is not supported
    /// </summary>
    E_NOINTERFACE = unchecked((int)0x80004002),
    /// <summary>
    /// Bad Pointer
    /// </summary>
    E_POINTER = unchecked((int)0x8004003),
    /// <summary>
    /// General failure HRESULT
    /// </summary>
    E_FAIL = unchecked((int)0x8004005),
    /// <summary>
    /// Invalid Argument
    /// </summary>
    E_INVALIDARG = unchecked((int)0x80070057),
    /// <summary>
    /// Insufficient buffer
    /// </summary>
    ERROR_INSUFFICIENT_BUFFER = unchecked((int)0x8007007A),
    /// <summary>
    /// HRESULT for failure to find or load an appropriate runtime
    /// </summary>
    CLR_E_SHIM_RUNTIMELOAD = unchecked((int)0x80131700),

    SEVERITY = unchecked((int)0x80000000)
  }

...并添加静态分类 CLSID IID (花了一些时间找到正确的CLSID):

  public static class IID
  {
    public const string IClrRuntimeInfo = "BD39D1D2-BA2F-486A-89B0-B4B0CB466891";
    public const string IClrMetaHost = "D332DB9E-B9B3-4125-8207-A14884F53216";
    public const string IClrStrongName = "9FD93CCF-3280-4391-B3A9-96E1CDE77C8D";
    public const string IEnumUnknown = "00000100-0000-0000-C000-000000000046";
  }

  public static class CLSID
  {
    public const string ClrStrongName = "B79B0ACD-F5CD-409b-B5A5-A16244610B92";
  }

我希望我能帮助你。如果您还有其他问题,请不要犹豫。

相关问题