Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 3 September 2011

Silverlight 5 : Platform Invoke (PInvoke) in Silverlight

Posted on 02:06 by Unknown

 

Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here, My friend Pete Brown already given very good information on Silverlight 5 RC here.

PInvoke (some type this as p/Invoke) that is Platform Invoke is most awaited feature in Silverlight since the announcement of Roadmap of Silverlight 5.

There are lot of talks in the community about Native HTML5 Apps using JavaScript,I am sure with this feature of PInvoke,Silverlight is now equally powerful player in App Development.

Why I kept above text in Bold? This is because there is lot of HTML5 talks around and sometime people unnecessarily without judging pulling Silverlight down for no good reason.For those who think we are Dead or we are just Animation technology..well by this feature you folks must have realize the potential of this Silverlight platform and where we are heading towards.

Coming back to PInvoke,So today to start with,I am putting forward a very simple and basic demo of PInvoke for you to understand and grab the topic and get hold of it.Then in next article I will be putting some advance samples and also in coming days I will be covering few more Silverlight 5 topics as well.

This demo is based on calling a “Beep” functionality from kernel32.dll file from Windows

If your fundas of PInvoke are not clear,In that case I encourage you to have a look at this article on Wikipedia which talks about PInvoke

http://en.wikipedia.org/wiki/PInvoke

Our First Silverlight PInvoke Demo :  Beep !!..Beep !!…

Namespace :

using System.Runtime.InteropServices;

This is the primary namespace used to develop PInvoke apps.

XAML :

<Grid x:Name="LayoutRoot" Background="Black">
        <Image Source="Beep.jpg"/></Grid>

I just taken a Road Runner Image as name of the demo is Beep Beep

C# Code :

Now what we are going to do is that we will invoke Beep function which is a System Beep function from kernel32.dll

#region Beep Module
       [DllImport("kernel32.dll", SetLastError = true)]
       [return: MarshalAs(UnmanagedType.Bool)]
       static extern bool Beep(uint dwFreq, uint dwDuration);
#endregion

[DllImport…] specified which dll file we are invoking.”SetLastError” is actually a Named Parameter in the signature which indicate whether the callee calls the SetLastError Win32 API function before returning from attribute method.

So we are all set to call Beep function from kernel32.dll in our Silverlight app like shown below :

public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
           
            for (uint i = 100; i <= 200; i=i+10)
            {
                Beep(i,100);
            }
        }

Done ! We have just develop our first PInvoke Sample using Silverlight 5 RC !

Now turn on your speakers and get ready to hear Beep !! Beep !! from Road Runner ..I mean you will get some sound / default sound from system.If you want to change bits and duration then go ahead and feel free to tune the code.

Oh ! Wait ! What’s this error about? :

pinvokeerror

This is Runtime Exception because of lacking of trust,so you need to turn on that in OOB setting like this :

pinvoketrust

To know more on this Trust part I encourage you to read my friend Kunal’s article here.

Output : (Turn On the Speakers for Beep)

pinvkop

This ends our first ever PInvoke functionality using Silverlight 5 RC.I request you to download new RC bits and try out this demo,meanwhile I will prepare my next advance demo of PInvoke and other few articles.So with this PInvoke I can say again that ..Future is Bright ! Future is Silverlight !!

Vikram.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ▼  2011 (24)
    • ►  December (1)
    • ▼  September (4)
      • Building your First Windows Metro style Applicatio...
      • Silverlight is dead…and our thought process too !
      • Silverlight 5 : Implement “Run” functionality usin...
      • Silverlight 5 : Platform Invoke (PInvoke) in Silve...
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ►  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ►  January (6)
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile