
problem with delay loading DLLs
In VC++ 6.0, when I specify the /DELAYLOAD linker option to delay load a
specific DLL in my application, I get a bunch of
MFC asserts at runtime,
then a crash. I tracked this down to the AfxGetInstanceHandle () routine.
It is supposed to return the instance of the EXE, as long as it's called
from within the EXE. However, after LoadLibrary gets called, it changes to
the instance of the last DLL loaded. Example code in InitInstance of my
app:
HINSTANCE hInst;
hInst = AfxGetInstanceHandle ();
HMODULE hDll = ::LoadLibrary ("MyDll.dll");
hInst = AfxGetInstanceHandle ();
The first AfxGetInstanceHandle call returns the EXE instance. The second
returns the "MyDll.dll" instance. This leads to the asserts and the crash.
If the /DELAYLOAD option is not specified, both calls return the EXE
instance and everything runs fine. Anyone know anything about this?
-Scott Haynie