-
June 7th, 2011, 03:29 PM
#1
Strange behaviour with MultiByteToWideChar
Hello,
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
Code:
HGLOBAL dStreamMem = NULL;
BYTE* dBuffer = NULL;
UINT dBufferLen = 0;
IStream* dStream = NULL;
BYTE* content = NULL;
CHAR additionalHTML[2024];
CHAR* pAdditionalHTML = NULL;
UINT additionalHTMLLen = 0
additionalHTML[0] = 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)
{
GlobalFree(dStreamMem);
}
RELEASE(dStream);
dStreamMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,size - (content - dBuffer) + additionalHTMLLen );
BYTE* dStreamMemBuf = (BYTE*)GlobalLock(dStreamMem);
memcpy(dStreamMemBuf,additionalHTML,additionalHTMLLen);
dStreamMemBuf += additionalHTMLLen;
memcpy(dStreamMemBuf,content,size - (content - dBuffer));
GlobalUnlock(dStreamMem);
if (FAILED(CreateStreamOnHGlobal(dStreamMem,false,&dStream)))
{
LOGTAG(Error_Download_CreateStreamOnHGlobal_Failed);
}
...
PostMessage(hwndMain,WM_DOWNLOADFINISHED,ret,0);
After the download is completed an event is triggered which causes an other part of the application to run
Code:
BOOL ShowPage()
{
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);
if(FAILED(hr))
{
LOG('E', "Read Steam failed with %x\r\n", hr);
}
else
{
wsprintf(bufferFilename,TEXT("pcDocument2_%d.html"),pageCount);
hFile = CreateFile(bufferFilename,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,NULL,NULL);
WriteFile(hFile,pcDocument2,strlen(pcDocument2),&written,NULL);
CloseHandle(hFile);
}
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);
doc->close();
SafeArrayDestroy(sfArray);
SysFreeString(bstr);
}
And here I've got an problem. The call of MultiByteToWideChar is giving wrong result randomly.
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?
Thanks
Akademos
-
June 7th, 2011, 04:21 PM
#2
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)
MultiByteToWideChar does not null-terminate an output string if the input string length is explicitly specified without a terminating null character
Isn't it better to let MultiByteToWideChar help you by calculating the needed buffer length?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|