Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString and WriteFile

CString and WriteFile

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andyg 101
    wrote on last edited by
    #1

    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

    J C 2 Replies Last reply
    0
    • A andyg 101

      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

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      // 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!

      1 Reply Last reply
      0
      • A andyg 101

        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

        C Offline
        C Offline
        Chintan
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • C Chintan

          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

          J Offline
          J Offline
          Joan M
          wrote on last edited by
          #4

          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...

          https://www.robotecnik.com freelance robots, PLC and CNC programmer.

          A 1 Reply Last reply
          0
          • J Joan M

            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...

            A Offline
            A Offline
            andyg 101
            wrote on last edited by
            #5

            Thanks for the help guys, I appreciate it Andy

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups