3 ms·
In my experience with both classic Win32 programming in the 1990s in C++ and then later in C# and now in Rust is that it is obscenely verbose. It takes a solid
by jiggawatts 6y ago
In my experience with both classic Win32 programming in the 1990s in C++ and then later in C# and now in Rust is that it is obscenely verbose. It takes a solid page of code to call a single method, and most of that is just noise.
A typical method call is:
Allocate some struct.
Zero it out.
Set a "length" field to the length of the struct.
Populate the "parameter" fields.
Call the API with the struct and 0 for the return buffer size.
You get back an expected error code for "buffer too small".
You allocate the required space in your buffer.
Call the method again. The amount of data changed! Loop and try again.[1]
You get your data! Now to check the error codes.
Look up error code using a Win32 method.
Free the buffer.
If on top of that I'd have to call "into" and "from" for every field and every parameter at every step... I think I might just snap and go become a bricklayer or something.
PS: Win32 strings and Rust strings aren't compatible. I've already noticed that the current version of the Microsoft code-generator seems to use an awful lot of [u16] arrays, which are not the system OsString struct.
For comparison, the C# "String" type isn't directly compatible with Win32 strings either, but the FFI engine handles this conversion back-and-forth for you.
Similarly, few (any?) languages have good built-in support for the various kinds of "struct-followed-by-dynamic-data" typically returned by system APIs. Notably, C does not have any way to encode this in the type system, so API metadata tends to just leave this out or describe it in the documentation. If the metadata was extented to cover this kind of thing, then the generators could do all sorts of clever things with this scenario...
[1] A lot of people don't do this, they assume that the second call will always succeed. Programming in this style leads to rare random errors throughout the entire application. A loop is required for correctness for most (but not all) Win32 calls that return dynamic data...
- orra 6y agoYou're not wrong that win32 is very verbose. > A loop is required for correctness for most (but not all) Win32 calls that return dynamic data... A very good point, one very rarely mentioned. Of course, the win32 windowing and common controls and single threaded, so if it's the same application it won't have changed underneath you.
- flohofwoe 6y agoC99 has made all that a lot easier, you can setup, initialize a struct and call the function taking a pointer to that struct all in one call: https://github.com/floooh/sokol-samples/blob/89f5825ab5d36906b2dc219efbce3d00be9e7cdf/d3d11/d3d11entry.c#L52-L59 https://github.com/floooh/sokol-samples/blob/89f5825ab5d3690... The Win32 window system functions and D3D11 are actually very convenient to work with in C99, better than C++ actually.
- ape4 6y agoYes as a simple example of a wrapper: std::string nicerGetComputerName() { char buf[1024]; DWORD len = sizeof(buf); memset(buf, 0, sizeof(buf)); GetComputerNameA(buf, &len); return buf; }
- criddell 6y agoWhat encoding will the string returned from GetComputerNameA be in? Is it the code page for the current regional setting? Wouldn't it be safer to use GetComputerNameW to get a UTF-16 string?
- jiggawatts 6y ago#1) GetComputerNameA can fail. You didn't check the error message or convert it into a C++ exception. #2) You probably want GetComputerNameW. GetComputerNameA uses an unspecified encoding that you'd have to handle. #3) A 1KB buffer is excessive! Over-sized buffers can blow through the L1 cache all too easily, slowing down applications. It's also stack allocated, and this can be a problem in some scenarios. #4) You're forcing the computer name to be heap allocated. This can also be problematic in some scenarios, and is slower. GetComputerName might be used in logging scenarios that are already trying to handle cases such as "out of memory"! Allocating more memory may be unacceptable. You get the idea.
- saagarjha 6y agoFYI: char buf[1024] = {0};
- gruez 6y agoTo be fair everything starting from "Call the API with the struct and 0 for the return buffer size." Is fairly uncommon, at least in my experience.