Readit News logoReadit News
masternight · 9 months ago
There is something I like about win32 gui programming. It's a little idiosyncratic, but if you read Raymond Chen's blog you'll see why.

The win32 API has its origins on the 8088 processor and doing things a certain way results in saving 40 bytes of code or uses one less register or something.

I wrote a lot of toy gui apps using mingw and Petzold's book back in the day. Writing custom controls, drawing graphics and text, handling scrolling, hit testing etc was all a lot of fun.

I see in your app you're using strcpy, sprintf. Any kind of serious programming you should be using the length-checked variants. I'm surprised the compiler didn't spew.

You'll also find that the Win32 API has a lot of replacements for what's in the C standard library. If you really want to try and get the executable size down, see if you can write your app using only <Windows.h> and no cstdlib. Instead of memset() you've got ZeroMemory(), instead of memcpy() you've got CopyMemory().

At some point writing raw C code becomes painful. Still, I think doing your first few attempts in raw C is the best way to learn. Managing all the minutiae gives you a great sense of what's going on while you're learning.

If you want to play more with win32 gui programming, I'd have a look at the WTL (Windows Template Library). It's a C++ wrapper around the win32 API and makes it much easier to reason about what's going on.

BarryGuff · 9 months ago
> There is something I like about win32 gui programming

Totally agree with you. I use an excellent PC app called AlomWare Toolbox, and it's the epitome of Win32 design (https://www.alomware.com/images/tab-automation.png), and despite it doing so much it's only about 3 MB in size because of it. No frameworks with it either, just a single executable file. I wish all software were still like this.

bmacho · 9 months ago
Is the font-size adjustable? It's too small on my screen
scripturial · 9 months ago
At minimum, these days, if you dont use strncpy instead of strcpy, you’ll have to suffer through every man and his dog (or AI tool) forever telling you to do otherwise. (For me this is one of the main arguments of using zig, a lot of these common pitfalls are minimized by using zig, but c is fine as well)
masternight · 9 months ago
Heh, and if you use strncpy() you'll have to suffer through me lecturing you on why strncpy() is the wrong function to use as well.
userbinator · 9 months ago
Instead of memset() you've got ZeroMemory(), instead of memcpy() you've got CopyMemory().

I believe MSVC intrinsics will use the rep stos/movs instructions, which are even smaller than calling functions (which includes the size of their import table entries too.)

kevin_thibedeau · 9 months ago
The standard allows memset/memcpy to be replaced by inline code. There is no need to use non-standard extensions to get a performance boost.
rcarmo · 9 months ago
I spent a lot of time doing that and to be honest, I miss the ability to develop for native UIs with native code.
codebolt · 9 months ago
> You'll also find that the Win32 API has a lot of replacements for what's in the C standard library. If you really want to try and get the executable size down, see if you can write your app using only <Windows.h> and no cstdlib. Instead of memset() you've got ZeroMemory(), instead of memcpy() you've got CopyMemory().

I see he's also using fopen/fread/fclose rather than CreateFile/ReadFile/WriteFile/etc.

donnachangstein · 9 months ago
> I see he's also using fopen/fread/fclose rather than CreateFile/ReadFile/WriteFile/etc.

It's a todo list, not a network service. So what if it's using unbounded strcpy's all over the place? It has basically no attack surface. He wrote it for himself, not for criticism from the HN hoi polloi.

For once maybe take someone's work at face value instead of critiquing every mundane detail in order to feel like the smartest person in the room.

Computers are tools to get stuff done. Sometimes those tools are not pretty.

I place much of the criticism being levied here in the same category as the "we must rewrite 'ls' in Rust for security" nonsense that is regularly praised here.

int_19h · 9 months ago
To be fair, CreateFile etc are a lot more verbose than fopen.

Deleted Comment

raverbashing · 9 months ago
I agree with most of this, but let's be honest, win32 gui programming (like this) is/was a pain

Even MFC barely took the edge out. It's amazing how much better Borland built their "Delphi like" C++ library.

> Instead of memset() you've got ZeroMemory(), instead of memcpy() you've got CopyMemory().

Yes. And your best API for opening (anything but files but maybe files as well) is... CreateFile

Aah the memories :)

int_19h · 9 months ago
> It's amazing how much better Borland built their "Delphi like" C++ library.

As I recall, it wasn't "Delphi like", but rather literally the same VCL that Delphi used. That's why C++Builder had all those language extensions - they mapped 1:1 to the corresponding Delphi language features so that you could take any random Delphi unit (like VCL) and just use it from C++. In fact, C++Builder could even compile Delphi source code.

