Solved

HELP! Accessing resource in a DLL

Posted on 2000-02-15
3
254 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 500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 169
ODBC Connection Logging, ADO.NET 6 71
dog bark java program 15 120
Fibonacci challenge 11 136
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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