CString and WriteFile
-
Hi I am trying to write data to the serial port using the windows api. I can write successfully by using a normal C style string with the MFC wrapper I wrote(with help from this site!) it goes something like this void CSerComPort::WriteData(char* cData,DWORD dwBufferSize) { DWORD dwBytesWritten; if(!WriteFile(m_ComPortHndl,cData,dwBufferSize,&dwBytesWritten,NULL)) ; //throw exception for wrong write else;// ASSERT(dwBytesWritten == sizeof(dwData)); } works fine when i declare something like char dwdata[]="test string"; But Ideally i would like to read data in from an edit box and this variable is of type CString, I have tried to use the member function GetBuffer which seems to be ok but when I pass GetLength as the second parameter it says I can't convert between int and unsigned long. Is there a standard way to do this, or would I be better rewriting the WriteData function? Please bear with me I am quite new to windows programming but I would like to know if there is a standard way to achieve this. Thanks Andy
-
Hi I am trying to write data to the serial port using the windows api. I can write successfully by using a normal C style string with the MFC wrapper I wrote(with help from this site!) it goes something like this void CSerComPort::WriteData(char* cData,DWORD dwBufferSize) { DWORD dwBytesWritten; if(!WriteFile(m_ComPortHndl,cData,dwBufferSize,&dwBytesWritten,NULL)) ; //throw exception for wrong write else;// ASSERT(dwBytesWritten == sizeof(dwData)); } works fine when i declare something like char dwdata[]="test string"; But Ideally i would like to read data in from an edit box and this variable is of type CString, I have tried to use the member function GetBuffer which seems to be ok but when I pass GetLength as the second parameter it says I can't convert between int and unsigned long. Is there a standard way to do this, or would I be better rewriting the WriteData function? Please bear with me I am quite new to windows programming but I would like to know if there is a standard way to achieve this. Thanks Andy
// I am asuming no UNICODE is involved. // This should work, since LPTSTR is defined as char* CString csData; GetDlgItemText(IDC_TEXTSTRING, csData); WriteData( LPCTSTR(csData),(DWORD)csData.GetLength() ); // Here are some more options:) // If using C char szBuffer[512]; int nLen = GetDlgItemText(hDlg, IDC_TEXTSTRING, szBuffer, sizeof(szBuffer)); WriteData( szBuffer,(DWORD)nLen ); // If using MFC int nLen = GetDlgItemText(IDC_TEXTSTRING, szBuffer, sizeof(szBuffer)); WriteData( szBuffer,(DWORD)nLen ); // Else If using MFCs CString CString csData; GetDlgItemText(IDC_TEXTSTRING, csData); // Then WriteData( LPCTSTR(csData),(DWORD)csData.GetLength() ); // or const char* pszData = csData.GetBuffer(csData.GetLength()); WriteData( pszData ,(DWORD)csData.GetLength() ); csData.ReleaseBuffer(); // or WriteData( csData.LockBuffer(),(DWORD)csData.GetLength() ); csData.UnlockBuffer(); Trust in the code Luke. Yea right!
-
Hi I am trying to write data to the serial port using the windows api. I can write successfully by using a normal C style string with the MFC wrapper I wrote(with help from this site!) it goes something like this void CSerComPort::WriteData(char* cData,DWORD dwBufferSize) { DWORD dwBytesWritten; if(!WriteFile(m_ComPortHndl,cData,dwBufferSize,&dwBytesWritten,NULL)) ; //throw exception for wrong write else;// ASSERT(dwBytesWritten == sizeof(dwData)); } works fine when i declare something like char dwdata[]="test string"; But Ideally i would like to read data in from an edit box and this variable is of type CString, I have tried to use the member function GetBuffer which seems to be ok but when I pass GetLength as the second parameter it says I can't convert between int and unsigned long. Is there a standard way to do this, or would I be better rewriting the WriteData function? Please bear with me I am quite new to windows programming but I would like to know if there is a standard way to achieve this. Thanks Andy
-
Conversion from CString to char* is simple. For example, CString str = "test string; then you should write WriteFile(m_ComPortHndl,(char*)(LPCTSTR) str, str.GetLength(), &dwBytesWritten,NULL) C.R.Naik
From Michael Dunn and Nishant S article in codeproject: Rule #1 of string classes Casts are bad, unless they are explicitly documented. Take a look at those articles: http://www.codeproject.com/string/CPPStringGuide1.asp[^] http://www.codeproject.com/string/CPPStringGuide2.asp[^] I think a GetBuffer(0) call would be better... Hope this helps...
-
From Michael Dunn and Nishant S article in codeproject: Rule #1 of string classes Casts are bad, unless they are explicitly documented. Take a look at those articles: http://www.codeproject.com/string/CPPStringGuide1.asp[^] http://www.codeproject.com/string/CPPStringGuide2.asp[^] I think a GetBuffer(0) call would be better... Hope this helps...