如何为Silverlight编译C ++ / CLI代码? [英] How do I compile C++/CLI code for Silverlight?

查看:267
本文介绍了如何为Silverlight编译C ++ / CLI代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++ / CLI库,我想在一个Silverlight应用程序中使用。应该可以用任何.NET语言编写Silverlight的代码,但到目前为止,我只是编制了C#。 Silverlight似乎不能使用为.NET编译的DLL。



我使用的是Visual Studio 2010和Silverlight 4. Silverlight唯一可用的新项目是C#项目。



如何为Silverlight编译C ++ / CLI代码?

解决方案

我想我可能得到一个VS2010 C ++ / CLI类库项目,引用(仅)Silverlight程序集。



< h2>更新

好的,可能。但它不是很好。

首先,你必须说服C ++编译器不加载.NET框架,使用一个没有文档的编译器开关。






  • 将C ++ / CLI项目的Common Language Runtime Support设置为 / clr :safe

  • 接下来,在参考下,删除所有参考。

  • C ++ / CLI项目属性,在C ++>命令行下输入 / d1clr:nomscorlib / FUC:\Program Files(x86)\Microsoft Silverlight\4.0.50917.0\ mscorlib.dll

  • 现在,保存项目并退出Visual Studio。在文本编辑器中打开.vcxproj,并更改框架版本设置。您希望它与C#Silverlight项目相同:



    < TargetFrameworkIdentifier> Silverlight< / TargetFrameworkIdentifier>
    < TargetFrameworkVersion> v4.0< / TargetFrameworkVersion>
    < SilverlightVersion> $(TargetFrameworkVersion)< / SilverlightVersion> ;


  • 现在,重新打开Visual Studio,并构建项目。您将收到一个错误,因为编译器使用#使用< mscorlib> 自动生成一个文件,搜索路径首先找到.NET Framework版本。



    Silverlight,Version = v4.0.AssemblyAttributes.cpp(1):致命错误C1197:无法引用c:\windows \microsoft.net\ framework \v4.0.30319\ mscorlib.dll',因为程序已经引用了'c:\program文件(x86)


  • 双击错误以打开自动生成的文件。将无路径引用替换为



    #using< c:\program files(x86)\ microsoft silverlight\4.0.50917.0 \mscorlib.dll>



    #using< c:\program文件(x86)\microsoft silverlight\4.0.50917.0\系统.dll>



    #using ; c:\program files(x86)\microsoft silverlight\4.0.50917.0\System.Core.dll>


  • 幸运的是,编译器将您的更改保留在原地。


  • 建立应该会成功。



然后,您需要将C ++ / CLI项目创建的DLL添加到您的Silverlight应用程序。注意,你不能设置项目引用,因为VS2010仍然不相信C ++ / CLI是一个Silverlight项目。因此,您必须浏览并将引用添加为装配文件。 (它不会自动在调试和发布之间切换以匹配Silverlight应用程序)。



最终注释



我得到它在Debug模式下运行一个空的Silverlight应用程序,并在C ++ / CLI代码中间的断点处停止。此外,C ++ / CLI代码成功地将值返回给C#,C#中的局部变量接收到正确的值。所以我想它的工作。



我经历了一些更多的步骤,试图使这项工作,但我不认为他们影响的结果。如果你遇到错误,但让我知道,我会尽力弄清楚我从这个答案中省略了什么。


I have a C++/CLI library that I would like to use in a Silverlight application. It is supposed to be possible to write code for Silverlight in any .NET language, but so far I've only worked out how to compile C#. Silverlight does not seem to be able to use DLLs compiled for .NET.

I'm using Visual Studio 2010 and Silverlight 4. The only new projects available for Silverlight are C# projects. Porting the code to C# is not a practical option.

How do I compile C++/CLI code for Silverlight?

解决方案

I think I may have gotten a VS2010 C++/CLI class library project to build with references to (only) Silverlight assemblies.

Update

Ok, it is possible. But it is not nice.

First, you must convince the C++ compiler to NOT load the .NET Framework, using an undocumented compiler switch. But that's not the worst part.

  • Set your C++/CLI project "Common Language Runtime Support" to /clr:safe
  • Next, under References, remove all references.
  • Next, in the C++/CLI project properties, under C++ > Command Line, enter /d1clr:nomscorlib /FU"C:\Program Files (x86)\Microsoft Silverlight\4.0.50917.0\mscorlib.dll"
  • Now, save the project and exit Visual Studio. Open the .vcxproj in a text editor, and change the framework version setting. You want it to be the same as a C# Silverlight project:

    <TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>

  • Now, reopen Visual Studio, and build the project. You'll get an error because the compiler auto-generated a file with #using<mscorlib> and the search path finds the .NET Framework version first.

    Silverlight,Version=v4.0.AssemblyAttributes.cpp(1): fatal error C1197: cannot reference 'c:\windows\microsoft.net\framework\v4.0.30319\mscorlib.dll' as the program has already referenced 'c:\program files (x86)\microsoft silverlight\4.0.50917.0\mscorlib.dll'

  • Double-click the error to open the auto-generated file. Replace the path-less reference with e.g. (here's where you put your references, not in the project properties)

    #using <c:\program files (x86)\microsoft silverlight\4.0.50917.0\mscorlib.dll>

    #using <c:\program files (x86)\microsoft silverlight\4.0.50917.0\System.dll>

    #using <c:\program files (x86)\microsoft silverlight\4.0.50917.0\System.Core.dll>

  • Luckily, the compiler leaves your changes in-place. So you should be good as long as no one cleans your temp directory.

  • Building should now succeed.

Then, you need to go add the DLL created by the C++/CLI project to your Silverlight application. Note that you can't set up a project reference, because VS2010 still isn't convinced that the C++/CLI is a Silverlight project. So you'll have to browse and add the reference as an assembly file. (And it won't automatically switch between Debug and Release to match the Silverlight application).

Final Notes

I got it to run an empty Silverlight application in Debug mode and stop at a breakpoint in the middle of C++/CLI code. Also the C++/CLI code successfully returned a value to C# and the local variable in C# received the correct value. So I guess it's working.

I went through a bunch more steps trying to make this work, but I don't think they affected the outcome. If you run into errors, though, let me know and I'll try to figure out what I omitted from this answer.

这篇关于如何为Silverlight编译C ++ / CLI代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