MortyWaves · 9 months ago
> Instead of memset() you've got ZeroMemory(), instead of memcpy() you've got CopyMemory().

What is or was the purpose of providing these instead of the existing Windows C std?

userbinator · 9 months ago
It's worth remembering that Windows 1.x and 2.x predates the C89 standard. This also explains why WINAPI calling convention was inherited from Pascal instead of C. The C standard library was "just another competitor" at the time.
masternight · 9 months ago
Those functions explicitly? I can't find any definitive explanation on why they exist.

It looks like nowdays ZeroMemory() and RtlZeroMemory() are just macros for memset().

Here's an article on some of the RECT helper functions. Relevant for the 8088 CPU but probably not so much today: https://devblogs.microsoft.com/oldnewthing/20200224-00/?p=10...

mike_hearn · 9 months ago
Windows didn't standardize on C. It was mostly assembly and some Pascal in the beginning with C and C++ later.

Microsoft have always viewed C as just another language, it's not privileged in the way UNIX privileges C. By implication, the C standard library was provided by your compiler and shipped with your app as a dependency on Windows, it wasn't provided by the operating system.

These days that's been changing, partly because lots of installers dumped the MSVC runtime into c:\windows\system and so whether it was a part of the OS or not became blurred and partly because Microsoft got more willing to privilege languages at the OS level. Even so, the Windows group retains a commitment to language independence that other operating systems just don't have. WinRT comes with lots of metadata for binding it into other languages, for example.

lmz · 9 months ago
You could write code without using libc / the C runtime. You still can.
int_19h · 9 months ago
Unlike Unix, Windows historically didn't have a standard C runtime at all. Stuff like MSVCRT.DLL etc came later (and are themselves implemented on top of Win32 API, not directly on top of syscalls as is typical in Unix land).
rlkf · 9 months ago
I second this, and just want to add that strsafe.h contains replacements for the runtime string routines.
electroly · 9 months ago
Instead of laboriously calling CreateWindow() for every control, traditionally we would lay out a dialog resource in a .rc file (Visual Studio still has the dialog editor to do it visually) and then use CreateDialog() instead of CreateWindow(). This will create all the controls for you. Add an application manifest and you can get modern UI styling and high-DPI support.
userbinator · 9 months ago
You also get automatic tabbing between controls, and a few other keyboard shortcuts this way. Note that resizing them still needs to be done manually if you want that, but that's usually easy and not more than a few hundred bytes of code.
pjmlp · 9 months ago
Only UNIX overlaps C standard library with OS library, and back in 1985 (Windows 1.0 release), there was still no standard to speak of.

Sure there was K&R C, which each OS outside UNIX cherry picked what would be available.

Additionally outside UNIX clones, the tradition among vendors has been that the C compiler is responsible for the standard library, not the platform.

Thus the C library was provided by Borland, Watcom, Symantec, Microsoft, Green Hills, Zortech,....

Note it was the same on Mac OS, until MPW came to be.

As it was in IBM and Unisys, micros and mainframes.

VMS before OpenVMS.

And so on.

Since Windows 10, you have the Universal C Runtime as well.

Narishma · 9 months ago
I think you replied to the wrong comment.
urbandw311er · 9 months ago
Great answer, helpful and not judgemental.
belter · 9 months ago
Look it up in Petzold they used to say...
kazinator · 9 months ago
However, this approach is easily translatable to a language that has decent FFI, and requires nothing else: no resource compiler and linker to make a resource DLL.

Resource files and their binary format are not a good API.

If you have those CreateWindow calls in a decently high level language, you can probably meta-program some resource-like DSL that fits right in the language.

int_19h · 9 months ago
You don't need a "resource DLL"; the compiled .rc file gets linked directly into the binary, and any Win32 C toolchain is capable of doing that, including MinGW.

As API goes, I don't see what's wrong with it (anymore so than Win32 in general). And you do get quite a lot for free, as GP mentioned. Hi-DPI, for example - .rc files use "dialog units" to measure all widgets, which, unlike raw pixel values you pass to CreateWindow, are DPI-independent.

tonyedgecombe · 9 months ago
>Visual Studio still has the dialog editor to do it visually

They are using gcc.

electroly · 9 months ago
That doesn't matter; you can still use Visual Studio to create the .rc file. This technique still works great for MinGW-based projects. The important thing is that Visual Studio has a .rc dialog editor.
broken_broken_ · 9 months ago
I have done something similar for Linux under 2 KiB in assembly some time ago: https://gaultier.github.io/blog/x11_x64.html

