Windows 7 Managed Code APIs

We have enabled an easy direct access to Windows 7 (and some Windows Vista) features for .NET developers. All you need to do is download Windows API Code Pack today!

As mentioned in the Windows 7 .NET Interop Sample Libraries post, most Windows 7 APIs are written in native code C, C++, and COM APIs, and have no .Net equivalents. This makes the life of managed code developers a bit hard, forcing them to create their own interoperability layers for communicating between .NET and the native Windows APIs. Some native APIs are easy to wrap and use from managed code, requiring simple interoperability efforts like adding a reference to a COM type library. However, some are extremely problematic and require a lot of knowledge about the “Win32” programming style. That is exactly why the Windows API Code Pack for Microsoft .NET Framework was created.

In the Windows 7 .NET Interop Sample Libraries post, we highlighted few samples codes showing how to access the Taskbar, Libraries, Sensor, and multi-touch from .NET, and we also promised to report in the near future on a new project, Windows API Code Pack for Microsoft .NET Framework. The Windows SDK team built the Windows API Code Pack to provide easy access to Windows 7 features for .NET developers , lowering the bar for developers who want to target Windows 7 Client applications. Basically, it is the closest thing a .NET developer will have to managed code APIs for Windows 7.

The Windows API Code Pack (Code Pack) is much larger in terms of offered functionality and quality than its older brother, the Windows Vista Bridge. The Code Pack contains a wide variety of APIs, including very comprehensive Windows Shell namespace objects, Windows Taskbar, Libraries, and Windows 7 Extended Linguistic Services. The following is a short list of the APIs contained in the Windows API Code Pack:

  • Support for Windows Shell namespace objects, including:
    • Windows 7 libraries
    • Known Folders
    • Non-file system containers
  • Windows Vista and Windows 7 Task Dialogs
  • Support for Windows 7 Explorer Browser Control
  • Support for Shell property system
  • Windows 7 Taskbar
  • Support for Windows Vista and Windows 7 common file dialogs, including custom file dialog controls
  • Support for Direct3D 11.0 and DXGI 1.0/1.1 APIs
  • Sensor Platform APIs
  • Extended Linguistic Services APIs

When you download and unzip the v0.85 file, you will find a Samples folder. This folder contains number of samples demonstrating the entire range of APIs provided by the API Code Pack. You will also find the WindowsAPICodePack folder that contains the full source code for this library. This provides a great interoperability learning opportunity, while giving you the full flexibility of changing source code.

There are many samples, 13 for now with more coming in future releases, but there are four in particular that I want to highlight.

WPF Explorer Browser Demo

Before going into details about this demo, let’s talk about the Windows Shell. The Windows Shell is the main area of user interaction in Windows. Many users refer to the Windows Explorer as “Windows”; however, it is just one part of the overall Windows Shell. The shell includes many other UI elements, such as the Taskbar, the Start Menu, Windows Explorer windows, the Recycle Bin, and search results. The most common and well known shell objects are files and folders. However, there are many other virtual objects such as the Recycle Bin, printers, Libraries, etc. Shell objects are organized into a hierarchical namespace that encompasses the file system namespace.

With a proper understanding of the Shell namespace, and access to the topmost Shell Object, the Desktop, you can traverse the entire computer, including files and folders, as well as printers, fonts, Program files, and Recorded TV. By using the Shell namespace, developers can access all the files and folders and all the Known Folders (which we briefly mentioned in the Libraries Under the Hood post) in a given computer.

Up until now, this was blocked from .Net developers; however, the new Code Pack provides an Explorer Browser control as well as well the defined objects, structs, and APIs needed to traverse the Shell namespace. In the Shell part of the Code Pack (Shell project), you can find Explorer Browser controls for WPF and WinForms, as well as all the functionality needed to work with Known Folders, and a wide range of Shell objects (under the Common folder) such as ShellObject, ShellLink, ShellFolder, ShellItem, ShellCollection, ShellThumbnail, and the list continues. The Taskbar and Libraries implementation also uses many of these shell objects. As shown in the following picture, the Explorer Browser demo spotlights the Explorer Browser control that can help visualize the Shell namespace.

image

Similarly, Shell Hierarchy Tree Demo, shows how you can traverse the entire computer contents using the Shell namespace starting from the topmost Desktop objects:

image

Sensors

The Code Pack also contains Sensor APIs for the Windows 7 Sensor and Location platform. The Sensor project contains all the necessary definitions to work with Sensors including a SensorManager to get sensors, and a Sensor class to work with sensors, and of course a SensorReport that allows you to access the sensor data. The Sensor demos include two demos illustrating two sensors, Light and Motion. You can use the Virtual Light Sensor from Win 7 RC SDK to simulate different lighting conditions, and the very simple Ambient Light Level application to display a visual representation of the total amount of light detected by the light sensor.

image

Extended Linguistic Services Demo

Extended Linguistic Services (ELS) is another very interesting technology available in Windows 7. ELS is a new feature in Windows 7 that allows developers to use the same small set of APIs to leverage a variety of advanced linguistic functions. By using ELS APIs in Windows 7, developers can auto-detect the language of any piece of Unicode text and use that information to make smarter user experience choices for customers around the world. The ELS platform is the interface between your application and ELS services. It provides a simple way to leverage several kinds of linguistic functionality through the same API, which allows the application to access and use specific services. For more information about the API, see Extended Linguistic Services Reference.

By using the MappingAvailableServices class you can query a given string for the language in which the string is written. This allows you to either verify the language in which a string is written or ask in what language a string is written. This can be a very powerful tool for multi-language application developers.

And it is easy to use. For example, the following line of code tries to detect what languages are used in the string:

scriptDetection.RecognizeText("This is English. АБВГД.", null))

The output will be something like:

Recognized 2 script ranges

Range from 0 to 16, script Latn

Range from 17 to 22, script Cyrl

Taskbar and Libraries

The Code Pack provides the required APIs to program against the Taskbar and Libraries in Windows 7. For the Taskbar you can:

  • Create and manipulate JumpLists, including tasks and items
  • Display Dynamic Overlay Icons, Thumbnail Toolbars
  • Use the Taskbar progress bar

At this stage of the development, you cannot Control Custom Thumbnail Preview, and custom Preview, also known as AeroPeek. To have this functionality, you will need to use the Windows 7 Taskbar Sample .NET Interop Library.

With the Taskbar demo, you can learn how to have full control over your Jump List as shown in the next image:

image

The Code Pack also provides full support for Windows 7 Libraries, which we will talk about extensively in the next post.

Please note that this library is currently a work-in-progress and this is version 0.85 of this library. The final version of this library will be available around the time of the Windows 7 RTM release. Please help us improve by downloading the source code and sample, and using them in your applications. Please report bugs or any other comments and ideas you might have using the Discussion, and Issue Tracker.


Stay in Sync with Windows 7 Libraries

Here is another post in the Windows 7 Libraries post series. So far, we have covered what Windows 7 Libraries are (Understanding Windows 7 Libraries), their internal structure, and how they integrate into the Windows Shell (Libraries Under The Hood). You know the different programming models that are available to choose from (Light Up with Windows 7 Libraries), and you know how to use Windows Shell APIs to consume the contents of a library (Consuming the Contents of Windows 7 Libraries). In previous post, we focused on the Windows 7 Libraries helper functions (Windows 7 Libraries Helpers). In this post you will learn how to stay in sync with a library.

Before Windows 7 and Libraries, you stored your music files in the My Music folder. Before Windows 7, when you wanted to play music using the Windows Media Player, you had to manually point Windows Media Player to the correct music folder and choose a song. If you moved a song from the music folder, Windows Media Player didn’t know that the file was moved. The music file would still appear in the play list, but when it was time to play the song, you would see that annoying message that Windows Media Player can’t find that music file -- all because Windows Media Player was out of sync with My Music folder. In this model, Windows Media Player needs to maintain its own database of songs and locations, and the user has to update that database manually.

With Windows 7 Libraries, this error is far less probable. You may ask, "Why?" Well the answer is simple. The Windows Media Player that ships with Windows 7 does not maintain any internal database of songs, it completely relies on the Windows 7 Music, Video, and Pictures Libraries, and it is fully synced with these libraries. This means that when you remove a song from the Music Library, it is no longer present in the Windows Media Player playlist. The following image shows that by right clicking on the Music tab you can choose to manage the Music library.

image

Stay in sync

You may want your application to recognized when the user makes changes to the library description, adds or removes folders from the library, or updates any of the files included in the library. There are several ways to “stay in sync,” depending on your application’s requirements. Most applications should use the Shell programming model with SHChangeNotifyRegister for all non-service processes.

Using the Shell programming model

To detect changes in items within a library using the Shell programming model, use the SHChangeNotifyRegister to listen for changes to items in all library folders. SHChangeNotifyRegister can take a single library item, for example the Music library, as a parameter to receive notifications on all content included in the library. It will note ANY status change in any of the items in the Music library. Additionally, registering the library will allow the application to receive notifications on the fly, so libraries are updated immediately upon changes to the library. The user may additionally modify the folders that have been included in the library without using the IShellLibrary API. For this reason, you should call SHResolveLibrary each time the library is loaded to identify changes to the folders included in the library. This will preserve changes in the folder or folder name back in the library description file. If the user adds, removes, or reorders folders using the library management dialog or the IShellLibrary interface, these changes will be persisted directly to the underlying .library-ms file. Using this information, a client application can then re-enumerate the library folders.

