Dialogs in DLL

It is nice to be able to make a DLL project that includes its own resources (such as a dialog resource) and then be able to call it from another project. Seems simple but I wnet nuts untill I found out the following:

In the DLL function that pops up the dialog you must manage the state so that the DLL code uses the DLL's resources.

extern __declspec(dllexport) void ShowEditDialog(int &MyData1, int &MyData2)
{
	//ensure we are using our own resources
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	CMyLocalDialog dlg;
	dlg.Arg1 = MyData1; //specific local data for MyLocalDialog
	dlg.Arg2 = MyData2;
	dlg.DoModal();
	MyData1 = dlg.Arg1; //data after processing
	MyData2 = dlg.Arg2;
}

Here's an update sent in by Johan Nilsson

The program calling this exported function will never be able to use the function GetLastError() to check for problems that may have occured (I know it from experience). This is because the AFX_MANAGE_STATE macro creates a temporary object on the stack which, when it is automatically destroyed on function exit, will clear any error codes set for the current thread (it calls into TlsGetValue, deep in MFC).

I've provided a workaround for this, which I've been using myself. Not so pretty, but it works. Example on how to fix the ShowEditDialog fn :

extern __declspec(dllexport) void ShowEditDialog(int &MyData1, int &MyData2)
{
	DWORD dwLastErr = NO_ERROR;

	//
	// surround the code in brackets, which will cause the temporary
	// object created by AFX_MANAGE_STATE to be destroyed before leaving
	// the exported function.
	//
	// NOTE : Do NOT call MFC code outside of these brackets.
	//
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState());
		CMyLocalDialog dlg;
		dlg.Arg1 = MyData1; //specific local data for MyLocalDialog
		dlg.Arg2 = MyData2;
		dlg.DoModal();
		MyData1 = dlg.Arg1; //data after processing
		MyData2 = dlg.Arg2;

		//
		// save possible errors
		//
		dwLastErr = ::GetLastError();
	}

	//
	// only set error if none is currently set.
	// (last error will always be NO_ERROR _unless_
	// TlsGetValue failed earlier)
	//
	if (::GetLastError() == NO_ERROR)
		::SetLastError(dwLastErr);
}