views:

143

answers:

2

I'd like to be able to compile a C/C++ library so that it runs within a safe managed runtime in the Silverlight CLR.

There are several tools for doing this with the JVM that allows C++ code to run within a CRT emulation layer (see NestedVM, LLJVM, etc), which effectively allows C++ code to be run within a Java Applet. There's even a tool for this for the Adobe Flash VM (see Alchemy).

However, I can't seem to find any tools like this for the CLR. fyi, the MSVC tools don't seem to allow for this: The /clr:pure flag will create C++ code that runs in the CLR, but it isn't safe (because the CRT isn't safe) and /clr:safe requires massive code changes (no native types, etc).

A: 

Then I think you are plain out of luck. If your code can't use the /clr:safe flag then it won't be compilable into something that can run in Silverlight. If the C++ is doing something that the CLR does not allow or support, then there is no way around this directly.

Depending what your code does, you could possibly execute it on the server and call that from Silverlight via a web service?

Colin Desmond
I understand there's no way to do this directly in the CLR or with MSVC tools. There's also no way to do it in the JVM or the Flash VM either: but NestedVM and Alchemy dodge the issue by creating a CRT emulation environment for the C++ code. No one has created a NestedVM-style tool for the CLR?
paleozogt
You can do this on the server side, either through native inter-op or via the unsafe /clr flag. The problem is you want to do this in Silverlight which does not support the unsafe code. If the JVM can do this, it could be via interop, but the C++ code itself surely does not run _in_ the JVM directly for all the same reasons it can't run in the CLR directly. I am not a Java expert, but it sounds like a server side technology for Java too?
Colin Desmond
Having now looked at NestedJVM, it is not running the C++ in the JVM, it is a translator from C++ to something that is translated to bytecode. I am not aware of anything that will do this for C++ and the CLR, sorry.
Colin Desmond
A: 

What you're looking for is not inherently possible. The problem is that native C++ types allow direct access to pointers. With pointer access, you can circumvent the .NET security model and compromise the execution environment. It's not just because the CRT is unsafe, it's because pointers are unsafe.

Billy ONeal
Have you taken a look at how NestedVM does it for the JVM? The C++ code runs inside an emulated CRT-- so the pointer access is within a giant ByteArray. No security model breaches.It should be possible with the CLR also, but I guess no one has done it.
paleozogt
Paleozogt: NestedVM is an emulation layer on top of the existing JVM. Just because it's possible does not mean it's a good idea.
Billy ONeal
@billy oneal: In your answer you say it's impossible, yet in the comment you say that its possible but its not a good idea. So what is it? If you don't know, it's better to not answer at all. Or if you have better insights, it's better to edit the anser and not add comments which override what you previously said.
Toad
@Reinier: If you want to run C++ natively on top of silverlight, what you want is impossible. If you want to fake it by setting up an emulation layer, sure, that's possible, but you could make an emultion layer do anything. If you wanted an Atari 2600 emulation layer on top of silverlight you could have one. That does *not* however mean spending the time to use such an emulation layer makes any practical sense.
Billy ONeal
@billy oneal: My question has to do with whether there are CRT emulation tools (in the style of NestedVM) for the CLR-- not your opinion of them. ;)
paleozogt
@paleozogt: Your question asks for a compiler, not an emulation layer. And you're asking for a hell of a lot more than an CLI version of the CRT.
Billy ONeal