How to compile C ++ / CLI code for Silverlight?

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

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

How to compile C ++ / CLI code for Silverlight?

+4
source share
5 answers

I think I might have gotten a VS2010 C ++ / CLI library project for assembly with links to Silverlight assemblies (only).

Update

Ok maybe . But this is not nice.

First, you must convince the C ++ compiler to NOT load the .NET Framework using an undocumented compiler. But this is not the worst part.

  • Set your C ++ / CLI project "Plain Language Time Support" to /clr:safe
  • Next, in the Links section, delete all links.
  • Next, in the properties of the C ++ / CLI project, in C ++> Command Prompt, enter /d1clr:nomscorlib /FU"C:\Program Files (x86)\Microsoft Silverlight\4.0.50917.0\mscorlib.dll"
  • Now save the project and close Visual Studio. Open .vcxproj in a text editor and change the frame 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 run Visual Studio again and create the project. You will get an error because the compiler automatically generated the file with #using<mscorlib> and the search path will first find the version of the .NET Framework.

    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 on the error to open the automatically generated file. Replace the link without specifying a path, for example. (here where you put your links, 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>

  • Fortunately, the compiler leaves your changes in place. Therefore, you should be good until nobody cleans your temp directory.

  • Construction must be successful.

Then you need to add the DLL created by the C ++ / CLI project to the Silverlight application. Note that you cannot configure the project link because VS2010 is still not convinced that C ++ / CLI is a Silverlight project. Therefore, you will need to view and add the link as an assembly file. (And it will not automatically switch between Debug and Release according to the Silverlight application).

Concluding observations

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, C ++ / CLI code successfully returned the value of C #, and the local variable in C # received the correct value. Therefore, I think it works.

I went a few more steps trying to make this work, but I do not think that they influenced the result. If you encounter errors, let me know and I will try to find out what I skipped from this answer.

+10
source

Ben Voigt, thanks for this, he also worked for me.

In addition, if your C ++ code does something specific for the C ++ language (i.e., not fully portable IL), for example, using the stack semantics for an array, you will get the following error:

 could not find assembly 'Microsoft.VisualC.dll' etc. 

If you recompile the full .NET Framework and then flush the IL code, you will find links to "'. $ ArrayType $$$ BY06 $$ CB_W modopt" or something similar. This tells you where to change the code.

I found that after installing the Silverlight SDK and it was added to "\ Program Files (x86) \ Reference Assemblies". I did not have to go through all the steps of Ben Voigt, just change the project file.

One more note: you can also use:

 <TargetFrameworkProfile>WindowsPhone71</TargetFrameworkProfile> 

if you want to target Windows Phone (first install the SDK).

+2
source

Silverlight does not support native C ++ libraries and any P / Invoke scripts due to security issues. If your library is pure .Net, you can decompile it with ILDASM and recompile Silverlight with ILASM.

+1
source

Silverlight is not a powerful development platform such as .NET, which is tightly integrated with the operating system. First of all, Silverlight should work on any operating system, so there will be no choice of the Native API in Silverlight.

Silverlight also does not fully support MSIL, so there are many problems in compiling and reprogramming at the IL level.

Can you say more about your C ++ / CLI code? Most rich Internet applications (Silverlight's goal) do not contain any high-performance computing; instead, they are simple, simple alternatives to HTML + JS. For powerful graphics, you can use Silverlight PixelShadder support.

Reflector

What you can do as an alternative, Compile your C ++ / CLI into a regular .NET library, use Reflector to parse and generate the C # source code from your DLL, this may not be perfect, but you will have most of your logic. converted back to c #.

+1
source

I managed to find a Ben Voigt solution with some minor changes in Visual Studio 2013. Here's what I did differently.

  • Upload the silverlight project you want to link to. Right click and select edit project.csproj
  • Copy the settings of the target structure. For me it was

     <TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier> <TargetFrameworkVersion>v5.0</TargetFrameworkVersion> <SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion> 
  • For the compiler switch /d1clr:nomscorlib This does not work for me. Also, a link to mscorlib for silverlight was automatically added to the corresponding output without specifying it in the command line parameters. Here's how I got around this.

  • Open a project in Just Deompile

  • Download the assembly editor plugin

  • Browse the tree for links

  • Remove the link for non silverlight mscorlib from the reflexil menu.

  • Right-click the top level of the tree to build and save it under the reflexil menu.

I did not test all the functions, but the ones I tested worked as expected.

Thanks Ben, your post saved me a lot of time. I thought I would have to migrate my library :)

0
source

All Articles