Click here to Skip to main content
12,358,699 members (57,311 online)
Rate this:
 
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 12:42pm
Ron Anders11.3K
Comments
Sergey Alexandrovich Kryukov 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 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?
Andrew Cherednik 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
Sergey Alexandrovich Kryukov 22-Jan-13 19:25pm
   
Ah, thank you very much! fixed it.
—SA
Ron Anders 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160621.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2016
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