Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Native Extensions for Silverlight (NESL)?

Blogs

Mike Taulty's Blog

Elsewhere

Archives

Silverlight applications are written as managed code which runs on a CLR which sandboxes the code.

In Silverlight 1 and 2 this was easy to understand because all applications were running in a browser and it’s common to expect code running in a browser to be security sandboxed such that (for example) a Silverlight application could not read a file from anywhere on the hard-drive unless the user had first consented to a file dialog requesting access to that specific file.

Equally, the Silverlight application could only raise that file dialog if the user had caused it to be raised by some direct interaction with the UI.

It’s fairly simple once you get used to it although an early area for confusion was for developers coming from the “full” .NET Framework and CLR who expected the sandbox to be configurable via policy and so on whereas it isn’t with Silverlight.

With Silverlight 3, things initially seemed a little more confusing because applications could now run as “Out Of Browser” (OOB) which opened the question as to whether that meant that they were running with less sandboxing but, in reality, they weren’t and the situation was unchanged.

With Silverlight 4 things did change in that an out-of-browser application could mark itself as wanting to be “trusted” at the point where it is installed which meant that the user then sees one of 2 dialogs;

image

depending upon whether the application has been signed or not (verified or unverified in the terminology) and, in the latter case the application is free to display a nice logo and also to take part in Silverlight’s out-of-browser automatic update capability so going verified is highly recommended.

Regardless, if the user installed the “trusted” application then it now ran in a relaxed sandbox where it got more permissions as below taken from one of my own PowerPoint slides on the topic;

image

This is true cross-platform – i.e. for both Windows applications and OS X applications. However, there is still a sandbox in place and, for instance, an application still can’t just go and read the contents of c:\temp\ even if it is running in a trusted environment.

However, there’s one extra piece of the puzzle which is that on Windows only the sandbox has a hole cut into it and that hole is COM shaped;

image

and, because there’s no trust model for COM components this effectively means that a Silverlight application running out-of-browser and trusted on Windows has full access to the machine and can go and read the contents of c:\temp\ if it can find a scriptable COM component to do its dirty work for it Winking smile

That’s great if;

  1. You were happy sacrificing the cross-platform requirement.
  2. Your users were happy to installed a “trusted” application.
  3. You could find a COM component already installed on the machine that did what you wanted that exposed its capabilities via IDispatch.

So, for example, it’d be a pretty safe bet if you wanted some kind of Office 2010 interoperability with Outlook, Excel or similar.

However, there’s a heck of a lot of Windows functionality that isn’t exposed via well-known-COM-components and so you’re left pondering what the best route is to take.

The “full” .NET framework/CLR already solved this problem in that, from day 1, it offered interoperability with both existing COM components (via more than just IDispatch) alongside interoperability with existing Windows DLLs via Platform Invocation (or PInvoke).

With Silverlight 5 (see keynote) it sounds like there will be a couple of new developments in this area;

  1. Silverlight 5 looks to also be getting PInvoke capabilities so that you’ll be able to call into DLLs.
  2. Silverlight 5 looks to be be getting the ability to run a trusted application inside the browser subject to some criteria being met about the application’s provenance.

That’s great but, in the meantime, there’s an “interesting” development over at the MSDN Code Gallery called the Native Extensions for Silverlight (NESL) which has taken an approach of making specific APIs easier to call from Silverlight. The approach is;

  1. Write COM components exposing the functionality of the APIs that you want via IDispatch.
  2. Write Silverlight library wrapper classes that make calling those COM components easy.

and the particular APIs covered at the time of writing look to be;

  1. Sensors
  2. Speech
  3. Portable Devices
  4. Taskbar Integration
  5. Webcam encoding and window capture
  6. Low Level Windowing APIs (i.e. WM_* style messages)

and ( from the discussions forum ) it looks like this has been tested/built for Windows 7 only at this point ( makes sense for some of those APIs are they are Windows 7 specific ).

