Click here to Skip to main content
11,806,026 members (68,922 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++

I have the code:

CString buf;
GetWindowText(GetForegroundWindow(), buf.GetBuffer(), 255);
int Bracket = buf.Find("[");
char p = buf.GetAt(Bracket+1);

buf is: "SONAR X1 Producer - [Two Track.cwp - Track]"
Bracket is: 20 - So it found the '[' character.

I want p to be 'T'

All I get for my trouble is an assertion in atlsimpstr.h at line:

ATLASSERT( (iChar >= 0) && (iChar <= GetLength()) ); // Indexing the '\0' is OK

iChar is 21 and less than the string length.

What the diddly hoo could be my problem?
Posted 22-Jan-13 12:42pm
Sergey Alexandrovich Kryukov at 22-Jan-13 18:45pm
And how about thinking a bit? :-)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

All you need to do is to call:


after your
GetWindowText(GetForegroundWindow(), buf.GetBuffer(), 255);

Better of, modify the GetWindowText line to:

GetWindowText(GetForegroundWindow(), CStrBuf(buf, 255), 255);
and get rid of Preallocate(). In this case you would not need to worry about ReleaseBuffer() anymore. Read about CStrBuf[^] in MSDN for more information. It's a very handy wrapper
Sergey Alexandrovich Kryukov at 22-Jan-13 19:10pm
Agree, a 5.
jibesh at 22-Jan-13 19:13pm
SA did you press the wrong vote?? or forgot to vote?
Andrew Cherednik at 22-Jan-13 19:17pm
very cool: +10 points for posting an answer, and -16 for a downvote.
jibesh at 22-Jan-13 19:19pm
my +5. I dont know who did downvote the correct solution, i mentioned the name SA because his comment to this solution says a 5 but vote shows 1 may be he forgot to vote.
Sergey Alexandrovich Kryukov at 22-Jan-13 19:26pm
Fixed, sorry for inconvenient. If you suspect such thing, please don't hesitate to comment. Sometime it happens.
Sergey Alexandrovich Kryukov at 22-Jan-13 19:25pm
Ah, thank you very much! fixed it.
Ron Anders at 22-Jan-13 21:20pm
Thanks everyone for all your help. I really appreciate it.

@ Andrew Cherednik - Thanks man, your solution fixed it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 300
1 Richard Deeming 225
2 Sergey Alexandrovich Kryukov 170
3 ppolymorphe 125
4 Abhinav S 118
0 OriginalGriff 3,300
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100