Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have the code:

C++
CString buf;
buf.Preallocate(255);
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
Comments
Sergey Alexandrovich Kryukov 22-Jan-13 18:45pm    
And how about thinking a bit? :-)
—SA

1 solution

All you need to do is to call:

C++
buf.ReleaseBuffer();


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


Better of, modify the GetWindowText line to:

C++
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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jan-13 19:10pm    
Agree, a 5.
—SA
Jibesh 22-Jan-13 19:13pm    
SA did you press the wrong vote?? or forgot to vote?
chaau 22-Jan-13 19:17pm    
very cool: +10 points for posting an answer, and -16 for a downvote.
Jibesh 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 22-Jan-13 19:26pm    
Fixed, sorry for inconvenient. If you suspect such thing, please don't hesitate to comment. Sometime it happens.
—SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900