This is a common question, due to the fact that several Win32 APIs take char* or LPTSTR parameters, and CString only provides a LPCTSTR overloaded operator, which won't work on these cases. You can go two ways:
CString::GetBuffer()
. It can be used in a manner similar to this: // prototype of a function that takes a LPTSTR parameter
// presented for argument's sake.
void test_func ( LPTSTR lpszString, int length );
CString string;
test_func ( string.GetBuffer ( 50 ), 50 );
string.ReleaseBuffer ( );
Forgetting to call CString::ReleaseBuffer()
can cause problems very difficult to debug as it releases the lock on CString's inner buffer.
One thing to keep in mind about CString::GetBuffer()
is that it returns a TCHAR* value (or LPTSTR, it's the same), so it is subject to the same ANSI/MBCS Vs. UNICODE convertions as most other Win32 APIs. It also means that if you're compiling a unicode version of your application, and specifically need a char* from your CString instance, you'll have to use a separate buffer of the appropriate type, and then make the convertion to unicode using one of the available API's before asigning it's value to the CString instance. The same goes if you're doing the exact opposite: getting a WCHAR* out of a CString, while compiling in MBCS mode.
2.Use a temporary variable. For example:
char temp[256];
CString string;
test_func ( temp, 256 );
string = temp;
3. 强制转换
CString string;
(char *)((LPCTSTR)string);
4.CString strDocTemplate;
char szFullPath[_MAX_PATH] = "assdfgggg";
strDocTemplate.Format("%s",szFullPath);
子对话框如何得到父对话框中的成员数据
答:((父对话框类 *)GetParent())->成员变量或成员函数;
4.matlab R2010b,mcc -W cpplib:libmyadd2 -T link:lib myadd2.m不再生成libmyadd2.ctf文件,需加-C选项
5.cstring
文章评论(0条评论)
登录后参与讨论