June 7th, 2011, 04:29 PM
Strange behaviour with MultiByteToWideChar
we've got an application which runs on Windows CE 6. Basically hte application is doing HTTP-Request and displaying the result.
When the response of a request is received it will be but in a stream
After the download is completed an event is triggered which causes an other part of the application to run
HGLOBAL dStreamMem = NULL;
BYTE* dBuffer = NULL;
UINT dBufferLen = 0;
IStream* dStream = NULL;
BYTE* content = NULL;
CHAR* pAdditionalHTML = NULL;
UINT additionalHTMLLen = 0
additionalHTML = 0;
pAdditionalHTML = additionalHTML;
sprintf(pAdditionalHTML,"<html><base href=\"http://%s:%d%s\" />",reqInfo->host,reqInfo->port,reqInfo->path);
additionalHTMLLen = strlen(additionalHTML);
//Free old buffers
if (dStreamMem != NULL)
dStreamMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,size - (content - dBuffer) + additionalHTMLLen );
BYTE* dStreamMemBuf = (BYTE*)GlobalLock(dStreamMem);
dStreamMemBuf += additionalHTMLLen;
memcpy(dStreamMemBuf,content,size - (content - dBuffer));
And here I've got an problem. The call of MultiByteToWideChar is giving wrong result randomly.
char* pcDocument2 = NULL;
uint uiDocumentSize = strlen((LPCSTR)documentData) + strlen(addHTML);
pcDocument2 = (char *)malloc(uiDocumentSize);
pwcDocument = (wchar_t *)malloc(sizeof(wchar_t) * uiDocumentSize);
HRESULT hr = dStream->Read(pcDocument2, uiDocumentSize, &bytesRead);
LOG('E', "Read Steam failed with %x\r\n", hr);
hFile = CreateFile(bufferFilename,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,NULL,NULL);
MultiByteToWideChar(CP_UTF8, 0, pcDocument2, uiDocumentSize,pwcDocument,uiDocumentSize);
bstr = SysAllocString(pwcDocument);
sfArray = SafeArrayCreateVector(VT_VARIANT,0,1);
hresult = SafeArrayAccessData(sfArray,(LPVOID*)¶m);
param->vt = VT_BSTR;
param->bstrVal = bstr;
hresult = SafeArrayUnaccessData(sfArray);
hresult = doc->write(sfArray);
The first call always works. After that it is randomly. It could work ten times but then the result in pwcDocument (a wchar_t *) is containing parts which seems to be a leftover from the previous call. I suspect that something is wrong with the length calculation and so the function call MultiByteToWideChar is accessing memory areas which are a random leftover. But I'm totally unsure in this.
Does anyone here have an suggestion or an idea how to fix this?
June 7th, 2011, 05:21 PM
Re: Strange behaviour with MultiByteToWideChar
If it's a buffer overrun that cause the issue it should be easily verified by allocating a lot more memory, fill the buffer with a known pattern and then run it in the debugger.
You are reading max uiDocumentSize bytes but the resulting number of bytes are bytesRead. Yet you use uiDocumentSize in the MultiByteToWideChar call.
Have you read this http://msdn.microsoft.com/en-us/libr...72(VS.85).aspx carefully? In particular this might be a clue (from MSDN)
Isn't it better to let MultiByteToWideChar help you by calculating the needed buffer length?
MultiByteToWideChar does not null-terminate an output string if the input string length is explicitly specified without a terminating null character
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.