If a Silverlight application is going to take a dependency on a library like this then it means that the COM components have to be pre-installed ( which I think requires admin rights ) and that’s done one of two ways by this library;

  1. Have the user pre-run some .MSI installer.
  2. Have the application use provided library classes which, effectively, provide a programmatic way to run the installer.

That’s quite a big deviation from how a standard Silverlight application operates plus there’s then also the consideration around how you would then version this kind of dependency as it changes over time.

That does lead me to something of a “sidebar”…

Sidebar – Should We Be Doing This Stuff?

My view on the COM interop functionality of Silverlight 4 has always been;

use sparingly

I’d say that if you’re writing an application with Silverlight and you find that there’s some small, well-contained bits of functionality that you can add by (e.g.) integrating with Outlook or offering an “Export to Excel” piece of functionality then that’s fine but if you’re writing reams of interop code then I suspect you’re using the wrong technology and you probably need to start thinking about this guy;

image

That’s what I’d recommend. You get all the goodness of .NET 4.0 plus you have all the flexibility of COM interop and PInvoke and you can go with whatever installation experience works best for the application.

So, generally, that would be the direction I’d take rather than getting into the distribution of COM wrappers that are likely to cause headaches down the line.

Trying Out the NESL

With that said, I thought I’d try out the NESL anyway and see how it works out and so I downloaded the bits in source form rather than runtime form. Downloading that way gave me;

image

so the runtime is also provided and I opened up the VS solution file, unloaded the projects within the samples folder and then built the rest of it;

image

and hit a post build error which I figured out was because the project has been set up to copy its outputs into a folder;

SilverlightLibraries\Binaries\Debug ( or release )

and the Debug part doesn’t exist so the post-build steps fail until you create that folder.

With everything built, I thought I’d try out the installation experience and so I made a new Silverlight project, set it up to be an elevated out-of-browser application and then added a reference to the NESL Microsoft.Silverlight.Windows assembly.

Next step is to add the MSI of the installer for NESL to my project so I added it in and set its build action to be Content (note – this is just one way of getting the MSI installer run );

image

bundling an MSI file into a Silverlight XAP felt a little weird so after a stiff drink Winking smile I carried on and put a button onto a form and wrote a tiny bit of code behind it;

      if (!Microsoft.Silverlight.Windows.Installer.CheckNESLInstalled(1, 0))
      {
        Microsoft.Silverlight.Windows.Installer.InstallNESL(
          new Uri("NESLSetup.msi", UriKind.Relative),
          false, // is the installation remote?
          true); // do I want to show progress? You BET!

        MessageBox.Show("NESL Installed!");
      }

and then I ran it and clicked the button and saw;

image

and then;

image

oh my. Time for another stiff drink before hitting the Yes button Confused smile which leads to;

image

and then my own MessageBox (note that the installation process was synchronous);

image

Ok, I’m installed and I note that clicking on my button again has no effect as I would hope for now that the installation is done.

Now time to try out an API or two. I figured Speech would be a good place to start so it’s time for a TextBox and a Button;

image

and a reference to the NESL library for speech;

image

and then I played around with a few bits of code. This one was to figure out what voices are available;

      SpeechSynthesizer synth = new SpeechSynthesizer();

      foreach (var item in synth.GetVoices())
      {
        string speech = string.Format("I have a voice called {0} which is {1} years old and is {2}",
          item.Name,
          item.Age.ToString(),
          item.Gender.ToString());

        synth.Speak(speech);
      }    
on my system this says;

“I have a voice called Microsoft Anna which is Adult years old and is female”

and getting default speech is crazily easy;

  SpeechSynthesizer synth = new SpeechSynthesizer();

      synth.SpeakAsync(txtSpeech.Text);

I figured I’d have a play with TaskBar integration and so I went ahead and brought in that library and made myself a very quick “media player” in Silverlight and added some bits of code to set up the task bar;

image

