In-Depth

VSLive! 2007: Target Key Native APIs in Vista from Managed Code

Exploit Vista in new apps with Auks, Desktop Search, and Win32 UI elements, while maintaining backwards compatibility.

Target Key Native APIs in Vista from Managed Code

Watch the video of the session! (Running time: 50 minutes)

Microsoft Developer Evangelist Nima Dilmaghani gave attendees an in-depth look at Windows Vista as a development environment in his morning session at VSLive! San Francisco 2007. No, Dilmaghani's presentation wasn't about using .NET framework 3.0. Instead, he focused on Vista's native APIs outside of the .NET Framework.

Of course you want to make sure your code runs on Vista. But the challenge is learning how to exploit Vista while maintaining backwards compatibility. Dilmaghani discussed how to make applications User Account Control (UAC) aware, how to integrate Vista's Desktop Search into your applications, and how to exploit Vista's Win32 UI elements—in particular, the command links in Windows Forms and the common file dialogs.

To tackle these tasks, you need a working knowledge of Visual Studio (VS) 2005 and VC++ or VB; the COM Interop and P/Invoke (Platform Invoke Services); Windows Forms and ADO.NET; and UAC.

Making Apps UAC-Aware
Dilmaghani's first set of tips concerned making apps UAC-aware. Apps run as standard user by default, while privilege elevation of apps occurs out of process and requires user consent. Making an app UAC-aware takes several steps. First, you embed a manifest entry in a managed assembly (or executable). Next, you launch an elevated process and set the security shield icon. Finally, you debug the completed app in VS 2005.

When you debug a standard user app (without a UAC data redirect), you have to disable the debugging host process. And to debug a process that requires elevation, you must launch an elevated VS 2005 process by right-clicking on the VS 2005 app icon and selecting Run.

Dilmaghani provided an additional tip about launching an elevated process. He observed that you can't elevate a running process, but you can programmatically launch a new elevated process. He also noted some practical down-level system considerations for backwards compatibility. He said that the UAC feature is only available on Vista, but the UAC manifest entries and the BCM_SETSHIELD message are ignored on previous Windows versions. For required privileges and additional information about launching elevated processes, he said that you need to explicitly check the user's token and invited the audience to check in with the UAC team blog.

Integrate Windows Vista Desktop Search
Dilmaghani turned the session focus to integrating Vista's Desktop Search into apps. This integration can considerably upgrade your app. You can access Vista's built-in client indexing platform and property system; integration of searches throughout Vista, including the start menu, control panel, and Explorer windows; support for building apps on top of Vista, including Outlook 2007 and OneNote 2007; and query APIs, including OLE DB (Object Linking and Embedding Database) Provider for Windows Search and IsearchQueryHelper.

Dilmaghani showed how you can easily use the OLEDB Provider from ADO.NET in your apps. It relies on a connection string format, SQL query syntax, and it supports Advanced Query Syntax. For example, you can dynamically retry the connection string by calling IsearchQueryHelper.get_ConnectionString.

He pointed out that Desktop Search uses tlbimp.exe to create an interop assembly. If you open the SearchAPI in the Lib folder of the Windows Software Development Kit (SDK), then you can create an instance of IsearchQueryHelper and call the GenerateSQLFromUserQuerty method to convert an advanced query syntax string into a SQL query. You can pass the SQL query to the OLE DB Provider for Windows Search.

Again, Dilmaghani emphasized backwards compatibility. You can download Windows Desktop Search (WDS) 3.0 for XP Service Pack 2 (SP2) and Windows Server 2003 Service Pack 1 (SP1).

Exploit Win 32 UI Functionality
Dilmaghani used the remainder of the session to explain how to implement command links in Windows Forms. Before you can use the command links in Windows Forms, he observed that you need to learn how to add the BS_COMMANDLINK style, call the BCM_SETNOTE message, and provide design-time support.

He gave a detailed demonstration of this process, which amounted to setting the necessary button styles and the note text. For example, you can set the styles by using BS_COMMANDLINK to override the CreateParams property and add the styles during button creation.

For more information about command links in the Windows SDK, Dimaghani suggested that you check out the SendMessage interop definition on PINVOKE.NET, the interop wiki. You'll also find details on the FlatStyle.System style and UI guidelines for using command links in dialogs on MSDN.

Dilmaghani also discussed using the new common file dialogs. The common file dialogs use six new IFileDialog COM Interop APIs as well as existing Win32 APIs, which are supported on Vista. (The Win32 APIs will display new Vista dialogs as long as customization options aren't specified.)

Dilmaghani explained that the common file dialogs involve wrapping the Win32 APIs with Windows Forms and Windows Presentation Foundation (WPF), both of which specify customization parameters. However, you must change your code and overcome current COM Interop challenges to use them. Namely, additional work is needed before you can call "Add Reference." Also, the new COM Interop APIs are standard v-table based interfaces, so they don't support Idispatch.

You have interesting options for using the new common file dialogs to manage your code. Advanced techniques include hand-crafting COM Interop definitions and building a managed wrapper using C++. Simpler options include using P/Invoke with Win32 APIs and using the Windows SDK, or "Vista Bridge," samples.

As for backwards compatibility, you must have Vista to use command links and the new common file dialogs. However, you can add this functionality to existing apps so they run conditionally in Vista.

Dilmaghani ended his session by encouraging the audience to work towards putting that "Certified for Windows Vista" logo on both existing and new apps.

About the Author

Lee The's first computer was a state-of-the-art unit with 48K RAM and a 1MHz processor. He has been writing and editing computer magazine articles since then, in between scuba diving trips. He's based in the San Francisco Bay Area.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube