Solved

HELP! Accessing resource in a DLL

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 93
maven archtype selection in eclipse 1 57
matchUp  challenge 6 64
mapAB Challlenge 35 145
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: 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 …
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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