Strange behaviour with MultiByteToWideChar
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: Strange behaviour with MultiByteToWideChar

  1. #1
    Join Date
    Mar 2000
    Germany, Franken

    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
    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)
    dStreamMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,size - (content - dBuffer)  + additionalHTMLLen );
    BYTE* dStreamMemBuf = (BYTE*)GlobalLock(dStreamMem);
    dStreamMemBuf += additionalHTMLLen;
    memcpy(dStreamMemBuf,content,size - (content - dBuffer));
    if (FAILED(CreateStreamOnHGlobal(dStreamMem,false,&dStream)))
    After the download is completed an event is triggered which causes an other part of the application to run
    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);
    		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*)&param);
    	param->vt = VT_BSTR;
    	param->bstrVal = bstr;
    	hresult = SafeArrayUnaccessData(sfArray);
    	hresult = doc->write(sfArray);
    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?



  2. #2
    Join Date
    Oct 2006

    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 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?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    and before posting

    Refresh your memory on formatting tags here

    Get your free MS compiler here

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This a survey!

On-Demand Webinars (sponsored)