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*)&param);
	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