Click here to Skip to main content
Click here to Skip to main content
Go to top

Invoking "Run" Dialog from In-Browser Silverlight 5 Application

, 12 Sep 2011
Rate this:
Please Sign up or sign in to vote.
How to open the "Run Dialog" directly from the Silverlight 5 application

Introduction

Silverlight 5 RC (Release Candidate) has already been released. There are some new features already included in this build. Among them, one most important feature is PInvoke. PInvoke means Platform Invoke. By using this, you can directly talk to system resources without using COM APIs.

In this article, we will explore it more and show you how to open the "Run Dialog" directly from the Silverlight 5 application. Here also, we will use In-Browser application which you can easily port to Out-Of-Browser.

Prerequisite

This article is based on Silverlight 5 RC. If you didn't install the latest RC bits, download them from "Download Silverlight 5 RC". Before downloading and installing the bits, make sure that you removed the previous Beta version of Silverlight 5 completely.

You also need to know about the changes in Elevated Trust Settings in Silverlight 5 RC. Read it from the blog post “Elevated Trust Settings for Out-of-Browser and In-Browser”.

Once you are done with the above prerequisite, let’s jump into the actual post where we will discuss about PInvoke with a small and simple demo.

Overview

Platform Invoke has been added newly to Silverlight 5 Release Candidate. It’s not a class or method. It’s just a way to communicate with system resources. If you are a .NET developer, you must be aware of the [DllImport(“”)] attribute to include system assemblies in your project and call those APIs.

Here, I will demonstrate it by importing the “shell32.dll” of the Windows System and calling the SHRunFileDialog() API call. This method takes different parameters as input and calls the actual Run Dialog. You can send proper title, description as parameter to it to change the dialog texts.

Playing with the Code

To start with this, we need an enum flag to set some flag parameters of the RunDialog. Inherit the enum from uint and set the Flags() as attribute to it. We need to write the proper enum values exposed by the API. Here is the complete code of that enum for your reference:

[Flags()]
public enum RunFileDialogFlags : uint
{
    /// <summary>
    /// Don't use any of the flags (only works alone)
    /// </summary>
    None = 0x0000,

    /// <summary>
    /// Removes the browse button
    /// </summary>
    NoBrowse = 0x0001,

    /// <summary>
    /// No default item selected
    /// </summary>
    NoDefault = 0x0002,

    /// <summary>
    /// Calculates the working directory from the file name
    /// </summary>
    CalcDirectory = 0x0004,

    /// <summary>
    /// Removes the edit box label
    /// </summary>
    NoLabel = 0x0008,

    /// <summary>
    /// Removes the separate memory space checkbox (Windows NT only)
    /// </summary>
    NoSeperateMemory = 0x0020
}

Now, we need to Import the DLL using the DllImport attribute. You will also need to declare the API in your code behind file. Here is the import and declaration of the API:

[DllImport("shell32.dll", CharSet = CharSet.Auto, 
	EntryPoint = "#61", SetLastError = true)]

static extern bool SHRunFileDialog(IntPtr hwndOwner, 
                                   IntPtr hIcon, 
                                   string lpszPath,
                                   string lpszDialogTitle, 
                                   string lpszDialogTextBody, 
                                   RunFileDialogFlags uflags);

Now it's time to use the API in our Silverlight application. To start with that, we will add a button in our XAML page called MainPage.xaml and register the click event to it. In the Click event of the button, we will give a call to the method that we declared just now with proper parameters.

Here is the click event implementation, have a look for reference:

private void ShowRunDialog(object sender, RoutedEventArgs e)
{
    SHRunFileDialog(IntPtr.Zero, 
                    IntPtr.Zero, 
                    "c:\\",
                    "Run Dialog using PInvoke from Silverlight 5",
                    "Type the name of a program, folder or internet address 
			and Windows will open that for you.",
                    RunFileDialogFlags.CalcDirectory);
}

This takes first parameter as the handle of the Window and the second parameter as the icon instance. We will pass IntPtr.Zero as the value to those two parameters to use the default values that Windows provides. Third parameter takes the default path; here we sent "c:\" as the default path. The fourth and fifth parameter takes string to represent the title and dialog text respectively. The last parameter takes RunFileDialogFlags that we created at the beginning. This enum is used to define whether the Browse button should be visible or whether you need to calculate the proper directory by the OS for the specified input. For more information on this, refer to the enum declaration.

Demo

It's time to give a demo of the same implementation. Run the Silverlight application in browser mode. In the UI, you will see a button as we added in the XAML page. Click the button and this will popup the Run Dialog of Windows into the screen. Here, you will notice that the title and the body text has been different than the original Run Dialog. This is because we passed customized text as parameter.

Silverlight 5 RC - PInvoke Demo - Calling Run Dialog from Browser

To check the functionality of the Run dialog, enter "calc" without quote in the input box called "Open" and hit OK as shown below:

Silverlight 5 RC - PInvoke Demo - Executing Command from Run Dialog

This will open the calculator application to the screen. You can do whatever you can do from the run dialog because it is just the same. So, what did we learn today? We learnt about PInvoke using Silverlight 5 RC, we also learnt the way to call the Run dialog directly from Silverlight application; whether it is an In-Browser or an Out-Of-Browser application.

End Note

Last but not least, make sure that you marked the application as a trusted one before building the project, else you will get some security exception. By default, the “localhost” domain is trusted for development purpose. You need to change a registry value for development. To put it to the production environment, you need to sign the XAP for trusting.

Do you still think ”Silverlight is Dead”? No my friend! The web still needs Silverlight and there seems to be a very bright future of it. Don’t be panic on rumours.

Hope this post was helpful. There are more on this topic that are coming on. Tomorrow, we will discuss how we can access the System resource and create a directory in 'C' drive from Silverlight web browser application. So, stay tuned to my blog to read about them.

History

  • 12th September, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

_ Kunal Chowdhury _
Technical Lead
India India
Kunal Chowdhury is a Microsoft "Client Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows 8 (WinRT). He posts his findings, articles in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionWhat about Mac? PinmemberMoshe Taieb1-Nov-12 0:47 
GeneralMy vote of 5 PinmemberMassimo Conti29-Jun-12 13:01 
QuestionI wont vote, but: PinmemberHaBiX13-Sep-11 21:28 
AnswerRe: I wont vote, but: PinmemberMassimo Conti29-Jun-12 21:35 
GeneralMy vote of 5 PinmvpAbhinav S12-Sep-11 18:50 
GeneralRe: My vote of 5 PinmemberMassimo Conti29-Jun-12 21:31 
GeneralMy vote of 5 Pinmembermbcrump12-Sep-11 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 12 Sep 2011
Article Copyright 2011 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid