MultiByteToWideChar
-
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(); }
-
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(); }
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.
-
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(); }
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
-
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(); }
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; -
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(); }
-
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
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.
-
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.
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
-
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
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.
-
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.
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