views:

307

answers:

1

Hello,

I need help on global variable usage in an ActiveX(ATL) project. Basically the ActiveX component function is to navigae to a specified URL in composite control(webbrowser embedded). The URL string is initialize in the beginning and saved in a global variable. here is my source code file of ActiveX project.

(Do not be concerned about the grammar, I just want to show the code flow).

    MyComponentInit.cpp


    // MyComponentInit.cpp : Implementation of MyComponentInit.cpp

    #include "stdafx.h"
    #include <ios> 

    char szURL[1024] = "\0";   // global variable holding URL string

    STDMETHODIMP CMyComponentInit::InitPlugin(BSTR url)
    {
       // convert BSTR to string
         ...... 

           memcpy(szURL, szUrl, len);
    }




    MyComponentCtrl.cpp 

    // MyComponentCtrl.cpp : Implementation of  MyComponentCtrl.cpp

    #include "stdafx.h"

    extern char  szURL[1024]

    LRESULT CMyComponentCtrl::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
       ......

        m_spWebbrowser->Navigate(szURL, &v, &v, &v, &v);
     }

The use of mycomponent ActiveX is in a MFC SDI(Single Doc/View) test application. The flow is like:

  1. In App Initinstance, I called activeX InitPlugin to initialize the URL string.
  2. In a Information dialog window, insert MyComponentCtrl ActiveX, the Information dialog is brought up by clicking Mainframe Information menu item. The goal is when Information dialog window pop up, it can always go to the specified url.

After my MFC test app starts, I click Information dialog menu, the Information dialog window can be up and navigate to the url correctly.

But the problem is if I close the Information dialog, wait fro a while (around 2~3 minutes), I tried to bring up Information dialog again, it could not navigate to the URL. I debug it and found the global variable szURL in MyComponenet project lost its value.

Can you please tell me what should I do to resolve this problem?

Thanks a lot in advance!

Bionicoder

+1  A: 

It looks like the application and COM might be releasing and reloading your DLL between the initialization call and the control being displayed. This could happen if the main app has no active instances of any of the DLL's COM components and subsequently calls CoFreeUnusedLibraries().

You can test that by putting a breakpoint or trace from DllMain for fdwReason == DLL_PROCESS_DETACH. If this is the problem, the breakpoint will trigger after the initialization call but before OnInitDialog().

If this is the problem, you can solve it by initializing the control instance with the URL directly, using instance state instead of using a global variable.

You can alternately keep a reference to the initialization interface for the lifetime of the application.

dmercredi
Hi dmercredi, Thanks! You are right. The fdwReason == DLL_PROCESS_DETACH was triggered after some idle time. I need to use global var coz total 5 composite ctrls use this. So I am looking for keeping reference to initialization for life time. What is the best way to do this? I am new to ActiveX.
bionicoder
I'd declare the smart pointer to the initialization object in your MFC application class instead of declaring it as a local in InitInstance(). Since the application object's scope matches the app's lifetime, this'll keep the initialized DLL in memory so it won't get uninitialized.
dmercredi