You might have noticed that the Silverlight 5 (Release Candidate) is out. One of the new features included in the RC is the ability to call P/Invoke. In this short demo, we will write a Silverlight 5 application that uses the feature.

By default, we have a new option called “Silverlight 5” selected as the Silverlight Version. Let’s go ahead and leave it at that. You also have the ability to select Silverlight 3 or 4 from this drop-down.

Let’s go ahead and Right click on our project and select Properties.

Put a check in “Enable running application out of the browser”.

Now go ahead and put a check in “Require elevated trust when running outside the browser”.

Now when the user fires up this project the application will go out of browser and the computer will beep multiple times in a different frequency each time.

You can also get this same functionality in-browser by going back to the Properties page and selecting “Require elevated trust when running in-browser”.

The only thing to note is that the .aspx page is no longer set to the default in your web project so you will need to do a “View in Browser” on your .aspx page in order to test.

Conclusion

As you can see it is very easy to use P/Invoke in a Silverlight 5 application. This sample was pretty simple but image the possibilities such as detecting when a USB key is inserted into a PC and copying files onto it through a Silverlight 5 application? Pretty cool stuff!