As others have said, doing so in pure C and linking dynamically, you can easily remain under 20 KiB, at least on Linux, but Windows should be even simpler since it ships with much more out of the box as part of the OS.

In any event, I salute the effort! You can try the linking options I mentioned at the end of my article, it should help getting the size down.

johnisgood · 9 months ago
Well, my somewhat extended TUI (ncurses) TODO program is 15K. Linux. Not statically linked though. I did not get around to build ncurses yet with musl.
Jamesits · 9 months ago
"Native Windows look and feel".

Before actually launching it, I hoped it's listview had context menu, and double clicking certain fields would lead to (in-line-ish) dropdown menus or textboxes.

Maybe people don't know how to design programs for the Win32 UI/UX anymore, or maybe I'm too old for this.

eviks · 9 months ago
> no frameworks

Checks out: blurry fonts in scaled dpi, no Tab support, can't Ctrl-A select text in text fields and do all the other stuff that pre-modern frameworks offered you, errors on adding a row, ...

> modern

In what way?

Dwedit · 9 months ago
Example of setting DPI awareness: https://github.com/Dwedit/GameStretcher/blob/master/Stretche...

This code dynamically checks for and calls one of the following: user32:SetProcessDpiAwarenessContext, shcore:SetProcessDpiAwareness, then user32:SetProcessDPIAware. If the Windows version is extremely old and doesn't implement any of those (Windows XP or earlier), it won't call anything.

scq · 9 months ago
You can also set it in the application manifest, which is recommended over setting it programmatically: https://learn.microsoft.com/en-us/windows/win32/hidpi/settin...
Tringi · 9 months ago
It's a little more complicated if you are to be using themes, GDI and common controls. Some time ago I put together this example: https://github.com/tringi/win32-dpi

The high DPI support in Windows went through quite an evolution since XP, but mostly to fix what app programmers messed up. You can have nice and crisp XP at 250% dpi if you do things right, e.g.: https://x.com/TheBobPony/status/1733196004881482191/photo/1

sargstuff · 9 months ago
Ah isn't the user32:<windows api functions> a framework not related to 'pure' C?
userbinator · 9 months ago
It's "modern" in that it's much bigger than necessary, while missing a lot of functionality.

(A lot of what you mention is missing is trivial to add, especially tabbing between controls.)

card_zero · 9 months ago
I think font scaling is fixed (i.e. turned on) with SetThreadDpiAwarenessContext(-4). Or whatever the constant that equates to -4 is called.
bobsmooth · 9 months ago
It's modern in that he just released it.
AaronAPU · 9 months ago
The 6502 programmer in me is dying inside that 278kb now passes as lightweight.
abbeyj · 9 months ago
I tried to reproduce this binary to see what the 278 KB was being taken up by. The first obstacle that I ran into was that the build.bat file doesn't work if you have git configured to use core.autocrlf=false. Changing that to core.autocrlf=true and recloning was sufficient to get me building.

I'm using x86_64-15.1.0-release-win32-seh-msvcrt-rt_v12-rev0.7z from https://github.com/niXman/mingw-builds-binaries/releases/tag... as the toolchain. This produces a 102 KB .exe file. Right off the bat we are doing much better than the claimed 278 KB. Maybe the author is using a different toolchain or different settings? Exact steps to reproduce would be welcome.

We can improve this by passing some switches to GCC.

    gcc -Os => 100 KB
    gcc -Oz => 99 KB
    gcc -flto => 101 KB
    gcc -s => 51 KB
    gcc -s -Oz -flto => 47 KB
If all you are interested in is a small .exe size, there is plenty of room for improvement here.

azhenley · 9 months ago
If you had a blog or YouTube channel where you just went around to open source projects optimizing them down, I’d be very interested.
jedimastert · 9 months ago
> Maybe the author is using a different toolchain or different settings?

I wonder if they are compiling with debugging symbols? I don't know how much this would change things in vanilla C but that would be my first guess

tecleandor · 9 months ago
I think there's a typo somewhere. The repo and the release says 27KB (not 278).
tomalbrc · 9 months ago
They used mingw, read TFA
jcelerier · 9 months ago
A lot of it is due to the platform and executable format. Things can be much more lightweight when there's no information for stack traces, no dynamic linking infrastructure, no exception handling tables (necessary even in C in case exceptions traverse a c function,) etc.
userbinator · 9 months ago
no dynamic linking infrastructure

You get that for free on Windows.