Show the familiar library management dialog

This is the last piece of the Windows 7 Libraries. To promote consistency among Windows components and among content management elements, Windows 7 includes a common library management dialog for use in your application. If your application needs to manage a library folder, like the Music folder, invoking the library management dialog (SHShowManageLibraryUI) will provide a consistent user experience for managing Windows Explorer libraries. If you choose to use this interface, it will make the changes to the library directly – as if Windows Explorer modified the library contents directly. Note that this dialog will not return any information to your application; if you show library contents within your application, you will need to receive notifications directly from the Shell as describe above.

image

By default, the library management dialog blocks addition of unsupported folders to libraries. However, if your application relies on content from folders that cannot be indexed locally or remotely by Windows Search, you can invoke the dialog with the LMD_ALLOWUNINDEXABLENETWORKLOCATIONS flag set to override the default setting and allow you to include such folders You can also use one of the new APIs like SHAddFolderPathToLibrary, which we covered in the Windows 7 Libraries Helpers post, to add folders, including folders that cannot be indexed.

This post completes our Windows 7 Libraries tour. Next, we will cover the Managed Code API and start posting about Windows 7 Taskbar.


Windows 7 Libraries Helpers

After two weeks break, we are back to our normal technical posts, covering Windows 7 Libraries. This is the sixth post about Windows 7 Libraries. By now, you should be familiar with Windows 7 Libraries (Understanding Windows 7 Libraries) and understand the internal structure of Libraries and how they integrate into the Windows Shell (Libraries Under The Hood). You know the different programming models that are available to choose from (Light Up with Windows 7 Libraries), and you know how to use Windows Shell APIs to consume the contents of a library (Consuming the Contents of Windows 7 Libraries). In this post, we focus on the Windows 7 Libraries helper functions.

The IShellLibrary COM object presents a Windows 7 Library. This interface defines methods for creating and managing Libraries. You can work directly with the COM interface, but it's easier to use a higher abstraction layer of functions. Shobjidl.h provides the following library helper functions:

  • SHAddFolderPathToLibrary - Adds a folder to a library
  • SHCreateLibrary - Creates an IShellLibrary object
  • SHLoadLibraryFromItem - Creates and loads an IShellLibrary object from a specified library definition file
  • SHLoadLibraryFromKnownFolder - Creates and loads an IShellLibrary object for a specified KNOWNFOLDERID
  • SHLoadLibraryFromParsingName - Creates and loads an IShellLibrary object for a specified path
  • SHRemoveFolderPathFromLibrary - Removes a folder from a library
  • SHResolveFolderPathInLibrary - Attempts to resolve the target location of a library folder that has been moved or renamed
  • SHSaveLibraryInFolderPath - Saves an IShellLibrary object to disk

Note: if you are really interested in how these helper functions are implemented, simply go to the Shobjidl.h h file and search for these functions.

Using these help functions, you can perform most of the management operations on a Library without using the lower level APIs. For example, you can add a folder to a Library using SHAddFolderPathToLibrary without calling ISHellLibrary::Commit(). Since the IShellLibrary is a COM-based API, you will need to CoInitialize the COM objects. After doing so, all you need to do is use the helper functions. The following code snippet creates a new Library, adds a folder to it, and saves the Library definition file in the Libraries folder.

IShellLibrary *pLib; 
IShellItem *pSi;
SHCreateLibrary(IID_PPV_ARGS(&pLib));
SHAddFolderPathToLibrary( pLib, L"C:\\temp ");
pLib ->SaveInKnownFolder(
FOLDERID_Libraries,
L"Test 1 Lib",
LSF_MAKEUNIQUENAME,
& pSi);
 
The helper functions don’t provide all the functionality in the IShellLibrary interface. By working directly with the IShellLibrary interface, you can get or set the Library icon using the GetIcon, and SetIcon. But more importantly, you can get the contents of a library by using the IShellLibrary:: GetFolders() as shown in the next code snippet

IShellLibrary *pslLibrary;
SHLoadLibraryFromKnownFolder(
FOLDERID_PicturesLibrary,
STGM_READ,
IID_PPV_ARGS(&pslLibrary));

IShellItemArray *psiaFolders;
pslLibrary->GetFolders(
LFF_FORCEFILESYSTEM,
IID_PPV_ARGS(&psiaFolders));
 
In the next post we will walk through some techniques of staying in sync with the library contents.


New Windows 7 Videos on Channel 9

