home *** CD-ROM | disk | FTP | other *** search
- #include <LowMem.h>
- #include "ResourceMap.h"
- #include "DllEntry.h"
- #include "CCFragResource.h"
-
-
- long CCFragResource::EntryCount = 0;
- ResourceMapHandle CCFragResource::CFragTopMapHdl = NULL;
- ResourceMapHandle CCFragResource::ContainerTopMapHdl = NULL;
- ResourceMapHandle CCFragResource::DLLTopMapHdl = NULL;
- short CCFragResource::CFragResRefNum = -1;
- short CCFragResource::ContainerResRefNum = -1;
-
-
- CCFragResource::CCFragResource()
- {
- if (EntryCount++ == 0)
- {
- // if we are coming through the first time then
- // ask the code fragment/dll about its initial resource state
- if (DLLTopMapHdl == NULL)
- DLLTopMapHdl = ::GetDllTopMapHdl();
- if (CFragTopMapHdl == NULL)
- CFragTopMapHdl = DLLTopMapHdl;
- if (CFragResRefNum == -1)
- CFragResRefNum = ::GetDllResRefNum();
-
- // save off the current resource state for latter
- ContainerTopMapHdl = ResourceMapHandle( ::LMGetTopMapHndl() );
- ContainerResRefNum = ::CurResFile();
-
- // test to see if the resource chain is consistent
- // if the container closed the resource file that was at the top of the resource
- // chain when the DLL was opened then the DLL's NextResourceMap handle
- // is invalid, and trying to put the DLL's resource map record on the top
- // of the chain could be REALLY BAD NEWS.
- // we can detect this condition by checking to see if DLL->NextResourceMap is
- // in the resource map chain. we attempt to put set DLL->NextResourceMap
- // to the current top of the chain if the unthinkable has happened.
- // Unfortunately for us, it doesn't always work.
-
- Boolean Found = false;
- ResourceMapHandle TempHandle = ContainerTopMapHdl;
- ResourceMapHandle Target = (*DLLTopMapHdl)->NextResourceMap;
- ResourceMapHandle Stop = ResourceMapHandle( ::LMGetSysMapHndl());
-
- while (!(Found = TempHandle == Target) &&
- (TempHandle = (*TempHandle)->NextResourceMap) != Stop)
- ;
-
- if (!Found)
- {
- // WARNING - WARNING - WARNING
- // this MAY crash
- // send a debugger message to let everyone know.
- DebugStr("\pWarning - Container closed resource DLL was pointing to.");
- (*DLLTopMapHdl)->NextResourceMap = ContainerTopMapHdl;
- }
-
- // set up the code fragment/dll's resource state
- ::LMSetTopMapHndl( Handle(CFragTopMapHdl) );
- ::UseResFile(CFragResRefNum);
- }
- }
-
-
- CCFragResource::~CCFragResource()
- {
- if (--EntryCount == 0)
- {
-
- // save the code fragment/dll's resource state
- CFragTopMapHdl = ResourceMapHandle( ::LMGetTopMapHndl() );
- CFragResRefNum = ::CurResFile();
-
- // restore the resource state of the container
- ::LMSetTopMapHndl( Handle(ContainerTopMapHdl) );
- ::UseResFile(ContainerResRefNum);
- }
- }
-