News

Mixing up Vista's Managed and Unmanaged APIs

Mention application programming interfaces (APIs) and Windows Vista together, and many programmers assume you're referring to the much-ballyhooed managed interfaces that comprise the new .NET Framework 3.0.

Mention application programming interfaces (APIs) and Windows Vista together, and many programmers assume you're referring to the much-ballyhooed managed interfaces that comprise the new .NET Framework 3.0.

But the managed APIs are only half -- actually, far less than half -- of the story. The vast majority of Vista's 7,000 brand-new APIs are still native, or unmanaged, code. These APIs allow developers to call everything from Vista's new Sidebar to Sideshow, not to mention the Vista P2P networking, search and security systems.

So what's a developer to do when it comes to potentially mixing managed and unmanaged code in Vista applications?

"A lot of Vista is still native code. Microsoft wanted to wrap more APIs in a managed layer, but [the team] ran out of time. We will do more of that over time," says Visual Studio Product Manager Jason McConnell.

That said, "mixing and matching [APIs] isn't really something new," McConnell continues. "But before Microsoft introduced last year the C++/CLI extensions [to C++], the syntax wasn't clean and it was quite difficult. But with C++/CLI, you can even mix native and managed code within one assembly."

McConnell says ISVs -- like Adobe Systems, Intuit and even Microsoft's own Game Studios and Expression Interactive Designer -- are most likely to bridge the managed and unmanaged gap. To encourage more custom application developers to mix up managed and unmanaged APIs, Microsoft plans to "get more prescriptive" about how to do so, says McConnell, by publishing more code samples before the end of calendar 2006 as well as a white paper dedicated to this topic by February 2007.

Guidance Please
Kate Gregory, a partner in Gregory Consulting Ltd., based in Peterborough, Ontario, Canada, is the one writing the aforementioned white paper and helping Microsoft develop new code samples.

"Mixing managed and unmanaged is definitely the right way to go, but it can feel like uncharted territory," says Gregory, who also serves as a Microsoft Most Valuable Professional for Visual C++. She is also founder of the East of Toronto .NET Users Group.

"There are so many ways to do everything that the challenge is being sure you're doing it the best way," she adds.

"Everyday tasks that are musts on Vista, such as adding a manifest to work properly with UAC [User Account Control], are far simpler in C++ than in C# or VB. When an API is hard to P/Invoke [the Platform Invoke service that allows managed code to invoke unmanaged functions residing in dynamic link libraries], the C++ developer can just call it. But when functionality's available as a managed API -- as with Windows Communication Foundation and Windows Presentation Foundation -- then the C++ developer can get to that too, from C++/CLI. It's the best of both worlds and those of us who remember how to do it are reaping benefits now."

Another developer who has been cutting his teeth on mixing and matching managed and unmanaged APIs is David Johnson, vice president of technology with IP Commerce, a Microsoft Gold Certified Partner based in Denver, Colo.

IP Commerce is developing the PASS Commerce Center, which is a .NET Framework 3.0 "container" application that hosts both .NET 3.0 and .NET 2.0 commerce application "modules," or services. The application, which is scheduled for general availability in January 2007, integrates with Vista's Windows Security Center APIs, Vista's Desktop Search APIs and Windows Error Reporting, says Johnson. It also "plays nicely" with Vista's UAC system, and includes a PASS Windows Sidebar gadget, as well as a PASS Cash Management Center -- another WPF-based application that integrates directly with Office 2007 via Office's Open XML file formats.

Managed vs. Unmanaged

Courtesy of Microsoft C++ MVP Kate Gregory, here's what separates managed code from unmanaged code:

Managed code is what Visual Basic .NET and C# compilers create. It compiles to Intermediate Language (IL), not to machine code that could run directly on your computer. Managed code runs in the Common Language Runtime. The runtime offers a wide variety of services to your running code.

Unmanaged code compiles directly to machine code that ran on the machine where you compiled it, and on other machines as long as they had the same chip, or nearly the same. It doesn't get services such as security or memory management from an invisible runtime -- it gets them from the operating system.
See her full explanation by clicking here.

"The Vista Desktop Search APIs are likely to be missed by ISVs that don't work with custom file formats," Johnson notes. "However, these APIs enable data and content to be available through desktop search no matter where it resides."

"We're using these APIs to index data that normally resides out on the network, blurring the lines between desktop and Internet search. Within PASS, this provides great benefit to businesses working with or providing financial services," Johnson says.

"We've had to develop quite a few wrappers for bridging these worlds within our applications. It would be nice to see some of the newer unmanaged APIs come with these bridges built in," as Microsoft has said it's planning to do with future Windows releases, Johnson explains.

"We've experienced some issues due to inadequate or inadequately accessible tools and documentation/training for some of the finer nuances of the new Vista APIs," he acknowledges. Undocumented changes to the APIs ahead of Vista's Nov. 8 launch haven't helped, Johnson adds.

Despite these challenges, Johnson says the new APIs open "a whole new world of opportunity. Keeping an open mind and being patient while Microsoft fine-tunes the ISV experience around these new technologies will be well worth the effort."

About the Author

Mary Jo Foley is editor of the ZDNet "All About Microsoft" blog and has been covering Microsoft for about two decades. She's the author of "Microsoft 2.0" (John Wiley & Sons, 2008), which examines what's next for Microsoft in the post-Gates era.

comments powered by Disqus

Featured

Subscribe on YouTube