When looping through windows on a screen with the code below, and searching for a non-existing windowtitle, i get a crash? No problem when the title exists (CS)
struct Window * GetWindow( STRPTR screenname, STRPTR windowabbrev ) { struct Screen *nxtscreen, *scr; struct Window *win, *selwin; int lgtwt, lgtabbr; char wtshort[31]; BOOL winfound =FALSE; lgtabbr = strlen(windowabbrev); if (screenname[0] == 0 ) { scr = LockPubScreen(NULL); } else scr = LockPubScreen(screenname); if (!(scr )) return (NULL); /// NULL Default public screen , mopstly WB screen win = scr->FirstWindow; while (win) { lgtwt = strlen(win->Title); /// ########crash here when all windows looped through if (lgtwt >= lgtabbr) { strncpy (wtshort, (char *)win->Title, lgtabbr); wtshort[lgtabbr]=0; if (strcmp(wtshort, windowabbrev) == 0) { winfound=TRUE; break; } } win = win->NextWindow; } if (winfound) { PutStr("found"); UnlockPubScreen(screenname, scr); return (win); } else { UnlockPubScreen(screenname, scr); return (NULL); } }
What is wring with the loop?
Why doesn't it stop when win is NULL ??
I'm not exactly sure as it has been a while since I last touched this issue (my MicroA1 broke down 2 years ago...), but if I remember correctly, some windows have no titlestring, hence win->Title is NULL.
Maybe this helps:
Try it.
Added an extra safeguard and removed some superfluous code. I'm sorry this being a nasty habbit of mine.
OldFart
@OldFart: your routine does not return the window which matches the title but the next one.
Here is a shorter one and it works (tested):
Note: I intentionally use Strnicmp which does not care about upper/lower case. You can as well use strncmp if case matters.
@OldFart &Thomas
Thanks for the help
Works OK now