Michael Crump is a Microsoft MVP, INETA Community Champion, and an author of several .NET Framework eBooks. He speaks at a variety of conferences and has written dozens of articles on .NET development. He currently works at Telerik with a focus on our XAML control suite. You can visit his blog at: MichaelCrump.Net or follow him on Twitter at: @mbcrump Michael is a DZone MVB and is not an employee of DZone and has posted 112 posts at DZone. You can read more from them at their website. View Full User Profile

Yet another Beeping P/Invoke Demo (SL5 RC)

12.17.2011
| 3200 views |
  • submit to reddit

Introduction

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.

Tools needed:

Getting Started

Go ahead and open Visual Studio 2010 SP1 and select File->New Project then Silverlight Application.

SNAGHTML425f655

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.

SNAGHTML427e8ce

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

image

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

image

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

SNAGHTML2b29bb45

Switch back over to the MainPage.xaml and add in the following code:

<Grid x:Name="LayoutRoot" Background="White">
    <Button Height="23" HorizontalAlignment="Left" Margin="169,132,0,0" VerticalAlignment="Top" Width="75" x:Name="btnclick" Content="click" Click="click_Click" />
</Grid>

This will simply put a no thrills button on the page that the user can press to call the P/Invoke code we will add shortly.

Let’s go ahead and add a new class to the project.

image

Let’s call it PlatformInvokeTest.cs and add the following code (Note: If your having a problem getting it to work then use my solution at the bottom of the post):

using System;
using System.Runtime.InteropServices;

namespace SilverlightApplication26
{
    public class PlatformInvokeTest
    {
        [DllImport("kernel32.dll")]
        public static extern bool Beep(int frequency, int duration);

    
        public static void PlaySound()
        {
            Random random = new Random();
            for (int i = 0; i < 50; i++)
            {
                Beep(random.Next(10000), 100);
            }
        }
    }
}

Let’s switch back over to the MainPage.xaml.cs and add the following code:

using System.Windows;
using System.Windows.Controls;

namespace SilverlightApplication26
{
 
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void click_Click(object sender, RoutedEventArgs e)
        {
            PlatformInvokeTest.PlaySound();
        }
    }
}

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”.

image

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.

image

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!

If you want the source code to this application and other Silverlight 5 demos then be sure to check out Michael’s “Mega Collection of #Silverlight 5" Demos.

References
Published at DZone with permission of Michael Crump, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)