Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Silverlight 4 – Writing a Windows-Only Application via COM Interop?

Blogs

Mike Taulty's Blog

Elsewhere

Archives

I was reading Tim’s post over here on “Silverlight 4 with COM can do anything – on Windows” which referenced Justin’s amazingly detailed post with a bunch of COM interop examples from Silverlight 4.

A Silverlight 4 beta application that has been explicitly installed as trusted by the user can create COM objects if they have a registered ProgId and if they implement IDispatch.

As anyone who’s been around Windows for a while and especially anyone who wrote scripting code like ASP pages will know, that opens up quite a lot of code to those applications. I think that was made pretty clear at the PDC when the feature was first announced.

My personal view is that the COM interop feature is something that you'd use sparingly to add additional functionality that becomes available when and where COM interop is turned on rather than writing a tonne of code that takes the COM interop route.

Why’s that?

Firstly, you might find your application goes into an environment where the users aren't allowed to run trusted Silverlight applications at all. It's driven by group policy and the admin may well make a decision that the policy will not allow the installation of your trusted application.

Secondly, going down the COM interop route makes your application Windows only. I disagree with Tim’s previous post in that I think that there are many good reasons to build a Windows-only application today and I agree with what Marc wrote here on that and have some other views of my own that I’ll add in some other post.

However, Silverlight is a cross-platform framework so if you need a lot of Windows-only functionality then you might look to a Windows-only framework like WPF which will get you that Windows-only functionality without forcing you to leave .NET and drop into...

Thirdly, you leave your .NET world of metadata-driven strong typing and IntelliSense and drop into the [best/worst - depends on your viewpoint] part of the COM world - IDispatch programming which was never (imo) a pleasant place to be. Debugging across that boundary isn’t really that pleasant either.

Fourthly, there's no way of deploying or versioning the COM code that you're taking a dependency upon and no pre-installation step where you can check those COM pre-requisites. That might be as something like trying to know whether the right COM components from Microsoft Office are installed or maybe making sure that some installed components are 32-bit rather than 64-bit and so on.

So, I think Justin's post does a very good job of illustrating some of what "interop via IDispatch" can mean in terms of being able to call lots of existing Windows code.

However, that doesn't necessarily mean that building an application where you write reams-and-reams of COM code in Silverlight is a good idea. Not that I’m suggesting that Justin was saying that in the first place by the way.

In my opinion, for those kinds of scenarios you'd save yourself a lot of blood,sweat and tears by writing a .NET 4.0 application with WPF and the whole of the .NET Framework at your disposal.

Now, it might be that you’d say “Ah, but I’d then have to deploy the full .NET Framework”. Maybe. If you’re targeting Windows 7 or Windows Vista (3+ years old now) or then the .NET Framework with WPF is already installed and ScottH recently posted figures pointing to;

  • Well over 90% of the PCs in the world have some version of the .NET Framework installed.
  • Over 65% of Windows PCs in the world have .NET 3.5 SP1 installed.

so, that’s a pretty healthy 65% on the latest version of WPF and might alleviate your worry about getting the .NET Framework out there. There’s also smarter deployment options for the client part of the Framework with the .NET Client Profile ( especially in .NET 4.0 ).

So, my view – I like Silverlight. It’s fantastic for building rich internet applications. I like WPF too. It’s fantastic for building rich Windows applications. Choose the one that best suits your purpose.


Posted Fri, Jan 22 2010 1:34 AM by mtaulty
Filed under: ,

Comments

Tim Anderson wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Fri, Jan 22 2010 4:21 AM

Mike

Thanks for the comments. It wan't my intent to say whether or not using these features is a good idea or a bad idea. It is an interesting question though. Programming tools often get used in unexpected ways. If you offer the capability to do something, chances are that some will take advantage of it, bad idea or not. And applications evolve - say you've done a Silverlight app, and then find that some feature request can be easily fulfilled with a dash of COM interop? Would you rewrite in WPF, just because that would be a "bad idea"?

