Click here to Skip to main content
14,924,665 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am porting an app originally written in c, to c++, and I am having difficulty converting a CString to a pointer to a character array. Please pardon all the commented out code, but I have been trying anything and everything to get this to work.

In the original app I used code from this website, with success. I am confused about if I need to use overlapped or non-overlapped. The overlapped code came from the msdn website.

When I try to use the overlapped code, I receive my data back with zero interspersed every other byte in the reply, which I expect since I am using a CString to contain the message.

I am using a loop-back connector to test the communication.

Thanks in advance.

C++
bool ELMCommWrite(CString strDataToWrite, HANDLE hWnd)
{
	OVERLAPPED	osWrite = {0};
	DWORD		dwWritten, dwRes, dwToWrite;
	BOOL		fRes;
	char		* chDataToWrite[] = {"1234\0"};

	LPCTSTR		pstrDataToWrite = strDataToWrite;

	dwToWrite = 5;//strWriteData.StringLength();


	//osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
	//if (osWrite.hEvent == NULL)
	//	return FALSE;

	//if (!WriteFile(hCom, pstrDataToWrite, lstrlen(pstrDataToWrite) * sizeof(TCHAR), &dwWritten, &osWrite))
	WriteFile(hCom, pstrDataToWrite, lstrlen(pstrDataToWrite) * sizeof(TCHAR), &dwWritten, NULL);
	/*{
		if (GetLastError() != ERROR_IO_PENDING)
		{
			err = GetLastError();
			fRes = FALSE;
		}
		else
			dwRes = WaitForSingleObject(osWrite.hEvent , INFINITE);
		switch(dwRes)
		{
		case WAIT_OBJECT_0:
			if (!GetOverlappedResult(hCom, &osWrite, &dwWritten, FALSE))
				fRes = FALSE;
			else
				fRes = TRUE;
			break;
		default:
			fRes = FALSE;
			break;
		}
	}
	else
		fRes = TRUE;
	//CString strReply = ELMCommRead(hCom);
	ELMCommRead(hCom);
	//CloseHandle(osWrite.hEvent);
	return fRes;
	*/
	return 0;
}
Posted
Comments
CPallini 8-Apr-13 12:59pm
   
And what is your problem? (Possibly you don't like the 'zero interparsed', do you?)?
Richard MacCutchan 8-Apr-13 13:14pm
   
It sounds as though your CString is Unicode rather than ASCII, so you need to convert it first using one of the conversion routines, or use the CStringT Class.

1 solution

If your strings have a '\0' as every second character, then it seems that you created the C++ application as Unicode. If you are converting source from C to C++ this seems an unlikely choice. Go to your project settings and deselect Unicode, which is the default project type. You didn't say what version of the compiler you are using (different versions have different details). On VS2010, right click the project, select "Properties", go to "Configuration Properties", "General", for "Character Set", choose "Use Multi-Byte Character Set".
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900