Native Extensions for Silverlight (NESL)?

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;

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;

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;

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

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;

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;

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;

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 );

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

view plaincopy to clipboardprint?
  1. if (!Microsoft.Silverlight.Windows.Installer.CheckNESLInstalled(1, 0))
  2. {
  3. Microsoft.Silverlight.Windows.Installer.InstallNESL(
  4. new Uri("NESLSetup.msi", UriKind.Relative),
  5. false, // is the installation remote?
  6. true); // do I want to show progress? You BET!
  7. MessageBox.Show("NESL Installed!");
  8. }
      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;

and then;

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

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

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;

and a reference to the NESL library for speech;

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

view plaincopy to clipboardprint?
  1. SpeechSynthesizer synth = new SpeechSynthesizer();
  2. foreach (var item in synth.GetVoices())
  3. {
  4. string speech = string.Format("I have a voice called {0} which is {1} years old and is {2}",
  5. item.Name,
  6. item.Age.ToString(),
  7. item.Gender.ToString());
  8. synth.Speak(speech);
  9. }
      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;

view plaincopy to clipboardprint?
  1. SpeechSynthesizer synth = new SpeechSynthesizer();
  2. synth.SpeakAsync(txtSpeech.Text);
  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;

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

view plaincopy to clipboardprint?
  1. void ChangeTaskBarIconOverlay()
  2. {
  3. byte[] iconBits = this.GetImageBitsForCurrentPlayState();
  4. TaskbarButton.Current.SetOverlayIcon(iconBits, "Play State",
  5. ButtonImageDataType.PNG);
  6. }
    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 );

view plaincopy to clipboardprint?
  1. void OnTick(object sender, EventArgs e)
  2. {
  3. if (this.playState == PlayState.Idle)
  4. {
  5. // NB: This line of code fails...maybe a bug?
  6. // TaskbarButton.Current.SetProgressState(TaskbarItemProgressState.None);
  7. // Doing the dynamic dispatch myself...
  8. TaskbarButton.Current.COMObject.SetProgressState(
  9. (int)TaskbarItemProgressState.None);
  10. }
  11. else
  12. {
  13. TaskbarButton.Current.COMObject.SetProgressState(
  14. (int)TaskbarItemProgressState.Normal);
  15. TaskbarButton.Current.SetProgressValue(
  16. (ulong)this.mediaElement.Position.TotalSeconds,
  17. (ulong)this.mediaElement.NaturalDuration.TimeSpan.TotalSeconds);
  18. }
  19. }
 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;

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

view plaincopy to clipboardprint?
  1. void CreateTaskbarButtons()
  2. {
  3. ThumbbarButton button = TaskbarButton.Current.CreateThumbbarButton(1);
  4. button.Tooltip = "Stop";
  5. button.ImageDataType = ButtonImageDataType.PNG;
  6. button.Image = GetImageBitsForResource(
  7. "SilverlightApplication11;component/StopHS.png");
  8. TaskbarButton.Current.ShowThumbbarButtons();
  9. }
   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;

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

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.

转载于:https://www.cnblogs.com/allanli/archive/2011/01/10/1931651.html

(转)Native Extensions for Silverlight (NESL)?相关推荐

  1. Flashdevelop解决ANE报Not supported native extensions profile

    http://hi.baidu.com/silvanote/item/ac6495fac7123225743c4ccc 报 Not supported native extensions profil ...

  2. Developing native extensions for Adobe AIR

    原文:http://www.adobe.com/devnet/air/articles/developing-native-extensions-air.html 以下为摘译 Note: Adobe ...

  3. MAC中安装 sudo gem install cocoapods 报错 Building native extensions. This could take a while...

    原文地址声明:https://blog.csdn.net/qq_23179075/article/details/104819430 在MAC中安装 sudo gem install cocoapod ...

  4. 开发Flex for Android第一个ANE(ActionScript Native Extensions)本地扩展

    本地扩展就是需要调用原生的东西要开发的插件 首先打开Android Studio,建个空项目, 再建立个名为 FirstANE 的Android Library Module, 然后把C:\Progr ...

  5. 分享Silverlight/WPF/Windows Phone一周学习导读(1月17日-1月23日)

    上周微软Silverlight团队发布"微软发布Silverlight Native Extensions 1.0 - 扩展OOB应用功能",对于Silverlight开发人员而言 ...

  6. macOS安装 cocoapods1.9.1失败Failed to build gem native extension

    最近团队内升级cocoapods到1.9.0.原本我简单的以为执行sudo gem install cocoapods 来更新.万万没想到. 执行多次都是:ERROR: Error installin ...

  7. cocoapods 安装失败 ERROR: Error installing cocoapods: ERROR: Failed to build gem native extension.

    cocoapods 安装失败 完整报错 尝试安装 我的问题 使用 Alamofire 需要基于 CocoaPods,但是在尝试安装 CocoaPods 的过程中遇到了很多问题,捣鼓了很久都失败了,最后 ...

  8. React Native从入门到实战--开发环境搭建

    前言: 从今天起开启一门全新的课程学习,就是React Native,远在18年https://www.cnblogs.com/webor2006/p/9350428.html其实当时就已经做过开篇, ...

  9. 解决报错“Ignoring ffi-1.12.2 because its extensions are not built. Try: gem pristine ffi --version 1.12”

    执行pod install时报错 Ignoring ffi-1.12.2 because its extensions are not built. Try: gem pristine ffi --v ...

最新文章

  1. 视频|深度相机与应用
  2. productFlavors 差异打包问题
  3. 解决依赖的moduleBuildConfig.DEBUG总是未false的问题
  4. maven学习(下)利用Profile构建不同环境的部署包
  5. 【小项目关键技术六】控制北斗 GPS 定位 / UWB 室内定位
  6. Project Tungsten:让Spark将硬件性能压榨到极限
  7. 开源 Serverless 里程碑:Knative 1.0 来了
  8. 需求决定设计,设计来源于需求
  9. VB.NET通讯录源代码
  10. Linux ulimit命令(控制shell执行程序的资源、并发)
  11. 关于在事件代码中如何访问类中的变量
  12. java向注册表单传递数据php_form表单传递数组数据、php脚本接收的实例
  13. 计算机编程的双引号怎么打,计算机双引号怎么打出来
  14. 我在Python的艳阳里,大雪纷飞
  15. 不能学习vlan的解决
  16. 关于微信小程序danmu-List所遇到的坑
  17. ANDROID内存优化(大汇总——全)
  18. VPP DPDK,不是翻墙!!
  19. 360全景拍摄有哪些技巧?
  20. 报错:“export ‘default‘ (imported as ‘mui)‘ was not fount in ‘../lib/mui/js/mui-min.js‘

热门文章

  1. 【c/c++编程】查找类问题
  2. 安全云是如何做好云安全的?
  3. 计算机科学导论在线作业,南开21春学期《计算机科学导论》在线作业
  4. Python 学习入门(26)—— 装饰器
  5. C++调用opencv完成运动目标捕捉
  6. Linux 文件与目录的管理
  7. 使用三丰云免费云主机安装cyberpanel面板并创建wordpress站点
  8. 【数据库课程】研讨02
  9. 教你竖屏视频上下黑边添加图片的剪辑技巧
  10. ArcGIS 同一要素图层合并