1

I'm working through Petzold "Programming Windows" 5th edition, using Visual Studio 2015 Community Edition on a Windows 8.1 machine, and having problems with the PoePoem program on p. 432-437.

The program will compile, but throws an exception on execution.

Apparently, in the WM_CREATE message processing, while I can load the text of the poem as a resource and count the number of lines, the debugger reports an access violation in the last line of this code snippet when I attempt to write a '\0' character in place of the backslash '\\'.

Evidently things have changed sufficiently in 20 years that the program as written no longer works. Is there a workaround?

hResource = LoadResource (hInst, 
                      FindResource (hInst, TEXT ("AnnabelLee"),
                                           TEXT ("TEXT"))) ;
          
          pText = (char *) LockResource (hResource) ;
          iNumLines = 0 ;
          
          while (*pText != '\\' && *pText != '\0')
          {
               if (*pText == '\n')
                    iNumLines ++ ;
               pText = AnsiNext (pText) ;
          }
          *pText = '\0' ;

I had a long wrestle with Visual Studio to get proper construction of the *.rc files and resource.h files, omitted the hInst declaration, misspelled the text several times. I finally got the program to compile, but I'm still missing something.

tadman
  • 208,517
  • 23
  • 234
  • 262
Confutus
  • 111
  • 3
  • Note: In C the conventional style is to call functions without a space after the function name. That differentiates them from things like `if (...)` and `while (...)`. – tadman Dec 17 '22 at 01:29
  • 1
    Are you sure `pText` is not `NULL` before you go dereferencing it? As per [the docs](https://learn.microsoft.com/en-us/previous-versions/windows/embedded/aa453417(v=msdn.10)): "If the loaded resource is locked, the return value is a pointer to the first byte of the resource; otherwise, it is NULL.". – tadman Dec 17 '22 at 01:30
  • I think you need to add a considerable amount of error checking here. A lot of things can and will go wrong when finding and loading resources. – tadman Dec 17 '22 at 01:31
  • note that ansinexyt is deprecated - use https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-charnexta – pm100 Dec 17 '22 at 01:37
  • The debugger reports that the error is in the assignment. The resource can be read, the value of iNumLines is correct, and pText points to the backslash character that ends the poem in the .txt file, but apparently it's read-only access. I attempted to reproduce the code as given in the text. It should work and apparently did on earlier versions; it's not mentioned in the errata, Unfortunately, Petzold included little discussion of error-checking. – Confutus Dec 17 '22 at 01:43
  • I don't remember being able to modify resources in memory. [Write access violation when trying to modify array](https://stackoverflow.com/questions/43239424/write-access-violation-when-trying-to-modify-array) seems to confirm they are read-only. I'd try without that line. I found this [source on github](https://github.com/yottaawesome/programming-windows-5th-edition/tree/master/src/Chap10/PoePoem) that works without it. The "fix" by that author was to assign 0 to the pointer rather than what it points at. Harmless but also pointless. – Retired Ninja Dec 17 '22 at 01:58
  • If it turns out the resource is not properly terminated [Embed Text File in a Resource in a native Windows Application](https://stackoverflow.com/questions/2933295/embed-text-file-in-a-resource-in-a-native-windows-application) shows how to make a copy and terminate that. – Retired Ninja Dec 17 '22 at 01:59
  • Just a guess ... Resources [locked] might be R/O unless the proper steps are taken: [Updating Resources](https://learn.microsoft.com/en-us/windows/win32/menurc/using-resources#updating-resources) On the link, there are `BeginUpdateResource` and `EndUpdateResource` functions. My _guess_ is that these didn't exist "back in the day". – Craig Estey Dec 17 '22 at 01:59
  • @CraigEstey Looks like those update functions became available in Windows 2000 and the 5th edition book was published at the end of 1998. I don't remember anything about resources but Windows 95/98 were definitely less strict about many things that became read-only in Windows 2000+.I had to patch a few of our games for doing naughty things in 2000/XP. – Retired Ninja Dec 17 '22 at 02:14
  • 1
    @Retired Ninja and CraigEstey. That gave me a clue, and I found a workaround. Thank you. – Confutus Dec 17 '22 at 03:02

0 Answers0