no exception handling tables (necessary even in C in case exceptions traverse a c function,

Not necessary if you're using pure C. SEH is rarely necessary either.

nottorp · 9 months ago
Maybe we could petition the demo scene competitions to have a '64kb TODO app' category.
jackjeff · 9 months ago
I’m surprised it’s that big to be honest. I was expecting it to be smaller or half the size to be taken by some app icon. I remember writing this kind of stuff back in the days and it was smaller.

Is it due to MinGw maybe?

mhd · 9 months ago
This reminds me of the days when all of a sudden win32 programming in assembly became hip enough, probably as a response to the increasing size of shareware downloads ('twas the dark time of MFC).

Combined with early Palm Pilot 68k programming, those were the last hurrahs of non-retrocomputing asm I can remember.

kgabis · 9 months ago
6502? Luxury! In my times you were lucky to have a processor.
pineaux · 9 months ago
A processor? Luuuxury! In my time we worked twenty-six hours a day, did all the calculations with pen and paper and would be thrilled to use an abacus!
Borg3 · 9 months ago
Hehe :) Okey.. I have sth easier to write.. but smaller:

15kB quickrun.exe :) C, pure Win32 API.. No hacks to shrink binary, Mingw32 compiler.

Its GUI app to quickly launch any application via alias.

stevekemp · 9 months ago
I'm spending some time this evening debugging a failure I have with an emulator I've written - it emulates a system running a Z80 processor with 64k of RAM.

Sometimes I too take a step back and look at the way things have changed. But then again we've made a lot of progress for the size-changes I guess.

Deleted Comment

kazinator · 9 months ago
I remember being thunderstruck in early 1990-something upon seeing that Nethack compiled to a 900kb+ executable.
webprofusion · 9 months ago
For some reason it's built with GCC instead of using MSVC, and there are no optimizations enabled (for speed or size).

Deleted Comment

p0w3n3d · 9 months ago
it's about ABI, also 8bit << ... << 64bit architecture. Every pointer is 8 times longer. Don't complain, just admire. It's an art.
tonyarkles · 9 months ago
That barely fits on a 5 1/4” floppy!

Deleted Comment

fx1994 · 9 months ago
try to open "new" calculator on win10/win11. It's like loading another operating system... useless.
alternatex · 9 months ago
Probably JIT-ing? Compiled against native should make startup instant. No idea how they compile these.
toxi360 · 9 months ago
Hello friends, I made this app just to try it out and have some fun, haha, but the comments are right, something like this could have been done more sensibly with C++ or other languages, ahaha.
tomtomtom777 · 9 months ago
This is exactly how I've learned to create my first Windows programs about 30 years ago, except that I'd use a C++ compiler.

I am not sure why but I believe writing C style code with a C++ compiler was how the windows API was documented to be used. I think Microsoft just went with the idea that C++ was an improved superset of C so should be used even for C-style code.

cesarb · 9 months ago
> I think Microsoft just went with the idea that C++ was an improved superset of C so should be used even for C-style code.

And as a consequence, for a long time their official C compiler was stuck on C89, while other platforms already had full C99 support and beyond. I believe their support for newer C standards has gotten better since then, but AFAIK they still don't have full C99 support.

drooopy · 9 months ago
Unironically, I would rather use your to-do app over the default Windows 11 one.
toxi360 · 9 months ago
AHhhahah thanks
Johanx64 · 9 months ago
It's just the way it should be.

Other language doesn't fundamentally change anything if you want to use win32 API, if anything it would make things more confusing.

People often fall prey to C++isms, and they would have made the whole thing an even more confusing mess (to people not familiar with win32 API).

This is a very cute thing to do and some familiarity with win32 APIs is a nice basic competency thing, regardless of what other people think.

int_19h · 9 months ago
C++ actually makes a lot of sense specifically for Win32 API because RAII takes care of releasing all the numerous handles at the right time in the right manner. Also, things like string operations are a pain in pure C (indeed, this app uses stuff like strcpy which is a recipe for buffer overruns etc).

WTL (https://en.wikipedia.org/wiki/Windows_Template_Library) is the oldschool way to do low-level Win32 coding in C++.

toxi360 · 9 months ago
https://github.com/Efeckc17/YoutubeGO By the way, you can also review or examine this application, I would be very happy :D
kmangutov · 9 months ago
This is exactly the sort of project (clean, native UI) that motivated me to learn programming, kudos!
transcriptase · 9 months ago
Seeing a lot of chirps in here from people who work on software or websites that load megabytes of JS or C# or in order to send 278kb of telemetry every time the user moves their mouse.
lostmsu · 9 months ago
A similar app in C# + WinForms is under 10KB on disk and 6MB RAM. This app takes 1.5MB RAM. Both start instantly.
throwaway2037 · 9 months ago
Impressive. Can you share a link to the source code?