CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Mar 2000
    Location
    Germany, Franken
    Posts
    257

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

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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?
    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
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

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
  •  





Click Here to Expand Forum to Full Width

Featured