I think the addition of COM to Silverlight 4 is a huge change to the platform, for better or worse. And I think Microsoft should keep parity with the Mac version; it was meant to be WPF *everywhere*. Perhaps few will use it though; we'll see.

Tim

mtaulty wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Fri, Jan 22 2010 5:17 AM

Hi Tim,

Yes, understand that your intention wasn't around good/bad.

Would I rewrite in WPF to get a "dash of COM interop"?

No, I was trying to convey here that I think a "dash of interop" is fine. What I'd personally avoid is an application that uses huge amounts of it because at that point there are probably better ways of achieving the functionality.

In terms of "rewrite", it's worth saying that in the Silverlight/WPF case it's possible to re-use code across the 2 frameworks and so target both worlds from the outset and/or move code between the frameworks at a later point. I don't want to trivialise that but it's certainly a possibility.

Mike.

Blog de Oskar Alvarez wrote Enlaces Interesantes WPF/Silverlight 22-01-2010
on Fri, Jan 22 2010 7:00 AM

Hola vengo con los enlaces interesantes de hoy pero quisiera resaltar uno y establecer un pequeño

Denny wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Fri, Jan 22 2010 8:18 AM

One thing I did not see was that it looks like .Net 4 client profile install is going to be about 40 megs!

thats way smaller than a 3.5 Sp1 install at over 150 megs!

so .Net 4 client is going to be much less hassle to get on customers boxes when it hits RTM / TRW in about 2 months.

WillSmith wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Fri, Jan 22 2010 8:55 AM

I think the difficultly of getting a .NET version > 2.0 deployed cannot be over stated.  Silverlight is a much better soln and has less deployment friction.  There are some serious deployment issues with Silverlight however.  For an enterprise rollout on shared workstations (more than one user uses the same computer), having them accept the trust elevation and accept to have it run on the desktop doesn't work.  There needs to be a group policy way to override these UI elements.  How do I deploy to 3000 workstations in a 1 month rollout and have the user just run the Silverlight app?  Local storage can't go to the "all users"/public documents folder.  Why NOT?   Add more flexibility to the Silverlight sandbox via group policy.  Allow me indicate what directories on the machine Silverlight can have full control over.  Also, please if I can get the web cam, why on earth can't I get to ANY serial or USB port that I need to from Silverlight?  Let me open up any tcp/ip port (socket) I want to by policy as well.  How about including a basic ADO .net stack, serial/usb port access, and more file i/o ALL controlled of course by policy (in this case Group Policy), that would avoid much of the need and security risks of COM interop.

Just expand these settings:

www.microsoft.com/.../grouppolicysettings.aspx

Then we would really be able to build the easy to deploy hybrid (client/webservice) enterprise applications.

Oleg wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Fri, Jan 22 2010 9:58 AM

Hi Mike,

Rationally I agree with you. As wierd as I felt seeing ASP.NET desktop applications (you, corporate intranets), that was at least justifyable because skills of writing ASP and WinForms are essentially different. So, if you have ASP developers only - why not.

But Silverlight instead of WPF for Windows desktop app (especially Win7 desktop app)  - what is really the point?

But I still see that this going to be favorable trend in coming years. And I cannot quite understand why.

May be it's just because developing in SL gives you hope you can easily adjust your SL Win app to web, or Mac? This never going to happen if you have bunch of refs to COM, but still it's a warm feeling.

mtaulty wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Fri, Jan 22 2010 1:23 PM

Will,

Appreciate your point around your difficulty in deploying a version of .NET beyond 2.0 although Windows 7 has 3.5 Sp1 and Windows Vista has 3.0 and, ScottH's figures referenced suggest that 65% of machines are already there.

The sorts of features you're looking for - open up a socket anywhere, talk to the hardware, talk via ADO.NET ( which means opening up a socket again ) are features already present in the .NET Framework on the client in a much more consumable form.

