Window Support

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

Wednesday, 7 September 2011

Silverlight 5 : Implement “Run” functionality using Platform Invoke (PInvoke)

Posted on 10:15 by Unknown

 

I hope you like my last article on PInvoke feature introduced in Silverlight 5 RC. As I mentioned in my last post that I will be posting more and more advance demos of PInvoke in coming days.Well, Today I am going to show you that how you can build “Run” kind of functionality using PInvoke in Silverlight 5 RC,Not getting this idea? Here is “Run” in our Windows look like :

WindowsR

Something similar we are going to develop using PInvoke in terms of look and feel and functionality.Idea is to make use of Shell32.dll’s ShellExecute( ).

I kept UI similar to Windows “Run” for better understanding,It is not exact replica or replacement for Windows “Run”, Idea is to demonstrate the functionality of ShellExecute( ) so I am not trying to break any copyright stuff of original Windows OS.

As discussed in my last article here,following the same steps lets build this component.

Namespace :

using System.Runtime.InteropServices;

This is the primary namespace used to develop PInvoke apps.

XAML :

<Image Source="Run.jpg" Height="42" Width="46" Canvas.Left="0" Canvas.Top="11" />
            <TextBlock Text="Type the name of Program,Folder,document,or Internet resource and Windows will open it for you." TextAlignment="Left" Height="44" Width="319" TextWrapping="Wrap" Canvas.Left="50" Canvas.Top="8" />
            <TextBlock Text="Open:" Height="20" Width="46" Canvas.Top="62" Canvas.Left="6" />
            <TextBox Height="23" HorizontalAlignment="Left" x:Name="txtValue" Width="311" Canvas.Left="50" Canvas.Top="62" />
            <Button x:Name="btnSubmit" Click="btnSubmit_Click" Content="Ok" Width="76" Height="22" Canvas.Left="50" Canvas.Top="100" d:LayoutOverrides="VerticalAlignment" />
            <Button x:Name="btnCancel" Click="btnCancel_Click"  Content="Cancel" Width="76" Height="22" Canvas.Left="150" Canvas.Top="100" d:LayoutOverrides="VerticalAlignment, Width" />

Note that like in Real “Run” box, I am skipping editable dropdown control and Browse button,Here I am taking Textbox to enter parameters and Ok and Cancel Button to handle the same.

UI will look like this after running :

RunOP

C# Code :

public MainPage()
{
      InitializeComponent();
      App.Current.MainWindow.Title = "Run";            
}    

#region Shell Execute

[DllImport("shell32.dll")]
static extern IntPtr ShellExecute(
            IntPtr hwnd,
            string lpOperation,
            string lpFile,
            string lpParameters,
            string lpDirectory,
            ShowCommands nShowCmd);

#endregion     

private void btnSubmit_Click(object sender, RoutedEventArgs e)
       {
           ShellExecute(System.IntPtr.Zero, "open", Convert.ToString(txtValue.Text), string.Empty, string.Empty, ShowCommands.SW_SHOWNOACTIVATE);
       }

       private void btnCancel_Click(object sender, RoutedEventArgs e)
       {
           if (App.Current.IsRunningOutOfBrowser)
               App.Current.MainWindow.Close();
       }

Note that you need to build ShowCommands enum, for more information you can visit ShellExecute Documentation on MSDN here

txtValue does the job of taking parameters and passing to ShellExecute.

That’s it ! We are done with our own “Run” box build using Silverlight 5 RC P/Invoke Feature using Shell32.dll ‘s ShellExecute( )

OOB Settings :

Kindly refer my last post where I have given all information about setting trust and what kind of possible exceptions you might get.So read more over there.

Output :

Here are some screenshots I took with our Silverlight 5 based “Run” :

Calling Calculator,Command Prompt and MS Paint from our “Run” box

WinRun

Visit my blog / any URL using our “Run” box ":

RunBlog

So if we compare Windows’s Run box and our in terms of looks..here we go !

ShellPInvoke

Where this can be applicable in Real life applications ? :

Idea in this demo is to use ShellExecute( ) in Silverlight application,UI can be different or in different formatting.Core idea is to use ShellExecute( ) and make use of that using P/Invoke, Do you want me to share what can be done with ShellExecute( ) ? I know I don’t need to..there are lot of business scenarios where this can be used,For example : Open instances of Word,Excel,Powerpoint,MS Paint,Calculator as I have shown above, Run Command Prompt and run scripts etc.

So I hope you will like this sample once you build and run this. Let me know your feedback and I will soon going to post one more P/Invoke sample in coming weeks along with other good and fun stuff on Silverlight 5 RC and then few interesting things on Windows Phone 7 as well.

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