Search code examples
winapiiwebbrowser2

IWebBrowser2 Controlling visibility


I create hidden IWebBrowser2 object and it work's fine but after few seconds I want to change visibility to true and my application crashes.

pBrowser2->put_Visible(VARIANT_TRUE);

What I'm doing wrong ?


Solution

  • You're using pBrowser2 after releasing it.

    You have:

    pBrowser2->Release();  // pBrowser NOW INVALID!!!
    srand( time( NULL ) );
    //Sleep( ( std::rand() % 5000 ) + 5000 );
    if(std::rand() % 100 <= chance ){
        pBrowser2->put_Visible(VARIANT_TRUE);  // instant crash here!
    }
    

    Move the call to Release() after you're done using it, or use a COM smart pointer so you don't have to manage it yourself. Simplest fix:

    srand( time( NULL ) );
    //Sleep( ( std::rand() % 5000 ) + 5000 );
    if(std::rand() % 100 <= chance ){
        pBrowser2->put_Visible(VARIANT_TRUE);  // works
    }
    pBrowser2->Release();  // pBrowser NOW INVALID!!!