问题描述
我有代码:
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是:SONAR X1 Producer - [Two Track.cwp - Track]
Bracket是:20 - 所以它找到了''''字符。
我希望p为''T''
我所遇到的麻烦就是atlsimpstr.h中的一个断言line:
ATLASSERT((iChar> = 0)&&(iChar< = GetLength())); //索引''\ 0''是好的
iChar是21并且小于字符串长度。
什么可能是我的问题?
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?
推荐答案
buf.ReleaseBuffer();
后你的
after your
GetWindowText(GetForegroundWindow(), buf.GetBuffer(), 255);
更好,将GetWindowText行修改为:
Better of, modify the GetWindowText line to:
GetWindowText(GetForegroundWindow(), CStrBuf(buf, 255), 255);
并摆脱Preallocate()。在这种情况下,您不再需要担心ReleaseBuffer()。阅读 []在MSDN中获取更多信息。这是一个非常方便的包装
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
这篇关于CString :: GetAt的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!