In a recent Dr. Dobb's blog post, I described how I use DNS Service Discovery (DNS-SD) in a product I work on for Cisco Systems. That project uses the Avahi browser, which does not have a Windows port. In this article, I'll show you how to perform service discovery on Windows using Apple's Bonjour SDK for Windows.
DNS-SD On Windows
Microsoft has been pushing us to use UPnP as our network discovery protocol, to the exclusion of all others. As a result, Windows ships with no support for DNS-SD nada. And that might be the end of it, if it weren't for Apple's vested interest in having iTunes installed on every Windows machine in the world.
iTunes uses DNS-SD to share music catalogs across Local Area Networks a natural choice with the native support in OS X for Zeroconf. Rather than isolate Windows users in an pocket UPnP universe, Apple chose to instead port the service discovery components of Bonjour to Windows, and install it with every copy of Windows. Thus, Windows and OS X users can happily share their iTunes libraries with no unusual calisthenics required.
To sweeten the deal, Apple has released a Bonjour SDK for Windows, which is currently shipping version 3.0 from their developer support site. In Apple's words:
"The Bonjour SDK for Windows includes the latest version of Bonjour as well as header files, libraries, and sample code to assist developers in creating Bonjour enabled applications on Windows. The SDK has been updated with the Bonjour core that is bundled with iTunes 10.3.1. This release will bring all existing Bonjour functionality released in Mac OS X 10.7 into the Bonjour for Windows product."
It sounds pretty good, doesn't it?
An Emphasis on Kit
Installing the SDK is a decision-free breeze, as shown in Figure 1:
The installation may be easy, but the dirty truth about the Bonjour SDK is that it is not much of an SDK at all. The developer interface to Bonjour services are packaged in a single DLL, and the SDK provides programmers with sample code that illustrates how to use some of the bindings in C, C#, Java, and VB. There is no documentation on the interfaces, no source for the Bonjour components, and the few available samples don't begin to provide comprehensive coverage of the interface.
For the most part, the Bonjour SDK interfaces follow a single pattern. Each request made to the API returns immediately and gives you a reference that you can use to track the progress of your request. That reference can be converted into a file handle, and you can use the file handle to see when your request has some data to produce.
When your request has generated some data, and the Bonjour components are ready to deliver it to you, they do so by a callback mechanism the Bonjour components make calls into your C or C++ program and provide the data you requested.
Most of the callbacks include a flag parameter. You can check the flag to see if there is any more data expected. If there isn't, you can delete the reference and you are then done with that particular call. Otherwise, you will have to wait for the request to be responded to when the Bonjour components get around to it.
An Overview