firstly setting up icon overlays for when the video is playing or paused;

    void ChangeTaskBarIconOverlay()
    {
      byte[] iconBits = this.GetImageBitsForCurrentPlayState();

      TaskbarButton.Current.SetOverlayIcon(iconBits, "Play State", 
        ButtonImageDataType.PNG);
    }
where the GetImageBitsForCurrentPlayState function just returns a byte[] of a 16x16 PNG resource based on whether the video is currently playing or paused.

I can also update the progress indicator as the video plays ( this function is called on a timer tick );

 void OnTick(object sender, EventArgs e)
    {
      if (this.playState == PlayState.Idle)
      {
        // NB: This line of code fails...maybe a bug?
        // TaskbarButton.Current.SetProgressState(TaskbarItemProgressState.None);

        // Doing the dynamic dispatch myself...
        TaskbarButton.Current.COMObject.SetProgressState(
          (int)TaskbarItemProgressState.None);
      }
      else
      {
        TaskbarButton.Current.COMObject.SetProgressState(
          (int)TaskbarItemProgressState.Normal);

        TaskbarButton.Current.SetProgressValue(
          (ulong)this.mediaElement.Position.TotalSeconds,
          (ulong)this.mediaElement.NaturalDuration.TimeSpan.TotalSeconds);
      }
    }
and that gives me an overlay and progress indication on the task bar;

image

and I can also create Play/Pause/Stop buttons on the taskbar – as an example for the Stop button;

   void CreateTaskbarButtons()
    {
      ThumbbarButton button = TaskbarButton.Current.CreateThumbbarButton(1);
      button.Tooltip = "Stop";
      button.ImageDataType = ButtonImageDataType.PNG;
      button.Image = GetImageBitsForResource(
        "SilverlightApplication11;component/StopHS.png");

      TaskbarButton.Current.ShowThumbbarButtons();
    }
which gives me;

image

but I was unsure of how to handle the button click event as it doesn’t seem to be surfaced from the wrapper and the source code doesn’t seem to include the source code for the COM objects that the libraries are wrapping so that led me to a bit of a dead end .

However, the docs revealed that in order to handle the button click you have to use the interop libraries to pick up a WM_COMMAND message. I left this as an exercise Smile

The other API which felt like it would be of interest was the video encoding API which offers the chance to output H.264 video from video/audio captured from a Silverlight webcam and microphone.

This is “non trivial” ( practically impossible ) in Silverlight 4 today and so this API offers some possibility for people wanting to do that kind of capture.

There’s a pretty complete sample of this in the docs for the NESL API and I won’t experiment with it for this post because writing Video/Audio sinks for the webcam/mic is quite a lot of code ( there are some samples along those lines on this blog site from when Silverlight 4 first came out ) and so this post would become huge but this might be one area where you look to the NESL for help.

Wrapping Up

It’s interesting to see the NESL libraries and experiment with them a little and it’s clever stuff but I suspect that it’s not for most Silverlight applications because of its need for (an admin driven) pre-installation and because it doesn’t quite fit into the model for updating a Silverlight application. I’m also not sure how it works in a 32/64-bit environment.

Generally, I’d say if you need this kind of functionality then you’re perhaps better looking to WPF but I’m sure there will be certain cases where Silverlight developers will pick this library up as it gives them “just that one additional capability” that their app needs so it’s an interesting development for those edge cases until Silverlight 5 comes along and offers PInvoke capabilities.


Posted Fri, Dec 31 2010 4:53 PM by mtaulty

Comments

Rod Mac wrote re: Native Extensions for Silverlight (NESL)?
on Fri, Dec 31 2010 7:28 PM

What plans are there for integrating SL5 controls and content directly into WinForms apps (i.e. without using a browser control and having to round trip to the server)?

Alan Cobb wrote re: Native Extensions for Silverlight (NESL)?
on Sat, Jan 1 2011 9:23 PM

Hi Mike,

I think exposing more local functionality from the Windows-OS up to Silverlight OOB apps is a great, even exciting, idea.  Why?

Although I originally started with WPF, I switched to mostly using Silverlight when SL2 came out.  Although WPF and SL are similar, there are enough differences in syntax, tooling and libraries that I find it irritating to have to switch between them.  Now that I'm used to Silverlight's syntax limitations and work-arounds I just want to exploit that SL-specific experience as much as possible.

Although the SL4 UI still lacks things I want (like better text quality, reportedly coming to some degree in SL5), for the most part I have what I need, UI-wise, to create the programs I want.  But the missing piece, for a certain class of programs, is better integration with the local OS.  Calling COM from SL, possibly via NESL can provide that missing piece, in some cases.

I can see situations today where it makes sense to write Windows-centric apps with WPF, WinForms or even raw Win32 or MFC, but my personal default UI technology is Silverlight.

Alan Cobb

http://www.alancobb.com/blog

mtaulty wrote re: Native Extensions for Silverlight (NESL)?
on Sun, Jan 2 2011 3:47 PM

Hi Alan,

Yes, I definitely take your point around the idea of Silverlight being your default choice for UI work and so you, naturally, look to get the most out of Silverlight rather than having to switch to another stack even if that stack (WPF) has a tonne in common with SL.

Mike.

Steo wrote re: Native Extensions for Silverlight (NESL)?
on Mon, Jan 3 2011 9:38 AM

This is not very good .... Silverlight is not going to be cross-platform .....

Andrew Marshall wrote re: Native Extensions for Silverlight (NESL)?
on Mon, Jan 3 2011 12:42 PM

At the risk of making an obvious plug, I did some work in this area a while ago, using the COM layer to write some image processing code using CUDA. It's not at all practical because of the amount of data marshalling going on, and also the deployment process is not exactly seamless. Still, it was an interesting experiment. The post is here if you're interested.

www.planetmarshall.co.uk/.../silverlight-and-cuda-interop

Craig wrote re: Native Extensions for Silverlight (NESL)?
on Tue, Jan 4 2011 12:48 AM

Hi Mike,

I'm pretty excited about these features, I see a really great used case for it.

We're building a broad public 'cloud app' that's basically a desktop replacement app, but which we want to make available on all desktops, Mac as well, so WPF is out.

There are cases where we need access to local hardware like point of sale components. All those users are running windows as they're existing office customers.

So it's a case of sticking to your "use sparingly". The extensions are used for value add.

mtaulty wrote re: Native Extensions for Silverlight (NESL)?
on Tue, Jan 4 2011 9:00 AM

Craig,

Yes, yours sounds like the perfect example - i.e. you want the cross-platform but you also want deeper integration when running on Windows. As you say, WPF won't give you that so Silverlight is the right choice.

Mike

mtaulty wrote re: Native Extensions for Silverlight (NESL)?
on Tue, Jan 4 2011 9:02 AM

Andrew,

Yes, definitely take your point about data marshalling.

I suspect that you'd hit similar problems in trying to do "live" encoding of video from the webcam in a Silverlight app into (say) H.264 while crossing a COM interop boundary with the data.

Mike.

Alan Cobb wrote re: Native Extensions for Silverlight (NESL)?
on Tue, Jan 4 2011 8:06 PM

Hi Craig,

If I understand correctly, you said part of your requirements are to target local features on the Mac as well as Windows.  If so, then how does NESL help you?  That is, there is no equivalent of SL-to-COM integration or NESL on the Mac.

I'm excited about SL/COM/NESL, but I only care about Windows.

Alan Cobb

http://www.alancobb.com/blog

mtaulty wrote re: Native Extensions for Silverlight (NESL)?
on Wed, Jan 5 2011 11:58 AM

Hi Alan,

If I understood Craig correctly then he wants to have a cross-platform client (Mac and Windows) but he wants to be able to "light up" certain features if he's running on Windows and NESL helps him in that case.

