10 ms·
TheForger's Win32 API Programming Tutorial
- pjc50 7y agoAh, the classic approach from WinProc() upwards. While I think it might be useful to work through this once, since then you understand what a message loop is and how it dispatches to controls, but for almost all practical purposes if you want to build a classic app it's easier to do it in C++ with MFC/ATL. Or even C#/Winforms. I note this version uses LPSTR rather than LPTSTR; perhaps it predates the UNICODE fiasco and the brief window where UCS-2 seemed like a good idea.
- Crinus 7y agoNote that if you decide to use MFC you still need to know Win32. A common misconception and source of frustration about MFC is that it is a layer over Win32 or that it somehow replaces it, which is not the case and if you (royal you here) expect that you'll be disappointed. MFC is a helper framework build with Win32 and C++ to ease some bits of making a Windows application, but even for the most basic stuff you are still dealing with Win32. It isn't a "Win32" or "MFC" situation, it is a "just Win32" or "Win32 and MFC" situation. If you don't like Win32 or feel it is too hard, chances are MFC wont change your opinion either (though you can like Win32 and dislike MFC - after all it has been left to rot).
- Ididntdothis 7y agoI have done quite a bit of Win32 in my life and I think even to this day this knowledge helps me diagnose issues with UI performance of browsers and other applications.
- axilmar 7y agoWin32, especially when it comes to GUI programming, is one of the worst APIs out there. MFC is also quite bad because of huge abstraction leakages; it fills like Win32 with bolted on classes. It seems like Win32 happened a bit by accident, a bit by overengineering and a bit by Microsoft's attempt to lock everyone to their 'special' development environments, which did not include C as a conscious choice by Microsoft.
- thrower123 7y ago> It seems like Win32 happened a bit by accident, a bit by overengineering and a bit by Microsoft's attempt to lock everyone to their 'special' development environments, which did not include C as a conscious choice by Microsoft. Maybe I just haven't had my coffee yet, but I'm not understanding what this is supposed to mean.
- pjc50 7y agoI don't understand this either. They've always offered C. In many ways Win32 and NT are as much a ""C operating system"" as UNIX. The only "special development environment" might be Visual Basic.
- dockd 7y agoMaybe they meant Microsoft didn't really stick to the C standard library. _strdup vs strdup. malloc vs HeapAlloc. assert, fopen, etc. The functions mostly exist but really aren't the way to program Windows.
- projektfu 7y agoAfter the p-machine era, MS was all in on C. They had some technical reason for preferring the pascal calling convention, like saving a few bytes of program text.
- mschaef 7y ago> Win32, especially when it comes to GUI programming, is one of the worst APIs out there. It's also one of the first. Win32's origins extend back over 35 years and start on machines with a tiny fraction of the capability of even the smallest of today's modern PC's. It was also built with a different set of tradeoffs and in a radically different culture of software development. (Most modern developers hadn't been born at the time of it's initial design, and most of the people who were actively writing this stuff in the early-to-mid 1980's, I'm sure have moved on. 35 years is pretty much a career.) > MFC is also quite bad because of huge abstraction leakages; it fills like Win32 with bolted on classes. That's pretty much what it was... by design. There was a more object-oriented library that Microsoft developed, prior to MFC. They tested it on developers that had essentially just come up to speed on the (then-new) Win16 API, who all complained about having to ascend another learning curve for a fancy object oriented language and framework. Microsoft's reaction was to drop the older framework (referred to as AFX) and develop MFC as a thin wrapper around Win16. (This is part of why there are AFX prefixes scattered throughout the MFC codebase.... it's a legacy of the earlier framework.) Even then, early versions of MFC had some utility, mainly around making things more type safe. Because it bound C++ instances to Window system instances, it made it easy to attach user data structures to UI components. (Otherwise, you were worrying about window or class words, which were Microsoft's original approach to that problem and used by MFC under the hood.) MFC also handled parsing of window message arguments. Win16 also sent messages with just a word and a long, when what you really might need is a set of mouse button flags and an x/y position or something. MFC helped with that too, by 'cracking' the message arguments into something closer to what you'd expect. (To this day, Win32 documentation still includes instructions on parsing WPARAMS, etc. https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-mousemove#remarks https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-m... )
- wirrbel 7y agoMy first job out of university was in a company that developed a GUI appication based on the Win32 C API. In fact, the product has been on the market for so long, that it's code had been a Win16 application in the past and pre-dated Win 3.1 (It is a product for a niche market, and had been the first Windows program on the market there, with all other software being MS-DOS Applications). So, I came from university, had written Software during my master thesis with Qt, had some exposure to Gtk on my home PC a few years before, and I faced this obscure Win32 API, which looks ugly, appaling, antiquated and plainly awful. But: Development was actually quite pleasant, and, while it didn't offer the flexibility of full-blown object oriented libraries like Gtk or Qt, it was amazingly good at getting stuff done. I wouldn't start a new project in it of course and it seems Microsoft has pretty much abandoned the API. But, I have to admit that I can see how it served Microsoft and its developer community well for a few years. In fact, a lot of things in there where quite thoughtful and I can still appreciate the non-bloaty way of doing things, if I see how many processes and memory an electron app uses that doesn't do much more than what a simple Win32 program does.
- jussij 7y ago> and I faced this obscure Win32 API, which looks ugly, appalling, antiquated and plainly awful. As you point out, the code you had to deal with was actually written using the Win16 API. That Win16 API dates back to the first ever version of Windows which takes you back to a pre 1990 era. As someone who did work on both Win16 and Win32, what I found amazing is Microsoft designed Win32 such that you could take your Win16 code, do a minimal number of changes and recompile that code for Win32. That meant you could take your old Win16 code designed for a now obsolete version of Windows, do a minimal number of tweaks, recompile the code using a 32 bit compiler and have it running on the most resent 32 bit versions of Windows. It was my first experience of a major coding upgrade and it was the last such upgrade that was anywhere near as simple as that upgrade.
- unalignedaccess 7y ago> and have it running on the most resent 32 bit versions of Windows. Or 64-bit, for that matter.
- efdee 7y agoAh, WinProg. The chatroom that built the world. [0] Good times, huhu. [0] https://www.wired.com/2005/11/chat-room-that-built-the-world/ https://www.wired.com/2005/11/chat-room-that-built-the-world...
- nurettin 7y agoLong time I haven't seen the IRC huhu.
- markus_zhang 7y agoIs it still active? Actually I followed the link to winprog so I guess it's still active.
- GarMan 7y agoThe irc channel is dead. But most of us mentioned in that article are still in another irc channel together and talk very frequently.
- markus_zhang 7y agoOh I see, thanks, no wonder I couldn't find anything on Winprog. Is it private? Can I join?
- efdee 7y agoJust #winprog on EFnet. Not sure how active it still is nowadays.
- markus_zhang 7y agoOh thanks will give it a try.
- jmiller099 7y agosups? :)
- russtrotter 7y agoI cut my teeth on making the jump from turbo pascal programs to GUI via the Win32 API. Its "rawness" felt fun, i.e. slinging around "long" pointers to stuff and big event handling switch statement state machines. As long as you didn't misbehave in the message loop, you could just kinda tinker endlessly. As both the UI evolved to many more complicated controls and the toolset was getting more abstract, I'd always find myself asking "dangit! i wish i could just get the UUID for the control that does the fizzywizzybizbang control that i see in Excel here!" Maybe that mindset is akin to a modern UI developer buried in quirksmode.org and their browser console debugger.
- dotandimet 7y agoMy favorite Win32 tutorial is by Bartosz Milewski, I found it as a series of posts on his CodeCoop website (https://web.archive.org/web/20110129015721/http://www.codecoop.com/ https://web.archive.org/web/20110129015721/http://www.codeco... ), that were later incorporated into his book "C++ in Action" (also findable online). He uses C++ to wrap every resource acquisition in a constructor and every release of a resource in the corresponding destructor, so that by allocating new objects on the stack you avoid a whole swath of memory management errors. This was my first encounter with RAII (not sure if he calls it that), and it seems a particularly elegant way to write C++ and to make the raw Win32 C API a lot less scary.
- contextfree 7y agoWindows development team now has an internal C++ library used inside Windows source code to do similar things, which was recently made public and open source on GitHub: https://github.com/Microsoft/wil/wiki https://github.com/Microsoft/wil/wiki
- bluedino 7y agoWin32 was complicated. Lots of cleanup work. No forced structure. Combine that with having to use C, and the amount of low-skilled porogrammers out there, you could really see why there was so much terrible Windows software out there. Learning Windows programming gave me so much insight to programs crashing and all the graphical bugs, beause I had caused them all in my own porograms before.
- GnarfGnarf 7y agoWhy on earth would anyone want to learn Win32 in 2019? It's horribly unproductive. I've been doing Win32 (also called "Petzold-style") since 1995, and am pleased to say I'm converting to Qt for multi-platform capability. When I look at the dreck necessary to create a dialog box in Win32, and compare to more modern frameworks, it's like milking a cow vs. buying milk at the store. Sure, you're "into the fundamentals", but is that really how you want to spend your time?
- zmodem 7y ago> Why on earth would anyone want to learn Win32 in 2019? Because it's the native API on the most common desktop OS?
- dstaley 7y agoThe Win32 API is on life-support. Microsoft will maintain backwards compatibility for legacy apps of course, but learning Win32 for new development doesn't seem like a good investment (unless, of course, you want to get a job maintaining a legacy Win32 app).
- badsectoracula 7y agoAnything that runs on the Windows desktop uses Win32 at some point in its stack. If you work on anything that is close to Win32 then you may also need to work with Win32 itself. For example if you work on a game engine or toolset then you must know Win32 - especially if you work on the tools, even if you are using a wrapper like wxWidgets or Qt. The only time where you can avoid Win32 is if you're using something that completely abstracts away the entire system, like Java Swing or a web browser.
- dstaley 7y agoIt's a little bit more complicated. Sure, if you're doing low-level programming I imagine you might run into the Win32 API, but if you're just building typical desktop applications (particularly for newer devices like the Surface Pro X or Surface Neo), you'll most likely want to use UWP. Usage of Win32 for app development is only going to decrease overtime for typical consumer applications. I'm sure businesses will continue building LOB apps in WPF and WinForms until the heat-death of the universe, but there's also businesses building apps in VB6 and you don't see many people recommending to learn that.
- ovi256 7y agoThis is strictly about the win32 GUI API. There are other win32 APIs that are quite interesting: * the networking API (native proactor servers, something that Linux doesn't have yet AFAIK) * the security and process API, which I had the pleasure to explore recently, in order to contribute to an open source server that launches a single user server for a connected user, under the user's profile. The CreateProcessAsUser function and its interactions with user authorization tokens is quite something.
- matheusmoreira 7y ago> native proactor servers I/O completion ports? Those are great and I wish Linux had a similar API. Epoll doesn't fully solve the asynchronous I/O problem since files always have data available to read. The kernel should be able to do reads and writes in the background while my program does something else.
- Rerarom 7y agoAlso, there aren't many books about those (compared to e.g. books about the *nix API). I only know of Hart's Windows System Programming.
- zerkten 7y agoWindows via C/C++ is another one in this area. Windows Kernel Programming by Pavel Yosifovich is a recent book, but it goes much lower-level. Windows Internals is pretty good, but it's not a programming book per se. It's trying to document the design of the OS, help with troubleshooting when you don't have the source to the OS, and help developers understand the high-level concepts.
- Rerarom 7y agoThanks!
- eps 7y agoA very well working combo is "Windows Internals" for the high-level view + MSDN docs for the details.
- voldacar 7y agoI'm not much of a windows user so sorry if it's a dumb question, but don't newer UI libraries like UWP and WinUI just act as wrappers around Win32 functions? Like if I wrote a UWP/WinUI/etc program and disassembled it, wouldn't it just call Win32 functions in some system dll to do things like create windows, receive messages etc?
- zerr 7y agoNot sure about others, but e.g. WinForms is a wrapper for Win32 controls. WPF has its own rendering.
- snagglegaggle 7y agoYes, for the most part. Some of them use Win32 functions but do their own drawing, like the newer opacity parts in the UWP settings menu.
- tasty_freeze 7y agoI wrote an emulator for an old 8080 microcomputer in 1999 or so using the Win32 API. First I bought the Petzold book, but as my program grew there were still a lot of API issues that Petzold simply couldn't fit into a book of that already impressive size. Then I got a "Win32 API Bible" type of book, then another from a different author, then I collected the multi-volume official win32 api books from microsoft, ..., and ended up with more than two linear feet of books. And yet development was still painful. Sometimes I would want to add a feature and I'd be amazed that in 30 minutes it was done, but far more often there would be something that didn't work and I'd literally spend two weeks of free time changing the order of state setting calls, or setting state that the books didn't say needed to be set but I had seen other source code do it, reading other program source, searching online, asking in forums... The last release was in 2005. I'd like to work on it more, but there is no way I'm ever going back to that codebase.
- maxpert 7y agoAaaaah the memory of making the an efficient self extracting archive. What is brilliant about the idea is that even to this very day many "new ideas" live around same event loop idea.
- rantwasp 7y agohahahah. React? :)
- mschaef 7y agoFunny you mention that, but one of the better pieces of MFC related advice I read was to have a single central location for computing UI element states. It did feel a lot like React does these days (albeit with less sophistication in the underlying 'DOM model' back then, and nothing like React's differencing stuff.) http://www.flounder.com/controls.htm http://www.flounder.com/controls.htm To me at least, if you change the terms in that article, it looks a lot like the arguments in favor of moving away from ad hoc jQuery spaghetti and to React's single global rendering pass.
- mckinney 7y agoAh the Win32 API. Oddly, I learned quite a lot of object oriented fundamentals doing C++ Win32 programming indirectly using Paul DiLascia's excellent book "Windows++"[1]. One of the few 90s era programming books I still have. [1]https://web.archive.org/web/20090606220454/http://www.dilascia.com/wpp.htm https://web.archive.org/web/20090606220454/http://www.dilasc...
- DarmokJalad1701 7y agoNice! I learned programming using "Iczelion's Win32ASM tutorials" (archived here: http://www.interq.or.jp/chubu/r6/masm32/masm006.html http://www.interq.or.jp/chubu/r6/masm32/masm006.html ). Same API, but in assembler. The terminology and variable names look very familiar