Solved

HELP! Accessing resource in a DLL

Posted on 2000-02-15
3
250 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
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
>>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
Comment Utility
That would explain it .... Thanks!
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

10 Experts available now in Live!

Get 1:1 Help Now