Influential scribe Charles Petzold: How I figured out the Windows API

From Programming Windows to Xamarin Forms

Charles Petzold

Interview Charles Petzold’s book Programming Windows (in various editions) was how a generation of developers learned to code for Windows – 25 years ago when Microsoft’s operating system was the hot new thing.

Charles Petzold

Petzold now works for Xamarin, and I caught up with him at the company's Evolve conference in Atlanta last week. It may come as a shock to anyone who assumed Petzold’s books were the official guide, but the truth is he worked out the Windows API for himself.

“I never worked for Microsoft,” he told me. “I really liked figuring things out on my own. Early on in the development of a new version of Windows, I would explore it, I would try out various things, I would see what worked, I would see what didn’t work.”

Petzold’s examples were in C and were low-level. How did he come up with the code?

“It was derived from the early samples,” he said. “For Windows 1.0, there were five sample apps. The first one was a hello world app, except that it was very large because it included a menu, and the menu triggered an about box, and a lot of people thought all that stuff was necessary.

"So I took that program and stripped away everything that seemed to be not necessary. I took away the about box and the program still worked. I took away the menu and the program still worked. I took away other things and simplified it as much as possible and that became the first sample program in my book.”

Did he really write a book for Windows 1.0 that hardly anyone used?

“I started that first book with Windows 1.0. Windows 1.0 had tiled windows and I had a paragraph in my first chapter explaining why tiled windows were better than overlapping windows. Then as I was working on the book, betas of Windows 2.0 started becoming available. So we decided to shift the book for Windows 2.0, which had overlapping windows. Eventually I came to believe that my job was to explain how to use the APIs and not indicate whether this was a good or bad way of doing it. I felt more like a scientist exploring nature and Windows was my environment. You don’t pass judgment on nature, you just explain how it works.”

'Windows Forms seemed to me what MFC should have been'

All the early books used C, but then Petzold shifted to writing about C# and .NET. Was he comfortable with such a high level of abstraction?

“I started out with machine code and assembly language. I took to C very well. I kind of skipped over C++ and MFC. C++ always looked like an extraordinarily ugly language to me, and MFC was not a very well-designed object-oriented wrapper around the API. I thought there should be something better. My first experience of .NET was with C# and Windows Forms, and was extremely favourable. I liked C# sharp as a programming language, I liked .NET as a sturdy, extensive, runtime library, and Windows Forms seemed to me what MFC should have been.”

Petzold then moved on to Windows Presentation Foundation, which uses C#, and the XML-based XAML language, which uses a declarative approach to coding a user interface.

“I made a lot of mistakes with the WPF book. The first mistake was that it had no screenshots. That was because a new version of Windows was anticipated, so all of the screenshots would have looked old-fashioned. The other thing was that the first half of the book has no XAML. Then I introduced XAML and explored the syntax. I regretted that decision, because the more I played around with XAML, the more I loved it.

“I never got into Blend. I have to this day handwritten every piece of XAML that I have published. Blend is great for designers because it implements a lot of sophisticated behaviours, but for what I like to do hand coding XAML is preferable, particularly because I have to publish it.”

'It seems a little unreasonable to ask that Xamarin Forms be a Silverlight clone'

Now Petzold works full time for Xamarin, where he is writing a book on Xamarin Forms, which uses C# and XAML for mobile apps running on iOS, Android or Windows Phone.

“Early this year I wrote a blog entry saying that I was available for consulting work and Xamarin contacted me and said 'Would you like to write a book about this thing we are developing?' I said I will need more than the $15,000 advance that you normally get for a book, because the old model is no longer working for me, so they hired me as a contractor. After about four months they offered me a job.”

The XAML aspect of Xamarin Forms was a later addition. The early releases used only C#. Petzold’s book was going to be mainly code. “Then when the XAML finally became available, and it actually worked, and all of our sudden you had this ability to do the stuff that we used to do in WPF and Silverlight, to define a visual tree for the display of each of the elements, and then apply data bindings to all those. It looks so elegant in XAML and so clunky in code that no sane person could resist.”

Unfortunately the XAML used by Xamarin is incompatible with the XAML used by Microsoft for WPF, Silverlight, and Windows 8 Store Apps (and these are not fully compatible with one another).

“A lot of people when they found out that XAML would be supported by Xamarin assumed that they would be able to take their Windows phone XAML and just move it over. It seems a little unreasonable to ask that Xamarin Forms be a Silverlight clone. When people say that the XAML should be compatible they are really saying that Xamarin Forms should be Silverlight and should run on the iPhone.”

Unlike the Windows API, Xamarin Forms (and the Xamarin platform) is a niche technology. “There are a lot of programmers who apparently are coding in Objective-C and Java,” says Petzold, referring to the native languages for IOS and Android.

That said, for developers coming to mobile from the Microsoft platform, Petzold believes the technology is attractive. “For the array of C# programmers, the Xamarin platform makes a lot of sense,” he added.

At a session during Evolve, Petzold gleefully shows how he got round the absence of vector graphics in Xamarin Forms by coding a sphere using a BoxView control, reducing its width to just a few pixels in order to get a line. “The Forms developers tell me I am abusing the BoxView,” he said.

It is good to know that 25 years on from Programming Windows, Petzold has retained his independent spirit. ®


Biting the hand that feeds IT © 1998–2017