请帮忙看看MSDN的例子中CreateStreamOnHGlobal出异常。
出差消息:
Windows has triggered a breakpoint in smart.exe.
This may be due to a corruption of the heap, which indicates a bug in Mefone.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Mefone.exe has focus.
The output window may have more diagnostic information.    
    HRESULT hr;
    IStream* pStream = NULL;
    HGLOBAL hHTMLText;
    static TCHAR szHTMLText[] = _T("<html><h1>á÷2aê?</h1><p>±?HTML?úèYò??-′óá÷?D?ó???£</html>");    //  TODO: °2è?μ??D?? szHTMLTextμ?3¤?è£?μ¥??ê?TCHAR.
    //hHTMLText = GlobalAlloc( GPTR, cchLength+1 );
    size_t cchMax = 256;
    hHTMLText = GlobalAlloc( GMEM_MOVEABLE|GMEM_DISCARDABLE, cchMax*2+1 );
    memset( hHTMLText, 0, cchMax*2);    if ( hHTMLText )
    {        StringCchCopy((TCHAR*)hHTMLText, cchMax, szHTMLText);        //  TODO: ?ú?aà??óè?′í?ó′|àí′ú???£
        //hr = CreateStreamOnHGlobal( hHTMLText, TRUE, &pStream );
        hr = CreateStreamOnHGlobal( hHTMLText, FALSE, &pStream );
        if ( SUCCEEDED(hr) )
        {
            // μ÷ó??¨?úoˉêyè?í????ˉàà?÷?ó??á÷?£
            LoadFromStream( pStream  );
            pStream->Release();
        }
        GlobalFree( hHTMLText );
    }MSDN 的原来代码, 编译不过
=============void myObject::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
    HRESULT hr;
    IUnknown* pUnkBrowser = NULL;
    IUnknown* pUnkDisp = NULL;
    IStream* pStream = NULL;
    HGLOBAL hHTMLText;
    static TCHAR szHTMLText[] = "<html><h1>Stream Test</h1><p>This HTML content is/
       being loaded from a stream.</html>";    // Is this the DocumentComplete event for the top frame window?
    // Check COM identity: compare IUnknown interface pointers.
    hr = m_pBrowser->QueryInterface( IID_IUnknown,  (void**)&pUnkBrowser );
    if ( SUCCEEDED(hr) )
    {
        hr = pDisp->QueryInterface( IID_IUnknown,  (void**)&pUnkDisp );
        if ( SUCCEEDED(hr) )
        {
            if ( pUnkBrowser == pUnkDisp )
            {   // This is the DocumentComplete event for the top 
                //   frame - page is loaded!
                // Create a stream containing the HTML.
                // Alternatively, this stream may have been passed to us.

                size_t = cchLength;
                //  TODO: Safely determine the length of szHTMLText in TCHAR.
                hHTMLText = GlobalAlloc( GPTR, cchLength+1 );

                if ( hHTMLText )
                {
                    size_t cchMax = 256;
                    StringCchCopy((TCHAR*)hHTMLText, cchMax + 1, szHTMLText);
                    //  TODO: Add error handling code here.                    hr = CreateStreamOnHGlobal( hHTMLText, TRUE, &pStream );
                    if ( SUCCEEDED(hr) )
                    {
                       // Call the helper function to load the browser from the stream.
                       LoadWebBrowserFromStream( m_pBrowser, pStream  );
                       pStream->Release();
                    }
                    GlobalFree( hHTMLText );
                }
            }
            pUnkDisp->Release();
        }
        pUnkBrowser->Release();
    }
}代码出处:
http://msdn.microsoft.com/en-us/library/aa752047(VS.85).aspxLoading HTML content from a Stream

解决方案 »

  1.   

    首先人家要求必须不允许discardable,你还显式让它discardable
    看看MSDN
    hGlobal 
    [in] Memory handle allocated by the GlobalAlloc function. 
    The handle must be allocated as movable and nondiscardable. If the handle is to be shared between processes, it must also be allocated as shared. 
      

  2.   

    而且你接口还没用完就GLobalFree调,那么接口的内存怎么办?
    你应该把delete on release设置为TRUE,自己不要去释放内存,除非创建stream失败才GlobalFree
      

  3.   


    大侠,看错文档了。
    改了还是不行。但是恢复成hHTMLText = GlobalAlloc( GPTR, cchMax*2+1 );就变成pPersistStreamInit->InitNew出错。我在看看。void   CSmartView::LoadFromStream( IStream*   pStream)   
    {   
        HRESULT   hr;   
        IDispatch*   pHtmlDoc   =   NULL;   
        IPersistStreamInit*   pPersistStreamInit   =   NULL;       //   Retrieve   the   document   object.   
        //LPDISPATCH CHtmlView::GetHtmlDocument( ) const;
        pHtmlDoc   =   GetHtmlDocument( );   
        if   (   NULL!=pHtmlDoc  )   
        {   
            //   Query   for   IPersistStreamInit.   
            hr   =   pHtmlDoc->QueryInterface(   IID_IPersistStreamInit,     (void**)&pPersistStreamInit   );   
            if   (   SUCCEEDED(hr)   )   
            {   
                //   Initialize   the   document.   
                hr   =   pPersistStreamInit->InitNew();   
                if   (   SUCCEEDED(hr)   )   
                {   
                    //   Load   the   contents   of   the   stream.   
                    hr   =   pPersistStreamInit->Load(   pStream   );   
                }   
                pPersistStreamInit->Release();   
            }   
        }   
    }