5 ms·
Poster has not worked very deeply with Win32, is unaware of its conventions. Film at 11. For example: > Yes, that’s right, every user of the Event Tracing for
by asveikau 12y ago
Poster has not worked very deeply with Win32, is unaware of its conventions. Film at 11.
For example:
> Yes, that’s right, every user of the Event Tracing for Windows API has to do the arithmetic and layout of the packed structure format themselves.
These represent very common idioms in Windows. One common idiom is about binary compatibility. Microsoft can change the length of the structure in a future rev of the SDK. Old callers can still work because they are specifying sizes and offsets - the library can look at these and know what to do. The other common idiom (very common in the NT kernel for example) is similar to what C99 introduced for structures with variable-length members, something C definitely didn't do for you and even today with it standardized still gets pretty clumsy.
The author lost all credibility when he wrote this:
> StringCbCopy((LPSTR)((char * )pSessionProperties + pSessionProperties->LoggerNameOffset),
sizeof(KERNEL_LOGGER_NAME), KERNEL_LOGGER_NAME);
Why on earth would you say (LPSTR)(char * ) ? That is literally saying (char * ) (char * ).
To me a "bad" API enters into questions like:
* How does it handle errors? Consistency is good. Swallowing them to the caller is bad.
* Does it give the caller the right level of detail about what is going on? It's especially common for it to be a black box and completely fail under some condition that the author did not envision. Some kind of escape hook that exposes implementation details makes library maintanence difficult but sometimes it's needed.
I haven't looked too deeply at etw but I don't suspect it fails at these. Maybe it errs too much on one extreme on the 2nd bullet.
- markbnj 12y agoHa, was my first thought as well: you could write this same article about essentially every aspect of the Win32 API.
- asveikau 12y agoAnd that article, having the same tone, would be about it being bad. But not understanding the conventions is not the same as being bad. A lot of this coding style is hard fought and battle tested.
- markbnj 12y agoNo question about it. My answer would actually be a lot more about the history of both Win32 and Win16, both of which I programmed in C++ for years. Looking at something like that now and proclaiming it bad is about the same thing as deploring the Mongol invasions.
- peterkelly 12y agoThe difference is that we no longer have to put up with Mongol invasions.
- angersock 12y agoA cumbersome API is a cumbersome API, regardless of why the developers thought it should be that way. Author understood conventions, I wager, but was treating it under the lens of "If I had to design this now, the sane way, where would the mismatch be?" The only saving grace of a lot of the MS stuff is that the MSDN docs are usually pretty good--usually.
- anon4 12y agoYes, that's frequently done in win32 land, but in this case, I'd argue the following should have been done 1) Why provide a copy of the parameter at the end of a struct? 2) How will you ever expand that struct given the fact that you put a variably-sized member at the end? 3) Why doesn't the current version of the header come with a char[] SessionName; member as last member, so it's at least halfway convenient? 4) No seriously - why is it copied in at the end and not a pointer? 5) That struct doesn't even have a DWORD size member as first member, are we expecting to get coupling between some flag and "oh and expect there to be additional members after that char array"? Even for Win32's usual badness, it's pretty bad.
- asveikau 12y ago> No seriously - why is it copied in at the end and not a pointer? Consider where you have seen similar patterns in the Unix world. The obvious one would be they intend to pass the buffer to kernel mode and a structure with lots of pointers inside will be a pain in the ass to pass over and validate. A flat buffer with a couple of offsets works better for that. Copy over the whole blob, check a few lengths. Generate your EFAULT errors in a single place. Better than following lots of user mode pointers.
- angersock 12y agoDude worked at RAD, so I'm willing to bet he's got some experience. In your LPSTR example, that could've been to silence compiler warnings about doing pointer arithmetic. EDIT: From bio: The most significant project I’ve created to date has been The Granny Animation SDK, a complete animation pipeline system that I first shipped in 1999 and which, 15 years later, still is in active use at many top-tier game studios. " So, uh, yeah. Maybe do some reading before spouting off on another's presumed abilities? EDIT2: Downvote all you want, but what code of yours has been in production for 15 years?
- asveikau 12y agoYou don't ever need to cast from char * to char * . Period. In 16 bit land the "L" meant something but that hasn't meant anything since long before the API he is calling existed.
- angersock 12y agoRight, but you're talking past me. Look at the code: (LPSTR)((char*)pSessionProperties + pSessionProperties->LoggerNameOffset) He's casting pSessionProperties to a char* so that he can do the arithmetic on it--otherwise, the compiler would assume "Oh, golly, I should increment the pSessionProperties pointer by LoggerNameOffset times sizeof(SessionProperties)". He has to make that conversion in order to do byte offsetting correctly. He then casts that back to what it wants (an LPSTR), to match the required argument type on the function. It's completely reasonable code, so stop complaining about it as though it weren't.
- dllthomas 12y agoI think the complaint was about the cast to LPSTR at the end. typedef char* PSTR, *LPSTR; After the addition, you've already got a char * - casting to a char * again by another name is unnecessary.
- angersock 12y agoSo, at that point, it gets to be a little more philosophical, right? I err on the side of "Oh, the API requests this type (which I'll pretend I don't know is actually just a char*), so I will explicitly cast to that". At least that way it's clear in the future that there is some explicit changing of types going on if, say, LPSTR ever changes. Silent "Oh, well, we all know that it's really going to be a <whatever> pointer here anyways" is a good way to get subtle bugs.
- peterkelly 12y ago> Why on earth would you say (LPSTR)(char * ) ? That is literally saying (char * ) (char * ). Possibly because it's not obvious that LPSTR is the same thing as char *. Sure, if you've done a non-trivial amount of windows programming I'm sure it's one of those things you just get use to. But as someone coming from the Unix world, I can't imagine why you wouldn't just use the native types (same with DWORD and friends).