Solved

ActiveX Control using MFC Extension DLL

Posted on 1998-04-16
3
539 Views
Last Modified: 2013-11-25
I created an ActiveX Control using the ActiveX Control wizard.  The control uses a MFC Extension DLL that works with .EXE applications just fine, and has been deployed at customer sites for several months.

The Extension DLL contains several classes that have member functions that return CStrings from the DLLs local stringtable resources.  It uses the CString::LoadString() function.  This works fine with the .EXE applications, but the ActiveX control does not find the strings in its search through its CDynLinkLibrary chain.

The Extension DLL framework was built by the Extension DLL wizard.  I am using VC++ 5.0

It appears that the ActiveX Control OCX is implemented like an Extension DLL and "wires" itself into the chain.

My question is:  Why don't the DLL functions find their own resources AND what can be done to correct this?

Modifying the Extension DLL, except to try some things out for debugging, is not a viable option.

Thank you
0
Comment
Question by:rhgaracci
3 Comments
 
LVL 7

Expert Comment

by:galkin
Comment Utility
First MFC ActiveX controls are implemented as shared DLL not as extension DLL. It is because ActiveX control exports 4 functions to be called by either OLE (DllGetClassObject, DllCanUnloadNow) or by utility(DllRegisterServer, DllUnregisterServer) which are not MFC applications. Extension DLL can be used only with MFC EXE. So ActiveX control doesn't insert itself into resources chain. Since ActiveX control is shared DLL it has its own CWinApp static instance and has its own internal MFC state, like global map of windows handles and objects, resource handle etc.
To allow control to load string from another dll you must supply handle of this DLL for example add hidden property and set it with the handle.
0
 
LVL 1

Author Comment

by:rhgaracci
Comment Utility
Actually, I figured out the correct answer.  I found the solution in Knowledge Base Article Q154126.  I modified my extension dll to use the two-step initialization as described in the article (search for words "more recommended" for a description) and called the exported initialization function in the ActiveX control app initialization.  This put the extension dll properly in the chain.

Thanks for the reply anyway.

No further responses are requested.
0
 
LVL 1

Accepted Solution

by:
dkremer earned 200 total points
Comment Utility
Hi, I had the same problem, here's the solution I'm using :
Create a new header file, and copy the following :

#ifndef __InstanceSwitcher_h__
#define __InstanceSwitcher_h__

/*
** *********************
** * CInstanceSwitcher *
** *********************
*/

class CInstanceSwitcher
{
public:
      CInstanceSwitcher();
      ~CInstanceSwitcher();

      static HINSTANCE m_hInstance;

private:
      HINSTANCE m_Temp;
};

#endif

Create a new cpp file and copy the following :
/*
** *********************
** * CInstanceSwitcher *
** *********************
*/

#include "StdAfx.h"
#include "InstanceSwitcher.h"

/*
** *********************
** * CInstanceSwitcher *
** *********************
*/

HINSTANCE CInstanceSwitcher::m_hInstance = NULL;

CInstanceSwitcher::CInstanceSwitcher()
{
      if(CInstanceSwitcher::m_hInstance)
      {
            m_Temp = AfxGetResourceHandle();
            AfxSetResourceHandle(m_hInstance);
      }
}

CInstanceSwitcher::~CInstanceSwitcher()
{
      if(CInstanceSwitcher::m_hInstance)
      {
            AfxSetResourceHandle(m_Temp);
      }
}

In the main file of the extention library add the following to DllMain:
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
      // Remove this if you use lpReserved
      UNREFERENCED_PARAMETER(lpReserved);

      if (dwReason == DLL_PROCESS_ATTACH)
      {
            TRACE0("APP8MFC.DLL Initializing!\n");
            
            // Extension DLL one-time initialization
            if (!AfxInitExtensionModule(App8MFCDLL, hInstance))
                  return 0;

            CInstanceSwitcher::m_hInstance = hInstance;
      }
      else if (dwReason == DLL_PROCESS_DETACH)
      {
            TRACE0("APP8MFC.DLL Terminating!\n");
            // Terminate the library before destructors are called
            AfxTermExtensionModule(App8MFCDLL);
      }
      return 1;   // ok
}

Ok, how you use it : Wherever you need (in your extention DLL ofcourse) to use any of it's local resources you just construct a CInstanceSwitcher in your function and voila - it's using the DLLs resources :)

Hope it works for you, it does for me :)

  Bye,
    - DKremer

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now