Mike.

Alan Cobb wrote re: Native Extensions for Silverlight (NESL)?
on Wed, Jan 5 2011 7:30 PM

@Mike, @Craig,

Ok Mike that makes sense as I re-read Craig's post.  So the idea is you'll get some common functionality everywhere (like browsing the database of this week's sales), including the Mac, but you'll only be able to access the Point-of-Sale hardware from Windows machines.

This raises the issue of whether anything like COM integration will ever be added to SL on the Mac.  I wonder how much SL consumption is done from the Mac vs Windows.

Alan Cobb

Steve wrote re: Native Extensions for Silverlight (NESL)?
on Thu, Jan 6 2011 3:10 AM

I'm fairly confused about this.  If you want to write .net for Mac why not use Mono?

These apps don't feel or act like Mac apps anyway, it's like java swing all over again.  ie I tried Seismic on Mac and the scrolling didn't work right, the window behavior was off, etc... I was really turned off by the experience.

Silverlight is starting to stretch into an area of confusion - like WTF are we back to COM ?  Wasnt .net about getting rid of that kludge?  

I applaud you explaining it but the spent the whole time feeling, as a dev, that MS is opening up a can of worms.  Let's face it too, much of this is about office (where it burns me up that it isn't managed Apis yet), or lower level needs like media (which again burns me up that it's not managed)

Make WPF easier to install and put more effort into real Mac development instead of these hacks!

Alan Cobb wrote re: Native Extensions for Silverlight (NESL)?
on Fri, Jan 7 2011 6:15 PM

The greater integration with the local OS, that comes with SL-to-COM interop and NESL, fit well with the rumors about "Jupiter" and Windows 8.

See: http://zd.net/f1w5I5 where Mary-Jo Foley says: "...One of my contacts described Jupiter this way: 'It has to do with XAML + Native Code on slate/iPad-like devices. I think this is Microsoft’s approach for putting Windows on the smaller device without the bloat.'"

Alan Cobb

http://www.alancobb.com/blog

Alan Cobb wrote re: Native Extensions for Silverlight (NESL)?
on Sun, Jan 9 2011 7:57 PM

Mike,

>I can't make any comment

I don't expect you to :).  

Regardless of the rumor, one interesting idea is the concept of writing unmanaged applications (say in C++) combined with the goodness of the XAML-based UI system.

One of the big downsides of WPF, for some senarios, like in applets that have to ship with Windows itself, is the relatively big memory footprint and slow load times caused by having to pull all the .NET stuff into a process just to display even a simple window.  Something like Notepad in Win7 is implemented as a lightweight unmanaged Win32 C++ app (see its DLLs and symbols).

So what if you could write a similar light-weight, .NET-less, C++ Win32 app with a XAML UI?

Alan Cobb

www.alancobb.com/blog

mtaulty wrote re: Native Extensions for Silverlight (NESL)?
on Mon, Jan 10 2011 8:43 AM

Alan,

I've seen the article - it's based on rumour and speculation and so I can't make any comment whatsoever with regard to what Mary Jo's written.

Mike.

Canadian Developer Connection wrote Native Extensions for Microsoft Silverlight (NESL)
on Fri, Jan 21 2011 2:26 PM

If you’re building an application for the Windows platform, you’re probably aware of the new capabilities

Kirill Chilingarashvili wrote re: Native Extensions for Silverlight (NESL)?
on Mon, Feb 14 2011 8:02 AM

Also this may be awaited feature of SL from some developers,

I think this is completely wrong direction for Silverlight development.

I dont get the point - and I am thinking more and more that SL is a wrong technology to start development with today - because the exitement that was around this technology just few years ago just went away

The exitement was - everybody hoped this will be valuable competitor of Flash in terms of cross platform support.

But unfortunately - IT IS NOT.

And with this direction IT WILL NOT BE.

Sorry