Mike.

Mike Stokes - HelpBurner wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Fri, Jan 22 2010 1:56 PM

COM Interop in Silverlight 4 should be used very sparingly and only if there is no other user-effective way of achieving the same outcome.  For example, we were forced into using COM interop to add screen-capture capabilites to our application.

The main reason to not use it is because it destroys the cross-platform run-anywhere nature of Silverlight.  Being able to provide 1-click trialing of your software to potential visitors on your website (using Silverlight) is such a huge benefit for software developers that it amounts to a significantly higher number of sales (compared to download-and-install-trialware).  From a startup's perspective this is a huge plus with Silverlight and AIR.

Luckily with Silverlight we can partition the areas off that need to use COM and inform the user of this as they need these capabilities, allowing for gradual enhancement whilst still providing the browser-based (even though it will be running as out-of-browser) software experience to users.

It's going to be very interesting to see what sort of native capabilities Silverlight provides on the Mac and Linux.

Great conversation piece Mike and it's just another great feature in our Silverlight bag of tricks.

Mike

Fallon Massey wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Fri, Jan 22 2010 11:32 PM

I don't get it.  With SL4, you can have a button that takes you to OOB for EXTRA features, otherwise, you stay in Browser... OR,

You go OOB, and have advanced functionality loaded via Prism or MEF.

Why is is one or the other, with SL4, you can have both!

BTW, if you're worried about COM interop and having the full .Net framework, just develop in C++ for your COM object, and you're guaranteed to run.

j23tom wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Fri, Jan 22 2010 11:33 PM

So it looks like the Silverlight, will become another windows only technology.

mtaulty wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Sat, Jan 23 2010 3:04 AM

j23tom - no, I disagree. Silverlight's a cross platform technology. It already runs on Windows, OS X and Linux.

It's not a Windows only technology and there's very little in Silverlight's functionality today that exists on Windows and not on OS X. That's as true in V4 as it was in V2.

Mike

mtaulty wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Sat, Jan 23 2010 3:06 AM

Fallon - not sure I understand your C++ comment. There is no way (AFAIK) to deploy new COM code with Silverlight so if you write your own custom COM object then you'll have to come up with your own way of deploying it which ( to me ) indicates you're firmly in the Windows-only camp and might be better looking at WPF.

Mike.

Tjhwa Kok Liong wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Sat, Jan 23 2010 10:12 AM

Choose a tool based on your target platform. Creating Windows desktop app using ASP or Silverlight is similar as designing PC with 64K memory and assuming it will never be a time that people will need more than 64K (remember that time?). Using ASP just because there is no one in the office has knowledge to write Win App is just too funny.

Fallon Massey wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Sun, Jan 24 2010 2:22 AM

Mike there are a couple of articles on building a COM object, downloading it from a website, and registering it, ALL in a SL4 OOB app, it's not that hard.

The problem they encounter is that they develop the COM object with C#, and therefore have to worry if the full framework is there, and that it's the proper version.

That's why I said to write the COM object in C++, and it's GUARANTEED to run on any windows platform, no dependencies to worry about.

You blew past my comment that you don't have to choose, you can have both cross platform capability AND Windows only capability, and use the ones that are appropriate.

The point is that I don't need to limit myself to WPF, with SL4, I can do it all, and deliver it all over the web through the browser.

There are places for WPF, I just don't see any reason for me to even consider WPF for 99.999% of what most customers want or need, and WPF is nowhere as flexible as SL4... period.

mtaulty wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Sun, Jan 24 2010 10:58 AM

Fallon,

Ah, I see what you're saying. You're wanting to use a Silverlight 4 application running elevated outside of the browser in order to download and register a COM object.

So, Silverlight itself becomes the installer for a COM component.

I still think the balance is there though.

