DLL and GetProcAddress() Problem

I've been developing a real time Image processing with
QuickCam program.
There are mainly 4 files

RV2.exe(uses rvcommon.dll through import lib and using mfc in dll)

rvcommon.dll(uses mfc in dll the project generated with appwizard using MfcExtension using MFC in dll)

addon1.dll(uses rvcommon.dll through import lib the project generated with appwizard using regular dll using MFC in dll)

addon2.dll(uses rvcommon.dll through import lib the project generated with appwizard using regular dll using MFC in dll)


addon*.dll is where the image processing functions reside.
users can load and unload them at rumtime.

rv2.exe would load addon1.dll and addon2.dll at runtime
using AfxLoadLibrary();

The program works fine under debug build(VC++5.0 sp3).But When I do a release build the program causes GPF whenever there is GetProcAddress() api. I have verified that AfxLoadLibrary() return correct HINSTANCE.

Does anyone got any idea why??


thanks

psksvp@ccs.neu.edu
petersvpAsked:
Who is Participating?
 
dustinnConnect With a Mentor Commented:
In order to pass an MFC class between modules (EXE and DLL or DLL and DLL) you must create an MFC Extension DLL.  A normal DLL will not work.

If you are using VC++ 5.0, AppWizard can create a shell of an MFC Extension DLL for you.  An extension DLL requires certain #defines and other implementation specifics that a regular DLL doesn't.

Check out "Inside VC++" by David Kruglinski for an in-depth discussion of the differences between the two.
0
 
mnguyen021997Commented:
a lot of times people get confused with ASSERT() and VERIFY().  you wouldn't happen to be doing an

ASSERT(AfxLoadLibrary()) would you?? if so then switch it to VERIFY().  

also initialize your hinstance variable to null prior to calling afxloadlibrary.  unless msoft screwed up it shouldn't be crashing for a null hinstance but may crash for an invalid (random stack value) instance.
0
 
petersvpAuthor Commented:
The problem is not the AfxLoadLibrary(), I verified it, before
I call GetProcAddress(). GetProcAddress() causes GPF whenever
I call it in a release build. In debug build, my program
works perfectly..without any asserting...

anyhelp would be great

psksvp@ccs.neu.edu
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
JohnWeidnerCommented:
If you load the library with out a full path specification, it could be that there is an old copy of the DLL in the "release" directory that is being loaded.
0
 
dr_funfrockCommented:

  I think you need to use _AFXDLL define in your app or DLL.
0
 
petersvpAuthor Commented:
When I build(both debug and release), I always have all the
dll(s) and exe, put into a same dir, regradless of release
or debug build.. so there would be only one place where the    dll(s) could be.
0
 
dustinnCommented:
Can you post a code snippet of the GetProcAddress call that is causing the crash?  Also, include any pertinent code around that general area of the program (specifically, code that modifies any of the parameters to GetProcAddress).
0
 
petersvpAuthor Commented:
Is this can be the case, I pass and MFC-derived class to a
function in the DLL. I just read an article in KB that MFC app
cannot pass MFC-derived class to RegularDLL..

nayone has any idea, if this is true..
0
 
petersvpAuthor Commented:
one last question before I go and fix my code.
If I call the MFC Extension DLL explicitly by using
AfxLoadLibrary(). Does the DLLMain() get called?
Or I need another export function that create
CDynLinkLibrary() ?
Thanks for all yoou helps
psksvp@ccs.neu.edu
0
 
dustinnCommented:
DllMain() will get called.  You should never have to play with the CDynLinkLibrary object that gets created by your DLL.  You should create the object in your DllMain() with a call to AfxInitExtensionModule() like so:

BOOL WINAPI DllMain(HMODULE hInst, ULONG uReason,
                    LPVOID lpvReserved)
{
    if (uReason == DLL_PROCESS_ATTACH)
    {
        if (!AfxInitExtensionModule(extMyExtension, hInst))
            return 0;
    }

    return 1;

}

And, if you are using AfxLoadLibrary() to load the DLL you should also add the following code to DllMain():

if (uReason == DLL_PROCESS_DETACH)
{
    AfxTermExtensionModule(extMyExtension)
}

This is not necessary, but recommended, otherwise the memory and resources allocated by your call to AfxInitExtensionModule() will not be freed until the calling executable terminates.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.