yottaawesome / programming-windows-5th-edition

Unofficial source code repo for Charles Petzold's Programming Windows 5th Edition.
https://www.microsoftpressstore.com/store/programming-windows-9780735692633
84 stars 35 forks source link

Bug in `src/Chap10/PoePoem/PoePoem.c` #3

Closed yanpeng closed 1 year ago

yanpeng commented 1 year ago

Hi,

bother you again...

Line 108 of PoePoem.c, it is

pText = '\0'

The same line of the book is

*pText = '\0'

I think the latter is right. The type of pText is char*, the intention of the author is to replace the last \ with \0. In this way, we make certain that the string we loaded into the memory is ending with \0, so when DrawTextA in WM_PAINT, we can pass -1 to the cchText argument safely.

In the text after this program, the author said:

In the POEPOEM.RC resource script, the user-defined resource is given the type TEXT and the text name "AnnabelLee":

ANNABELLEE TEXT POEPOEM.TXT

During WM_CREATE processing in WndProc, a handle to the resource is obtained using FindResource and LoadResource. The resource is locked using LockResource, and a small routine replaces the backslash (\) at the end of the file with a 0. This is for the benefit of the DrawText function used later during the WM_PAINT message.

After changing pText = '\0' to *pText = '\0', another problem arises: Visual C++ didn't allow to modify that piece of memory pointed by pText.

yottaawesome commented 1 year ago

Hi,

I'm currently on vacation, so I'll look at this as soon as I can.

yottaawesome commented 1 year ago

Actually, I think this is a modification that I did, because the original version crashed on line 108. This is because resources are immutable in memory, although the sample apparently worked back when it was first written.

I think line 108 could probably be deleted, but I'd have to test it first.

yanpeng commented 1 year ago

Actually, I think this is a modification that I did, because the original version crashed on line 108. This is because resources are immutable in memory, although the sample apparently worked back when it was first written.

I think line 108 could probably be deleted, but I'd have to test it first.

oh, sorry for bothering. yeah, if *pText = '\0', it would crash the program, the reason is what you said above (Visual Studio showed this error). But if we deleted this line, we have to calculate the length of the text (loaded by LockResource) because we cannot guarantee the text ending with \0. We also cannot pass -1 to cchText of DrawTextA, pass the length of the text instead.

Have a good holiday!!!

yottaawesome commented 1 year ago

I've added a note explaining the change I did to clarify why it differs from the original sample. Thanks for your keen eye, as I had forgotten I had done this change.

yanpeng commented 1 year ago

Thanks for your archiving these codes. BTW, the afxres.h in resource script should be replaced with winres.h, or cannot compile the project on new version visual studio. (or that afxres.h needs MFC component to be installed, but I never touched MFC.)

yottaawesome commented 1 year ago

Yeah, that's mentioned in the readme under the "Building" category. I chose to leave it as I don't want to change the original samples too much if it can be avoided.