Solved

HELP! Accessing resource in a DLL

Posted on 2000-02-15
3
258 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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 …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

636 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