If you are going to do a _lot_ of that kind of work then you're Windows only and I'd write WPF. If you're going to do a little of as a supplement to cross-platform functionality then that makes sense in Silverlight.

Mike.

Fallon Massey wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Sun, Jan 24 2010 2:08 PM

Great Mike, and I totally agree with your last sentence.

The fact is that SL4 provides so much flexibility to solve these problems that WPF becomes a specialized solution, here's my thinking.

Since SL4 can be the ultimate delivery product, if you need WPF functionality, THEN download the WPF app from the OOB experience!

I love SL4 because I have the option to stay in the browser, go to OOB, all the while maintaining 100% cross platform, and then you have the "Nuclear Option" of going Windows only, ALL in one app.

SL4 doesn't eliminate the need for WPF, it just places WPF in a smaller niche.

JC wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Mon, Jan 25 2010 6:33 AM

One of the things which has been noticeable by its absence is any form of blogging by the people who are developing Silverlight for OSX; who are the people involved?  Presumably there's a big team of developers, but do we know who they are?  Nope.  Let's see/hear who they are :-)

silverlightchina wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Mon, Jan 25 2010 9:09 AM

great topic!

mtaulty wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Mon, Jan 25 2010 2:31 PM

JC,

I'm not sure I could name the guys who write Silverlight for OSX.

Then again, I could not really name many of the people writing other feature areas like the Silverlight media stack, the networking stack, the display stack, etc. etc.

I don't think that's particularly unusual - I guess they're just busy people who are focused primarily on writing/testing their code rather than writing public blogs.

They seem to be doing a pretty good job of it ( imho ) :-)

Mike

ゆむかブログ wrote Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Mon, Jan 25 2010 9:17 PM

Silverlight 4 – Writing a Windows-Only Application via COM Interop? - Mike Taulty's Blog - Mike Taulty's

Andrew Marshall wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Tue, Jan 26 2010 3:52 AM

I agree that it's definitely to be used sparingly, but it's better to have the feature and the option to use it than not. It can be frustrating as a developer to always have to program for the lowest common denominator, and COM interop at least gives some option for those of us who want to provide some accelerated functionality beyond what Silverlight alone can offer.

As an example ( apologies for the shameless plug ), see my post on using CUDA with Silverlight via COM.

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

joongs14's me2DAY wrote 중스의 생각
on Sun, Jan 31 2010 6:01 PM

Silverlight 4에서 COM 지원하는 것과 관련한 많은 이야기들 중에 하나 윈도우에서만 돌아가는 Silverlight 애플리케이션을 만들것인지?

Lucy wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Sat, Feb 13 2010 8:14 AM

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Lucy

http://dataentryjob-s.com

Tom wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Thu, Feb 18 2010 3:09 PM

With file system access and now COM Interop, Silverlight 4 is sounding more like ActiveX. I think it is a terrible idea to put WIndows only features in a runtime that is supposed to be cross-platform. Sorry MS, you are really losing me here.

mtaulty wrote re: Silverlight 4 – Writing a Windows-Only Application via COM Interop?
on Thu, Feb 18 2010 4:59 PM

Tom,

Silverlight doesn't have file system access.

Cross-Platform a non-trusted application can only read/write a file that you hand it via a file dialog just like you do when you upload/save a file from an HTML app.

Cross-Platform a trusted application can only read/write files to 4 specific locations - the cross-platform equivalent of my documents, my music, my videos, my photos.

Then there's COM interop which does open a door to effective full-trust on Windows.

COM interop is a Windows specific feature as COM in a Windows specific feature. My view of its intention is to allow people to call out to _existing_ COM code where they have it or know it to be present on the desktop.

Today, if you browse the web you can download and run executable files on Windows or Mac that contain malware. You get a big warning before doing it but you can do it and it happens every day.

With a trusted Silverlight application it's possible to download and run a Windows or Mac application that contains malware. You get a big warning before doing it but you can do it.

Mike.