Multe functii Windows API cer ca parametru LPCTSTR sau LPTSTR.
Cum pasez un tip CString acestor functii?
Raspuns
- Daca se cere LPCTSTR, putem utiliza CString::GetString sau CString::opertor LPCTSTR.
- Daca se cere LPTSTR, putem apela CString::GetBuffer sau CString::GetBufferSetLength.
Code: Select all
// GlobalAddAtom gets LPCTSTR CString strAtom = _T("demo atom text"); // ... ATOM atom = ::GlobalAddAtom(strAtom.GetString());
Code: Select all
// RegisterWindowMessage gets LPCTSTR CString strMsg = _T("demo message text"); // ... UINT uMsg = ::RegisterWindowMessage((LPCTSTR)strMsg);
Code: Select all
// CreateDirectory gets LPCTSTR CString strDir = _T("demo directory"); // ... BOOL bRet = ::CreateDirectory(strDir, NULL); // Note: implicit (LPCTSTR) cast has been performed.
Code: Select all
// GetCurrentDirectory gets LPTSTR CString strCurDir; ::GetCurrentDirectory(MAX_PATH, strCurDir.GetBuffer(MAX_PATH)); strCurDir.ReleaseBuffer();
- Important: O greseala frecvent intalnita este de a face dublu cast (LPTSTR)(LPCTSTR):
Code: Select all
// GetSystemDirectory gets LPTSTR CString strSysDir; ::GetSystemDirectory((LPTSTR)(LPCTSTR)strSysDir, MAX_PATH); // ... // Bang!!! The program can crash, sooner or later.
- CString::GetString nu exista in MFC6.0 sau mai vechi. Utilizati operatorul LPCTSTR care in fond face acelasi lucru.
- In versiunile noi de MFC, CString este implementat ca teplate, in consecinta puteti gasi documentatia la CStringT si CSimpleStringT. De asemenea puteti gasi operatorul PCXSTR in loc de LPCTSTR. In orice caz, putem in continuare folosi "sintaxa veche" din exemplele date mai sus.
- CString::operator LPCTSTR (VS.6.0)
- CSimpleStringT::operator PCXSTR
- CString::GetBuffer (VS 6.0)
- CSimpleStringT::GetBuffer
<< Back to MFC index