Note – this post will be updated from time to time, to include new content. Last update: June 13th (New Sensor, Location and Windows Animation videos)

During the past couple of months, we have being working on Windows 7 videos featuring the new technologies in Windows 7. We’ve also created a Windows Topic area on Channel 9 to make it easier for you to find specific Windows 7 content. This topic area includes all the Windows 7 videos since PDC08, which was when we first introduced Windows 7. Since PDC, we have created enough content about specific Windows 7 features that it makes sense to group these topics into categories so they are easier for potential viewers to find. We hope these videos will help you to get started programming for Windows 7. Here is one new of the many videos we have for you on Channel 9.

Here is the list of category topics and their associated videos:

Kernel and Internals

Inside Windows 7 - User Mode Scheduler

Inside Windows 7 - Service Control Manager

Mark Russinovich – Inside Windows 7

New NUMA Support with Windows 7

Windows 7: Best Practices for Developing for Windows Standard User

Taskbar

Windows 7 New Taskbar - An Overview

Taskbar - Behind the Scenes

Designing the Windows 7 Taskbar

Jump into the Windows 7 Taskbar Jump Lists

Windows 7 Taskbar Beta Feedback 

Windows 7 Taskbar Advanced Topics

Graphics

Windows 7 Graphics Architecture Overview – Part 1

Windows 7 Graphics Architecture Overview – Part 2

Windows 7 Graphics Architecture Overview – Part 3

Windows 7 Graphics - WIC

Windows 7 Graphics - High DPI

Multi-Touch

Using Touch in Line-of-Business Applications

Programming Windows 7 Multi Touch – Part 1

Programming Windows 7 Multi Touch - Part 2

Sensor and Location

Windows 7: The Sensor and Location Platform - Building Context Aware Applications

Windows 7 Sensor and Location Platform Overview

Windows 7 Location Platform Overview

Windows 7 Sensor and Location Platform – Deep Dive

Ribbon

A Lap around Windows 7 Ribbon

Ribbon Markup Overview

Ribbon Deep Dive

Windows Animation Manager

Windows Animation Overview

Inside Windows 7: Animation Manager Deep Dive and Tutorial

Libraries and Federated Search

Windows 7: Find and Organize Part 1 - The User Experience

Windows 7: Find and Organize Part 2 - Building Federated Search applications

Windows 7: Empower users to find, visualize and organize their data with Libraries and the Explorer

PowerShell

PowerShell V2 Functions

PowerShell V2 Sessions and Debugging

PowerShell V2 Modules

General

The story behind a few of the new Windows 7 wallpapers

Windows 7 RC Features Slideshow on eWeek


Windows 7 RC Is Here

Both the Windows 7 RC and the Windows 7 RC SDK are available for download; make sure you get them quickly. As Brandon mentioned in his post, with this release we are coming down the home stretch to release of the final product. Now is when you need to be working on your applications to make sure they are Windows 7 Compatible. On top of that, you can use new Windows 7 features such as the Sensor and Location Platform, Taskbar, Libraries, Multi-Touch, the new Graphics APIs, the Windows Ribbon, and many other important and exciting technologies offered by Windows 7 to Light Up your applications. We covered some of these features in previous posts, and we will continue to do so.

The Windows 7 RC incorporates user feedback from Beta users and includes a few new features we have already covered in Some Changes Since Beta for RC and A few more changes from Beta to RC….

We hope that you will install the Windows 7 RC build soon so you can start testing your application and taking advantage of the new features and technologies Windows 7 offers. We’ve created a short list of resources to help get you started programming for Windows 7:

  • The Windows page on MSDN is the one-stop shop for Windows client developers
  • At Develop for Windows 7 you can find all the information you need about specific technologies like Direct2D, Taskbar, Sensor and Location, Power Shell 2, Windows Ribbon, and many more
  • Windows Application Compatibility is the one page you want to visit to make sure your application runs properly on Windows 7. This page includes content and tools to test and fix any application compatibility issues
  • Directly accessible from that page, is the Windows 7 Application Quality Cookbook
  • Also still very relevant, is the Windows Vista Application Compatibility Cookbook. Most Windows 7 compatibility issues are the direct result of the changes introduced in the Windows Vista timeframe, and are included as topics in this Cookbook (UAC, Session 0 Service Isolation, IE Protected Mode, etc.), which is a great starting point for addressing these issues
  • New Windows 7 videos on the Channel 9 Windows page explore specific features and technologies, and are great “windows” to the Windows engineering team
  • As always, the E7 blog is an amazing source of the engineering back story behind Windows 7
  • For IT-Pro, we have our SPRINGBOARD SERIES

And, of course, continue to watch for our new posts on specific Windows 7 features