Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.


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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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.

604 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