New Line of Business Features in Silverlight 5 – Using pInvoke
This is the fourth post in a series covering new line of business features in Silverlight 5.
- Implicit Data Templates
- Ancestor RelativeSource Binding
- Debugging Bindings in XAML
- Elevated Trust Enhancements
- Using pInvoke
- Running with Elevated Trust In-Browser
- Creating Multiple Windows in Out-of-Browser Applications
- Vector Printing
- Custom Markup Extensions
In this post I’ll provide an overview of how pInvoke can be used in Silverlight 5 applications.
Using pInvoke in Silverlight 5
Silverlight 4 introduced the concept of elevated trust which allowed a Silverlight application running out-of-browser to perform tasks such as accessing the hard-drive and integrating with COM components installed on the client machine. Silverlight 5 adds several new enhancements to elevated trust applications including the ability to perform pInvoke calls (low level calls to the operating system API), run elevated trust applications in the browser, and run the WebBrowser control while in a browser. Out-of-Browser applications have also been enhanced to support multiple Ad-hoc windows. This new functionality allows in-browser and out-of-browser applications to perform tasks typically reserved for traditional desktop applications such as accessing USB devices and spawning windows that can run on multiple monitors.
The process of enabling elevated trust is the same as it was previously in Silverlight 4. Right-click on the Silverlight project and check the box titled Enable running application out of the browser. Once the checkbox is checked you can click the Out-of-Browser Settings button and then check the Require elevated trust when running outside the browser box located at the bottom of the dialog:
Once elevated trust is enabled, new Silverlight 5 functionality such as pInvoke can be used in an application. pInvoke calls allow a Silverlight application to call into unmanaged APIs exposed by the Windows operating system. By using pInvoke you can gain low-level access to functionality that was off limits before. The following code shows an example of using pInvoke to access the logged on user's user name directly from a Silverlight application.
public partial class PInvoke : UserControl { public PInvoke() { InitializeComponent(); } [DllImport("Advapi32.dll")] private static extern bool GetUserName(StringBuilder lpBuffer, ref int nSize); private void button1_Click(object sender, RoutedEventArgs e) { int size = 64; StringBuilder sb = new StringBuilder(size); GetUserName(sb, ref size); UserNameTextBlock.Text = sb.ToString(); } }
The pInvoke class shown above uses the DllImport attribute (located in the System.Runtime.InteropServices namespace) to reference Advapi32.dll which is part of the Windows operating system. Advapi32.dll contains a method named GetUserName that can retrieve the logged on user's user name directly from the operating system without having to pass the user name into the Silverlight application using the object tag's initParams parameter or call an ASMX or WCF service to get it. The GetUserName method is marked with the extern keyword since it is implemented outside of the Silverlight framework.
Once the GetUserName method is marked with the DllImport attribute it can be called directly from a Silverlight application. The previous code sample creates a StringBuilder object with a size of 64 and passes it into the GetUserName method. Once the method is called the user name can be retrieved directly from the StringBuilder. More sophisticated pInvoke techniques can be used to add advanced functionality to LOB applications that wasn't previously possible with earlier versions of Silverlight. Visit http://www.pinvoke.net for a list of pInvoke APIs that can be called.