Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Unloading and reloading an Active X control

Posted on 1998-06-29
Medium Priority
Last Modified: 2013-11-25
We writing an MFC dialog (Formview) app. (VC++ 5.0). Its using an Active X email control from a company called Mabry Software to automatically read and respond to email. It needs to be very reliable. Occasionally if the mailserver drops off, this block gets in a confused internal state. We can detect when we are in this state, but
unfortunately the block has no method to allow it to be reset. The block developer says we need to "unload and
reload" the block to release its memory and resources and then restart it in a fresh state.
This is apparently easy in VB, but I have no idea how to do it in this dialog MFC app.

How do you do this in MFC? Does it require a different way of putting the block in the app than plopping
it down with the resource editor?
Question by:titchener
  • 5
  • 4
LVL 11

Expert Comment

ID: 1318508
What is a block?  What does "this block" mean?  Are you referring to the OLE control?

B ekiM

Author Comment

ID: 1318509
Sorry, when I say block, I always mean "Active X control", as I mentioned in the first
part of the question.
LVL 11

Expert Comment

ID: 1318510
Actually, you don't mention that you mean "OLE Control" when you say "block" anywhere in your question.

OLE Controls are COM objects; they don't get loaded and unloaded explicitly by a program. Instead, a client of the object creates the object. The code serving the object will load if it isn't already loaded, and will begin serving the object. When the client of the object indicates it is done with the object, it will release the object. The code serving the object may be immediately unloaded by OLE, but may be stick around in memory for some time before it's actually removed. If the code is servicing another instance of the object, it will remain in memory and not be unloaded until no more instances of the object exist.

Given those facts, it's hard to understand exactly what you mean by "load and unload". What VB code have you written in order to perform this operation?

Also, there are many details missing from your question: exactly how are you using the control in MFC? Are you calling CreateControl() on the control directly?  Are you calling Create() on a wrapper class for the control? Have you added the control to a dialog box? Does the control have a UI?

B ekiM

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

sergem earned 400 total points
ID: 1318511
Declare the variable with your mail contol as pointer

And create it by new method. when you need to reset it, just call delete and create it againe.

CMailObject *m_Mail;

void ResetState()
     delete m_Mail;
  m_Mail = new CMailObject()
Initialize your mail object here



LVL 11

Expert Comment

ID: 1318512
Unfortunately, that won't work if the control needs data persisted in the resource template.  Nor will it work if there's more than one instance of the control in the application.

B ekiM

Author Comment

ID: 1318513
This sounds promising, and we can live with the restrictions pointed out by mikeblas above. However, I'm not quite sure how to implement what you recommended, since
MFC and the AppWizard has taken care of all of the setup and communications to this
Active X control. Here's what I tried, but didn't work.

1) I changed the variable that was originally added using the AppWizard to be a pointer,
  and initialized it to NULL in the init code for the dialog that contains it:

// Dialog Data
      enum { IDD = IDD_MABRY_DIALOG };
      CMail      *m_mail_ctl;

2) I updated all the references to it in any of the files in the project to handle the
extra indirection.

3) I tried creating the object as shown below, but its probably not right. How
   do you do this for an Active Control. Where and how is MFC doing it for me
   in the "regular" case?

      /* If m_mail_ctl has been allocated already, delete it */
            delete m_mail_ctl;
      m_mail_ctl = new CMail();
             /* Using CreateObject was just a wild guess, but doesn't work. */
            /* The first time you try to use any method with the control, you get a message
            * Debug Assertion Failed in winocc.cpp


Author Comment

ID: 1318514
In response to an earlier question, we are adding this control to a dialog.
It looks like creating it programmatically is what we need to do, but as I mentioned
in an earlier comment, we're not quite sure how to do that. Any examples anywhere?

Here's what the control designer says has to be done in VB to reset the control-

At the present time, the only way to completely reset both the
control and socket is to unload it from memory. There is an item
on the Feature Request list to have a method that does this, but it
has not been implemented yet. In the meantime, this can be done
quite easily in VB by placing the control on a separate hidden form
and change all references to the control to include the Form, such as
Form2.mMail1.Host ="mail.foo.com". Then, after a winsock error or
after each Disconnect, Unload Form2 and in the Form_Unload event

    Set Form2 = Nothing

This instruct Windows to release all resources associated with the
form thus destroying both the control and socket, so each time the
form is reloaded, the control and socket are "fresh", just like the
first time. I'm not sure what the syntax would be in VC++,  but I
assume you can load/unload forms from memory so the idea would be the

Author Comment

ID: 1318515
To answer some clarification questions more specifically:
We added the block to the main dialog of an MFX "FormView" (dialog based) application.
We're not calling CreateControl or Create, I'm assumming one way or another
MFC/AppWizard set that up for us when we added the control and a control variable
for it. Doing the create programmatically would be fine if it fixes the problem for us,
but we need an example of how to do that.
The control has no UI.

Author Comment

ID: 1318516
Ok, here's how we finally got his to work. We used the AppWizard to put the control into a separate dialog, we create that as a modeless dialog. If the control needs resetting,
we destroy the dialog and open a new copy of it.

This was easier than the proposed answer above since we didn't have to wade through
how to create the control programatically, which apparently uses a CreateControl
but with lots of arguments and probably some black majic that the approach above
LVL 11

Expert Comment

ID: 1318517
There's no black magic in using CreateControl().  If you put a breakpoint on COleControl::CreateControl() and let MFC create your control from a dialog box (as you had it set up), you'd see the exact parameters being used to do the creation.

Your solution will work, as long as the inefficiencies and focus problems in having an extra dialog don't bother you.

B ekiM


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
How does storytelling impact the web? What goes into a great story on the web? And how can it be used for marketing purposes? All of these questions will be answered.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question