【问题标题】:Linking to presentationcore.dll in mixed mode DLL在混合模式 DLL 中链接到 Presentationcore.dll
【发布时间】:2012-01-17 08:36:09
【问题描述】:

我们有一个用 C++ 编写的混合模式 DLL,它包装原生 C++ DLL 并公开托管类。在公开的托管类中,我们使用 Vector3D 等类型的方法参数,它们是 PresentationCore.DLL 的一部分。

因此,混合模式 C++ 代码需要引用 PresentationCore.DLL。我们通过

#using <PresentationCore.dll>

这需要项目的搜索路径包含 PresentationCore.dll 所在的文件夹。

这很糟糕,因为这些文件夹在不同的机器上有所不同,我们的项目需要在几台机器上编译而不更改。目前,我们已经通过在我们的代码库中包含一个 PresentationCore.dll 的副本来解决这个问题,这显然不是一个好的解决方案。

我将不胜感激我们如何绕过指定应该可以通过 GAC 完全访问的 DLL 的显式路径的建议。

【问题讨论】:

  • GAC 中不是presentationcore 吗? V3 和 V4(x86 和 AMD 64)在我的 GAC 中。是否需要为 MC++ 中的 GACed 程序集添加搜索路径
  • 是的,它在 GAC 中。但是,C++ 编译器似乎没有看到那里,因为如果 dll 不在直接搜索的路径中,则 using 语句将失败。

标签: c++ gac using managed mixed-mode


【解决方案1】:

不要做#using &lt;PresentationCore.dll&gt;。您需要右键单击该项目,转到References...,单击Add New Reference...,然后从.Net 选项卡中选择PresentationCore。我得到了提示:

http://msdn.microsoft.com/en-us/library/aa970266.aspx

【讨论】:

    【解决方案2】:

    呸,我搞定了。我有一个原生项目,我遇到了同样的问题,我需要使用 PresentationCore 中的 HwndSource,对 hwnds 进行一些分析。我所做的是将我的项目保留为本机(没有 /clr 开关),然后对于具有使用 HwndSource 的功能的源文件,我添加了 /clr 开关,这样我就可以包含其他源的设置,例如异常处理等等。

    #using <System.dll>
    #using <WindowsBase.dll>
    #using <PresentationFramework.dll>
    #using <PresentationCore.dll>
    #using <UIAutomationProvider.dll>
    #using <UIAutomationTypes.dll>
    

    这很好用,您将不会获得任何 Intellisense 支持。输出中的一些警告,如果你能忍受,这是给你的。

    【讨论】:

      【解决方案3】:

      GAC 位于 %windir%\Assembly\ 然后是子目录 GAC_32 或 GAC_64

      在这种情况下,我会使用符号链接。在本地目录中创建指向 DLL 的链接。然后从该链接编译。

      是的,您必须为每台机器更改它。但是你只需要一行批处理脚本就可以了。

      考虑这个在我的机器上运行。

      C:\temp>for /f "tokens=*" %f in ('dir \windows\assembly\presentationcore.dll  /s/b') do @echo %f
      C:\windows\assembly\GAC_32\PresentationCore\3.0.0.0__31bf3856ad364e35\PresentationCore.dll
      C:\windows\assembly\GAC_64\PresentationCore\3.0.0.0__31bf3856ad364e35\PresentationCore.dll
      

      选择你需要的(比如 GAC_64)并设置一个链接 - 这就是你应该需要的。

      @for /f "tokens=*" %f in ('dir \windows\assembly\presentationcore.dll  /s/b') do @echo %f | @findstr GAC_64 | mklink .\presentationCore.dll "%f"
      

      【讨论】:

      • 从不在您的项目中引用来自 GAC 的程序集。 GAC 是一个部署细节。 GAC 中的程序集副本与参考程序集相同,尤其是从 .NET 4 开始。
      • @HansPassant 谢谢 - 请您提供参考。
      猜你喜欢
      • 2011-09-22
      • 2014-04-07
      • 2011-02-11
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2016-01-14
      相关资源
      最近更新 更多