Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HELP! Accessing resource in a DLL

Posted on 2000-02-15
3
Medium Priority
?
260 Views
Last Modified: 2013-11-20
Environment: VC++6/SP3

Ok, I've read all the books, searched all the documentation, and tried just about *everything*, and I still can't get this to work: How do I invoke a dialog that is part of an MFC extension DLL from my application? Here is my extension DLL code; *please* let me know what I'm doing wrong!

<snip>
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("RPLOGINEXT.DLL v1.0 Initializing!\n");
            
            // Extension DLL one-time initialization
            if (!AfxInitExtensionModule(RPLoginExtDLL, hInstance))
                  return 0;

            // blah, blah,blah comments

            new CDynLinkLibrary(RPLoginExtDLL);
      }
      else if (dwReason == DLL_PROCESS_DETACH)
      {
            TRACE0("RPLOGINEXT.DLL Terminating!\n");
            // Terminate the library before destructors are called
            AfxTermExtensionModule(RPLoginExtDLL);
      }
      return 1;   // ok
}
</snip>

I created my exported DLL function originally as follows:
<snip>
BOOL LoginDlg( CRPLogin & rpl );
/* porting is #defined as "extern __declspec(dllexport)" */
Porting BOOL DoRPLogin( CRPLogin & rpl )
{
      return LoginDlg( rpl );
}
</snip>

When both my application and the DLL were compiled _DEBUG, everything is fine; the dialogs pop up where expected. However, when I compiled my application in release mode, it bombed off in my derived CDialog class in the DLL at AfxGetResourceHandle() (indicating that my DLL is not being placed on the resource chain).

Next, I tried using the AFX_MANAGE_STATE macro:

<snip>
BOOL LoginDlg( CRPLogin & rpl );
/* porting is #defined as "extern __declspec(dllexport)" */
Porting BOOL DoRPLogin( CRPLogin & rpl )
{
      BOOL bVal = FALSE;

      AFX_MANAGE_STATE( AfxGetStaticModuleState() );

      bVal = LoginDlg( rpl );

      return bVal;
}
</snip>

However, this time, my DLL failed to build:

<snip>
Linking...
mfcs42d.lib(dllmodul.obj) : error LNK2005: __pRawDllMain already defined in RPLoginExt.obj
mfcs42d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in RPLoginExt.obj
mfcs42d.lib(dllmodul.obj) : error LNK2005: __pRawDllMain already defined in RPLoginExt.obj
mfcs42d.lib(dllmodul.obj) : warning LNK4006: _DllMain@12 already defined in RPLoginExt.obj; second definition ignored
mfcs42d.lib(dllmodul.obj) : warning LNK4006: __pRawDllMain already defined in RPLoginExt.obj; second definition ignored
   Creating library Debug/RPLoginExt.lib and object Debug/RPLoginExt.exp
Debug/RPLoginExt.dll : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
</snip>


I have also tried:

<snip>
/* porting is #defined as "extern __declspec(dllexport)" */
Porting BOOL DoRPLogin( CRPLogin & rpl )
{
      BOOL bVal = FALSE;

      // we want to load a specific resource from this DLL
      HINSTANCE hInstOld = AfxGetResourceHandle();
      AfxSetResourceHandle(RPLoginExtDLL.hModule);

      bVal = LoginDlg( rpl );

      AfxSetResourceHandle(hInstOld); // restore the old resource chain

      return bVal;
}
</snip>

with no success; my app bombs off at AfxGetResourceHandle().

_AFXDLL is defined in the application that invokes the DLL. WHAT AM I DOING WRONG ?
The only thing I can think of is that my app uses an ActiveX control; does this mean that I need to export an explicit DLL initialization function (new CDynLinkLibrary() and all that) ? Does the fact that my DLL is _DEBUG and my application is release have anything to do with this ?

Thanks for any help you can give.
0
Comment
Question by:xn12z9i
[X]
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
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 2524476
>>However, when I compiled my application in release mode,
>>it bombed off in my derived CDialog class in the DLL at
>>AfxGetResourceHandle() (indicating that my DLL is not
>>being placed on the resource chain).

This is a 'common' behaviour - you *MUST* *NOT* mix debug and release versions when building an extension DLL...
0
 

Author Comment

by:xn12z9i
ID: 2524493
That would explain it .... Thanks!
0
 
LVL 86

Expert Comment

by:jkr
ID: 2524533
Hmm, as you accepted my comment as an answer so quickly, I think I should elaborate a little - TN058 describes the MFC module state implementation and how memory allocations are handled. When you're mixing debug and release versions, at least 2 different memory allocators are being used (the debug version and the release version), which definitely will lead to problems...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

721 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