Click here to Skip to main content
11,428,825 members (63,406 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi,

I have the code:

 
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 22-Jan-13 13:42pm
Comments
Sergey Alexandrovich Kryukov at 22-Jan-13 18:45pm
   
And how about thinking a bit? :-)
—SA

1 solution

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

Solution 3

All you need to do is to call:

buf.ReleaseBuffer();

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
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 22-Jan-13 19:10pm
   
Agree, a 5.
—SA
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.
—SA
Sergey Alexandrovich Kryukov at 22-Jan-13 19:25pm
   
Ah, thank you very much! fixed it.
—SA
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)



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | 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