我在给一个三维图形软件开发中(UG)想用到MFC的对话框。
我的模块通过DLL连接到三维系统中,但是如果我在我的DLL中使用了资源文件,例如对话框后,运行是出错,有没有人对MFC和DLL资源调用比较熟悉的。
以下是我写的代码
static AFX_EXTENSION_MODULE MfcdllforugDLL = { NULL, NULL };
//这是MFC产生的DLL入口
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved); if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("MFCDLLFORUG.DLL Initializing!\n");

// Extension DLL one-time initialization
if (!AfxInitExtensionModule(MfcdllforugDLL, hInstance))
return 0; // Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
//  an MFC Regular DLL (such as an ActiveX Control)
//  instead of an MFC application, then you will want to
//  remove this line from DllMain and put it in a separate
//  function exported from this Extension DLL.  The Regular DLL
//  that uses this Extension DLL should then explicitly call that
//  function to initialize this Extension DLL.  Otherwise,
//  the CDynLinkLibrary object will not be attached to the
//  Regular DLL's resource chain, and serious problems will
//  result.// new CDynLinkLibrary(MfcdllforugDLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("MFCDLLFORUG.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(MfcdllforugDLL);
}
return 1;   // ok
}
//这是三维设计系统的入口函数
extern "C" __declspec(dllexport) void 
ufusr( char *parm, int *returnCode, int rlen )
{
char lpFilename[100];  // path buffer
GetModuleFileName (NULL,lpFilename,100);
HMODULE  ug_handle = GetModuleHandle(lpFilename);
// HINSTANCE hInstance = GetModuleHandle(MAYDLG);
// if (!AfxInitExtensionModule(MfcdllforugDLL, hInstance))
// return;
 
new CDynLinkLibrary(MfcdllforugDLL);    UgSession session( true );//这个是三维系统的初始化函数
try
{
HWND ug_hwnd = GetActiveWindow();
AfxSetResourceHandle(MfcdllforugDLL.hResource);
char lpString[256];
int nMaxCount = 256;
GetWindowText(ug_hwnd,lpString,nMaxCount);
// HINSTANCE hinst = AfxGetInstanceHandle();
CWnd * tempwnd = CWnd::FindWindow(NULL,lpString);
mdllug m_Dlg(tempwnd);
m_Dlg.DoModal();//程序在这一步出错了。
    }

// Handle errors
catch ( const UgException &exception )
{
processException( exception );
}}