如何在.NET 4中使用ICLRStrongName?

发布于 2024-09-28 05:39:29 字数 219 浏览 0 评论 0原文

与我之前的帖子相关,我正在转向 .NET 4。我发现使用以前的 StrongName.h 在非托管代码中获取程序集签名密钥现在已被弃用,我需要使用 MetaHost.h 和 ICLRStrongName:: StrongNameTokenFromAssembly。

之前的 StrongNameTokenFromAssembly(..) 非常简单,现在这个新的没有关于如何使用的文档。有人有这个界面的经验吗?

Related to my previous posts I'm moving to .NET 4. I've found that using the previous StrongName.h to get my assembly signing key in unmanaged code is now deprecated, and I need to use MetaHost.h and ICLRStrongName::StrongNameTokenFromAssembly.

The previous StrongNameTokenFromAssembly(..) was very straight forward, now this new one has no documentation on how to use. Does anyone have experience with this interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-10-05 05:39:29

哇...这需要大量的黑客工作。开始了!

ICLRMetaHost *pMetaHost = NULL;
HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost,
               IID_ICLRMetaHost, (LPVOID*)&pMetaHost);  
if(hr == S_OK)
{
  WCHAR version[100];
  DWORD size;

  hr == pMetaHost->GetVersionFromFile(MyGetApplicationExecutablePath().c_str(), (LPWSTR) &version, &size);
  if(hr == S_OK)
  {
     LPWSTR assemblyVer = version;
     ICLRRuntimeInfo *pRuntimInfo = NULL;

     hr = pMetaHost->GetRuntime(assemblyVer, IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimInfo);
     if (hr == S_OK)
     {
        ICLRStrongName *pStrongName = NULL;
        hr = pRuntimInfo->GetInterface(CLSID_CLRStrongName, IID_ICLRStrongName, (LPVOID*)&pStrongName);

        if(hr == S_OK)
        {
           pStrongName->StrongNameTokenFromAssembly(MyGetApplicationExecutablePath().c_str(), &token, &len);
           DWORD verified = 0;
           BOOLEAN sigVerified = pStrongName->StrongNameSignatureVerification(MyGetApplicationExecutablePath().c_str(), SN_INFLAG_FORCE_VER  , &verified);
           if (!verified)
           {
              //Do something nasty here if the Signature verification failed
           }
           pStrongName->StrongNameFreeBuffer(token);
        }
     }
  }

}

Wow... that required a lot of hacking around. Here we go!

ICLRMetaHost *pMetaHost = NULL;
HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost,
               IID_ICLRMetaHost, (LPVOID*)&pMetaHost);  
if(hr == S_OK)
{
  WCHAR version[100];
  DWORD size;

  hr == pMetaHost->GetVersionFromFile(MyGetApplicationExecutablePath().c_str(), (LPWSTR) &version, &size);
  if(hr == S_OK)
  {
     LPWSTR assemblyVer = version;
     ICLRRuntimeInfo *pRuntimInfo = NULL;

     hr = pMetaHost->GetRuntime(assemblyVer, IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimInfo);
     if (hr == S_OK)
     {
        ICLRStrongName *pStrongName = NULL;
        hr = pRuntimInfo->GetInterface(CLSID_CLRStrongName, IID_ICLRStrongName, (LPVOID*)&pStrongName);

        if(hr == S_OK)
        {
           pStrongName->StrongNameTokenFromAssembly(MyGetApplicationExecutablePath().c_str(), &token, &len);
           DWORD verified = 0;
           BOOLEAN sigVerified = pStrongName->StrongNameSignatureVerification(MyGetApplicationExecutablePath().c_str(), SN_INFLAG_FORCE_VER  , &verified);
           if (!verified)
           {
              //Do something nasty here if the Signature verification failed
           }
           pStrongName->StrongNameFreeBuffer(token);
        }
     }
  }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文