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

MultiByteToWideChar

Scheduled Pinned Locked Moved C / C++ / MFC
comhelpannouncementworkspace
9 Posts 6 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.
  • T Offline
    T Offline
    tanarnelinistit
    wrote on last edited by
    #1

    i have the folowing function that doesn't work: i get an error like: "MultiByteToWideChar' : cannot convert parameter 5 from 'char *' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast" void CMina_sView::OnListenCom1() { CSerial serial; LONG lLastError = ERROR_SUCCESS; TCHAR tszMsg[200]; lLastError = serial.Open(_T("COM5"),0,0,false); lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1); lLastError = serial.SetMask(CSerial::EEventBreak | CSerial::EEventCTS | CSerial::EEventDSR | CSerial::EEventError | CSerial::EEventRing | CSerial::EEventRLSD | CSerial::EEventRecv); lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking); // Wait for an event lLastError = serial.WaitEvent(); // Save event const CSerial::EEvent eEvent = serial.GetEventType(); // Handle ring event // Handle data receive event if (eEvent & CSerial::EEventRecv) { // Read data, until there is nothing left DWORD dwBytesRead ; char szBuffer[101]; do { // Read data from the COM-port lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); // Finalize the data, so it is a valid string szBuffer[dwBytesRead] = '\0'; // Convert the ANSI data to Unicode LPTSTR lpszBuffer = LPTSTR(_alloca((dwBytesRead+1)*sizeof(TCHAR))); MultiByteToWideChar(CP_ACP, 0, szBuffer, strlen(szBuffer), lpszBuffer, sizeof(lpszBuffer)+1); m_pSet->AddNew(); m_pSet->m_Nume=szBuffer; m_pSet->m_Serie="asfasdvce"; CTime time = CTime::GetCurrentTime(); m_pSet->m_Data=time; UpdateData(TRUE); if (m_pSet->CanUpdate()) m_pSet->Update(); m_pSet->Requery(); UpdateData(FALSE); m_pSet->Close(); m_pSet->Open(); Invalidate(); UpdateWindow(); } while (dwBytesRead == sizeof(szBuffer)-1); } // Close the port again serial.Close(); }

    J S L N 4 Replies Last reply
    0
    • T tanarnelinistit

      i have the folowing function that doesn't work: i get an error like: "MultiByteToWideChar' : cannot convert parameter 5 from 'char *' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast" void CMina_sView::OnListenCom1() { CSerial serial; LONG lLastError = ERROR_SUCCESS; TCHAR tszMsg[200]; lLastError = serial.Open(_T("COM5"),0,0,false); lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1); lLastError = serial.SetMask(CSerial::EEventBreak | CSerial::EEventCTS | CSerial::EEventDSR | CSerial::EEventError | CSerial::EEventRing | CSerial::EEventRLSD | CSerial::EEventRecv); lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking); // Wait for an event lLastError = serial.WaitEvent(); // Save event const CSerial::EEvent eEvent = serial.GetEventType(); // Handle ring event // Handle data receive event if (eEvent & CSerial::EEventRecv) { // Read data, until there is nothing left DWORD dwBytesRead ; char szBuffer[101]; do { // Read data from the COM-port lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); // Finalize the data, so it is a valid string szBuffer[dwBytesRead] = '\0'; // Convert the ANSI data to Unicode LPTSTR lpszBuffer = LPTSTR(_alloca((dwBytesRead+1)*sizeof(TCHAR))); MultiByteToWideChar(CP_ACP, 0, szBuffer, strlen(szBuffer), lpszBuffer, sizeof(lpszBuffer)+1); m_pSet->AddNew(); m_pSet->m_Nume=szBuffer; m_pSet->m_Serie="asfasdvce"; CTime time = CTime::GetCurrentTime(); m_pSet->m_Data=time; UpdateData(TRUE); if (m_pSet->CanUpdate()) m_pSet->Update(); m_pSet->Requery(); UpdateData(FALSE); m_pSet->Close(); m_pSet->Open(); Invalidate(); UpdateWindow(); } while (dwBytesRead == sizeof(szBuffer)-1); } // Close the port again serial.Close(); }

      J Offline
      J Offline
      Jetli Jerry
      wrote on last edited by
      #2

      tanarnelinistit wrote:

      // Convert the ANSI data to Unicode LPTSTR lpszBuffer = LPTSTR(_alloca((dwBytesRead+1)*sizeof(TCHAR))); MultiByteToWideChar(CP_ACP, 0, szBuffer, strlen(szBuffer), lpszBuffer, sizeof(lpszBuffer)+1);

      I think you need to use WCHAR instead of LPTSTR and for sizeof(TCHAR) use sizeof(WCHAR) hope this helps Jetli Constant Thing In World Is Change.

      1 Reply Last reply
      0
      • T tanarnelinistit

        i have the folowing function that doesn't work: i get an error like: "MultiByteToWideChar' : cannot convert parameter 5 from 'char *' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast" void CMina_sView::OnListenCom1() { CSerial serial; LONG lLastError = ERROR_SUCCESS; TCHAR tszMsg[200]; lLastError = serial.Open(_T("COM5"),0,0,false); lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1); lLastError = serial.SetMask(CSerial::EEventBreak | CSerial::EEventCTS | CSerial::EEventDSR | CSerial::EEventError | CSerial::EEventRing | CSerial::EEventRLSD | CSerial::EEventRecv); lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking); // Wait for an event lLastError = serial.WaitEvent(); // Save event const CSerial::EEvent eEvent = serial.GetEventType(); // Handle ring event // Handle data receive event if (eEvent & CSerial::EEventRecv) { // Read data, until there is nothing left DWORD dwBytesRead ; char szBuffer[101]; do { // Read data from the COM-port lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); // Finalize the data, so it is a valid string szBuffer[dwBytesRead] = '\0'; // Convert the ANSI data to Unicode LPTSTR lpszBuffer = LPTSTR(_alloca((dwBytesRead+1)*sizeof(TCHAR))); MultiByteToWideChar(CP_ACP, 0, szBuffer, strlen(szBuffer), lpszBuffer, sizeof(lpszBuffer)+1); m_pSet->AddNew(); m_pSet->m_Nume=szBuffer; m_pSet->m_Serie="asfasdvce"; CTime time = CTime::GetCurrentTime(); m_pSet->m_Data=time; UpdateData(TRUE); if (m_pSet->CanUpdate()) m_pSet->Update(); m_pSet->Requery(); UpdateData(FALSE); m_pSet->Close(); m_pSet->Open(); Invalidate(); UpdateWindow(); } while (dwBytesRead == sizeof(szBuffer)-1); } // Close the port again serial.Close(); }

        S Offline
        S Offline
        Steve S
        wrote on last edited by
        #3

        tanarnelinistit wrote:

        LPTSTR lpszBuffer = LPTSTR(_alloca((dwBytesRead+1)*sizeof(TCHAR))); MultiByteToWideChar(CP_ACP, 0, szBuffer, strlen(szBuffer), lpszBuffer, sizeof(lpszBuffer)+1);

        Shouldn't that be: LPWSTR lpszBuffer = reinterpret_cast<LPWSTR>(_alloca(dwBytesRead+1)*sizeof(WCHAR)); Personally, I wouldn't use _alloca, I'd use something like WCHAR wszBuffer[102]; // If you really must use magic numbers... Steve S Developer for hire

        1 Reply Last reply
        0
        • T tanarnelinistit

          i have the folowing function that doesn't work: i get an error like: "MultiByteToWideChar' : cannot convert parameter 5 from 'char *' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast" void CMina_sView::OnListenCom1() { CSerial serial; LONG lLastError = ERROR_SUCCESS; TCHAR tszMsg[200]; lLastError = serial.Open(_T("COM5"),0,0,false); lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1); lLastError = serial.SetMask(CSerial::EEventBreak | CSerial::EEventCTS | CSerial::EEventDSR | CSerial::EEventError | CSerial::EEventRing | CSerial::EEventRLSD | CSerial::EEventRecv); lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking); // Wait for an event lLastError = serial.WaitEvent(); // Save event const CSerial::EEvent eEvent = serial.GetEventType(); // Handle ring event // Handle data receive event if (eEvent & CSerial::EEventRecv) { // Read data, until there is nothing left DWORD dwBytesRead ; char szBuffer[101]; do { // Read data from the COM-port lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); // Finalize the data, so it is a valid string szBuffer[dwBytesRead] = '\0'; // Convert the ANSI data to Unicode LPTSTR lpszBuffer = LPTSTR(_alloca((dwBytesRead+1)*sizeof(TCHAR))); MultiByteToWideChar(CP_ACP, 0, szBuffer, strlen(szBuffer), lpszBuffer, sizeof(lpszBuffer)+1); m_pSet->AddNew(); m_pSet->m_Nume=szBuffer; m_pSet->m_Serie="asfasdvce"; CTime time = CTime::GetCurrentTime(); m_pSet->m_Data=time; UpdateData(TRUE); if (m_pSet->CanUpdate()) m_pSet->Update(); m_pSet->Requery(); UpdateData(FALSE); m_pSet->Close(); m_pSet->Open(); Invalidate(); UpdateWindow(); } while (dwBytesRead == sizeof(szBuffer)-1); } // Close the port again serial.Close(); }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Hello, replace your code: // Convert the ANSI data to Unicode LPTSTR lpszBuffer = LPTSTR(_alloca((dwBytesRead+1)*sizeof(TCHAR))); with this code: // i use similar like this: TCHAR* unicode_string; unicode_string = (TCHAR*)calloc(dwBytesRead+1, sizeof(TCHAR)); maybe helps? or try instead LPTSTR to use LPWSTR??? regards break;

          1 Reply Last reply
          0
          • T tanarnelinistit

            i have the folowing function that doesn't work: i get an error like: "MultiByteToWideChar' : cannot convert parameter 5 from 'char *' to 'unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast" void CMina_sView::OnListenCom1() { CSerial serial; LONG lLastError = ERROR_SUCCESS; TCHAR tszMsg[200]; lLastError = serial.Open(_T("COM5"),0,0,false); lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1); lLastError = serial.SetMask(CSerial::EEventBreak | CSerial::EEventCTS | CSerial::EEventDSR | CSerial::EEventError | CSerial::EEventRing | CSerial::EEventRLSD | CSerial::EEventRecv); lLastError = serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking); // Wait for an event lLastError = serial.WaitEvent(); // Save event const CSerial::EEvent eEvent = serial.GetEventType(); // Handle ring event // Handle data receive event if (eEvent & CSerial::EEventRecv) { // Read data, until there is nothing left DWORD dwBytesRead ; char szBuffer[101]; do { // Read data from the COM-port lLastError = serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); // Finalize the data, so it is a valid string szBuffer[dwBytesRead] = '\0'; // Convert the ANSI data to Unicode LPTSTR lpszBuffer = LPTSTR(_alloca((dwBytesRead+1)*sizeof(TCHAR))); MultiByteToWideChar(CP_ACP, 0, szBuffer, strlen(szBuffer), lpszBuffer, sizeof(lpszBuffer)+1); m_pSet->AddNew(); m_pSet->m_Nume=szBuffer; m_pSet->m_Serie="asfasdvce"; CTime time = CTime::GetCurrentTime(); m_pSet->m_Data=time; UpdateData(TRUE); if (m_pSet->CanUpdate()) m_pSet->Update(); m_pSet->Requery(); UpdateData(FALSE); m_pSet->Close(); m_pSet->Open(); Invalidate(); UpdateWindow(); } while (dwBytesRead == sizeof(szBuffer)-1); } // Close the port again serial.Close(); }

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            maybe u haven't define _UNICODE. Hence LPTSTR lpszBuffer is equavalent to char* lpszBuffer. so Either define _UNICODE or change LPTSTR lpszBuffer to wchar_t* lpszBuffer; nave

            T 1 Reply Last reply
            0
            • N Naveen

              maybe u haven't define _UNICODE. Hence LPTSTR lpszBuffer is equavalent to char* lpszBuffer. so Either define _UNICODE or change LPTSTR lpszBuffer to wchar_t* lpszBuffer; nave

              T Offline
              T Offline
              tanarnelinistit
              wrote on last edited by
              #6

              Thanks for the tips. Now i don't get the error any more, but still my string hasn't got the correct value. I should get something like: "FFFFAAAAA121312C" and I get "F", only F.

              D N 2 Replies Last reply
              0
              • T tanarnelinistit

                Thanks for the tips. Now i don't get the error any more, but still my string hasn't got the correct value. I should get something like: "FFFFAAAAA121312C" and I get "F", only F.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                tanarnelinistit wrote:

                ...my string hasn't got the correct value. I should get something like: "FFFFAAAAA121312C" and I get "F", only F.

                How are you verifying this?


                "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                "Judge not by the eye but by the heart." - Native American Proverb

                T 1 Reply Last reply
                0
                • D David Crow

                  tanarnelinistit wrote:

                  ...my string hasn't got the correct value. I should get something like: "FFFFAAAAA121312C" and I get "F", only F.

                  How are you verifying this?


                  "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                  "Judge not by the eye but by the heart." - Native American Proverb

                  T Offline
                  T Offline
                  tanarnelinistit
                  wrote on last edited by
                  #8

                  I have a program that came with the tool that connects to the COM, and I know the value I should get because that's what that prg gives me.

                  1 Reply Last reply
                  0
                  • T tanarnelinistit

                    Thanks for the tips. Now i don't get the error any more, but still my string hasn't got the correct value. I should get something like: "FFFFAAAAA121312C" and I get "F", only F.

                    N Offline
                    N Offline
                    Naveen
                    wrote on last edited by
                    #9

                    i think this is because the IDE dosen't show the unicode strings... change the IDE settings as below.. take tool->options menu. now take the debug tab in the Options dialog displayed. U can find a check box saying "Display unicode strings". Tick that check box. now run the application with a break point set in the next line of "MultiByteToWideChar(..)" function. Check the value in the variable when breakpoint reaches there:) nave

                    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