15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Problem in using Shared/Static MFC lib in DLL...help

Glenn T. Jayaputera -- gtj@nunkeen.apana.org.au
Tuesday, March 05, 1996


I am wondering if somebody can shed some light to me...
I am using VC4.0 and Win95

Using AppWizard I create a DLL and  EXE frameworks.
In my DLL I would like to display a dialog box when show() is called.
However, rather than instantiating my dialong box class on the stack I
would like to use the heap.

Therfore the skeleton code of my dll is as follows

class myDLL : CWinApp {
 public:
   CMyDlg *m_pDlg;
   :
   :
};

CMyDll myDLLInstance;

myDLL::InitInstance()
{
  m_pDlg = new CMyDlg;
  return TRUE;
}

myDLl::ExitInstance()
{
  delete m_pDlg;
  return 0;
}

extern "C" __declspec(dllexport) void show()
{
  myDLLInstance.m_pDlg->DoModal();
}


in my EXE program, I linked the mydll.lib file into it and when the
user click a menu entry I just call this show().

My problem is when both the DLL and EXE are build using "shared MFC DLL", the 
dialog box never show up, and when I terminate the program I saw the following 
message in my output window

  First-chance exception in exe.exe (KERNEL32.DLL): 0xC0000005: Access Violation.


If I build them using "static" or DLL->static and EXE->shared then it
works fine.  If I build DLL->shared and EXE->static, it becomes worst,
I got assertion failure as well as the above message in my output
window

Can somebody give me some pointers on this.....  I would like to use
shared MFC on both DLL and EXE

thanks in advance
glenn tesla




Randal Parsons -- Randal.Parsons@btal.com.au
Thursday, March 07, 1996

Glenn T. Jayaputera wrote:
> 
> I am wondering if somebody can shed some light to me...
> I am using VC4.0 and Win95
>

[snip]
 
> My problem is when both the DLL and EXE are build using "shared MFC DLL", the
> dialog box never show up, and when I terminate the program I saw the following
> message in my output window
> 
>   First-chance exception in exe.exe (KERNEL32.DLL): 0xC0000005: Access Violation.
> 
> If I build them using "static" or DLL->static and EXE->shared then it
> works fine.  If I build DLL->shared and EXE->static, it becomes worst,
> I got assertion failure as well as the above message in my output
> window
> 
> Can somebody give me some pointers on this.....  I would like to use
> shared MFC on both DLL and EXE
> 

We has the same problem here when we ported from VC2.2 to VC4. A colleague at work pointed out that you need to 
add the macro AFX_MANAGE_STATE at the beginning of each function you expose that uses MFC.

For example we have a database access DLL that uses the MFC ODBC classes. The functions that only access our 
own data structures do not need this macro, but the functions using anything from MFC do. There is an article 
in the help file called "Managing the State Data of MFC Modules" that hints at what you have to do.

eg :
__declspec(export) void
funcMFC()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// use MFC classes
}

Unfortunately for us, the default implementation of DllMain (in DLLMODUL.CPP) contains the code :

#ifdef _DEBUG
		// check for missing AfxLockTempMap calls
		if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
		{
			TRACE1("Warning: Temp map lock count non-zero (%ld).\n",
				AfxGetModuleThreadState()->m_nTempMapLock);
		}
		AfxLockTempMaps();
		AfxUnlockTempMaps();
#endif

Unless it was compiled with _AFX_PORTABLE defined (which is not the default), the code for AfxUnlockTempMaps 
(in WINHAND.CPP) assumes that AfxGetApp() returns a non-null pointer. If it doesn't, it tries to access 
pApp->m_nSafetyPoolSize and crashes. Since we don't have a main window in our DLL, AfxGetApp() returns NULL. So 
I copied most of the code from DLLMODUL.CPP, commenting out the code given above and renaming some of the 
functions. This had the added advantage that I could modify DllMain to do some initialisation and cleanup as it 
had previously.

If you need more detailed code, I will be happy to send it to you directly.

Regards,

Randal Parsons.




| Вернуться в корень Архива |