Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3425184
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:33:49+00:00 2026-05-18T06:33:49+00:00

Here is a concrete example: I create a IWeBrowser2 interface by calling wb.CoCreateInstance(CLSID_InternetExplorer, 0,

  • 0

Here is a concrete example:

I create a IWeBrowser2 interface by calling wb.CoCreateInstance(CLSID_InternetExplorer, 0, CLSCTX_SERVER);. This gives me a marshaled interface from my process into whichever of the running iexplore.exe processes happens to contain this browser tab in my thread A.

Now I use the IGlobalInterfaceTable to get a cookie for this interface, pass it to my thread B and request the marshaled interface from there.

Question: Do I get a proxy to the proxy in my thread A or directly to the instance in the IE process?

It seems sensible to me that I will get a direct proxy to the instance with its own reference to it,
however:

If I end my thread A, the cookie I created there becomes invalid and I can’t retrieve (and close) the interface pointers to the web browsers I created any more. This does not make sense unless there is a thunk in that thread that is destroyed when the thread quits.

Edit: Oh, both threads are STA.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:33:50+00:00Added an answer on May 18, 2026 at 6:33 am

    I finally had some time to figure out what is happening, so I wrote a short test to see what is going on.

    // MarshalTest.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    enum { WM_THEREYOUGO = WM_USER+1, WM_THANKYOU, WM_YOURWELCOME };
    
    DWORD WINAPI TheOtherThread(DWORD * main_thread_id)
    {
        MSG msg = { 0 };
        HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
        assert(SUCCEEDED(hr));
    
        {
            // create web browser
            CComPtr<IWebBrowser2> wb;
            hr = wb.CoCreateInstance(CLSID_InternetExplorer, 0, CLSCTX_SERVER);
            assert(SUCCEEDED(hr) && wb);
    
            // navigate
            hr = wb->Navigate2(&CComVariant(_T("stackoverflow.com")), &CComVariant(0), &CComVariant(_T("")), &CComVariant(), &CComVariant());
            assert(SUCCEEDED(hr));
            hr = wb->put_Visible(VARIANT_TRUE);
            assert(SUCCEEDED(hr));
    
            // Marshal
            DWORD the_cookie = 0;
            {
                CComPtr<IGlobalInterfaceTable> com_broker;
                hr = com_broker.CoCreateInstance(CLSID_StdGlobalInterfaceTable);
                assert(SUCCEEDED(hr));
                hr = com_broker->RegisterInterfaceInGlobal(wb, __uuidof(IWebBrowser2), &the_cookie);
            }
    
            // notify main thread
            PostThreadMessage(*main_thread_id, WM_THEREYOUGO, the_cookie, NULL);
    
            // message loop
            while(GetMessage(&msg, 0, 0, 0)) {
                if(msg.hwnd == NULL) {
                    // thread message
                    switch(msg.message) {
                        case WM_THANKYOU:
                            PostQuitMessage(0);
                            break;
                    }
                } else {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
        }
    
        CoUninitialize();
    
        PostThreadMessage(*main_thread_id, WM_YOURWELCOME, 0, NULL);
        return msg.wParam;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        MSG msg = {0};
        DWORD main_thread_id = GetCurrentThreadId();
    
        HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
        assert(SUCCEEDED(hr));
        {
            DWORD ThreadId = 0;
            HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TheOtherThread, &main_thread_id, 0, &ThreadId);
    
            DWORD the_cookie = 0;
    
            CComPtr<IWebBrowser2> wb, wb2;
    
            while(GetMessage(&msg, 0, 0, 0)) {
                if(msg.hwnd == NULL) {
                    // thread message
                    switch(msg.message) {
                        case WM_THEREYOUGO:
                            // we got the cookie.
                            the_cookie = msg.wParam;
    
                            // get the browser. This should work.
                            {
                                CComPtr<IGlobalInterfaceTable> com_broker;
                                hr = com_broker.CoCreateInstance(CLSID_StdGlobalInterfaceTable);
                                assert(SUCCEEDED(hr));
                                hr = com_broker->GetInterfaceFromGlobal(the_cookie, __uuidof(IWebBrowser2), (void**)&wb);
                                assert(SUCCEEDED(hr) && wb);
                            }
    
                            // do something with it.
                            hr = wb->put_FullScreen(VARIANT_TRUE);
                            assert(SUCCEEDED(hr));
    
                            // signal the other thread.
                            PostThreadMessage(ThreadId, WM_THANKYOU, 0, NULL);
                            break;
    
                        case WM_YOURWELCOME:
                            // the other thread has ended.
                            PostQuitMessage(0);
                            break;
                    }
                } else {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
    
            // the other thread has ended. Try getting the interface again.
            {
                CComPtr<IGlobalInterfaceTable> com_broker;
                hr = com_broker.CoCreateInstance(CLSID_StdGlobalInterfaceTable);
                assert(SUCCEEDED(hr));
                hr = com_broker->GetInterfaceFromGlobal(the_cookie, __uuidof(IWebBrowser2), (void**)&wb2);
                //assert(SUCCEEDED(hr) && wb2); // this fails, hr == E_INVALIDARG.
    
                // clean up, will not be executed.
                if(SUCCEEDED(hr)) {
                    hr = com_broker->RevokeInterfaceFromGlobal(the_cookie);
                }
            }
    
            // try using it
            if(wb2) {
                hr = wb2->put_FullScreen(VARIANT_FALSE);
                assert(SUCCEEDED(hr));
            } else if(wb) {
                // this succeeds
                hr = wb->put_FullScreen(VARIANT_FALSE);
                assert(SUCCEEDED(hr));
            }
    
            CloseHandle(hThread);
        }
    
        CoUninitialize();
        return msg.wParam;
    }
    

    The bottom line is this:

    • Ending the thread that registered the interface invalidates the cookie.
    • The already marshaled interface stays valid. (In this case, that is.)

    This means that I get a proxy to the IE process instead of to the other thread’s object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
EDIT: I will leave this here as an example. Read the comments for more
Here is a concrete example: Wordpress stores user information(meta) in a table called wp_usermeta
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
This is a generalized example of what I am up against. I have created
This question was asked already here , but rather than answering the specific question,
Example 1: <asp:Panel Visible=false runat=server> <asp:TextBox ID=textbox runat=server /> </asp:Panel> Here, textbox.Visible returns false
So this is probably a fairly easy question to answer but here goes anyway.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.