How to use MFC CDialog in a DLL ?
-
Hello, folks, I got some trouble using MFC CDialog class in my DLL (MFC static linked DLL). Though my DLL is made MFC static linked, when I use MFC classes to create a dialog (pop-up or child), I always got a window creation error. I know I can create dialog using win32 SDK (i.e, using CreateDialog(..) or DialogBoxParam(..), I did them before !), but I still want to use MFC to create my dialog. Because I also have something like CListCtrl or CListView on that dialog. Could anybody help me ? Or I should use win32 SDK instead ? And, are "list control" and "list view" same ? Any response would be appreciated.
-
Hello, folks, I got some trouble using MFC CDialog class in my DLL (MFC static linked DLL). Though my DLL is made MFC static linked, when I use MFC classes to create a dialog (pop-up or child), I always got a window creation error. I know I can create dialog using win32 SDK (i.e, using CreateDialog(..) or DialogBoxParam(..), I did them before !), but I still want to use MFC to create my dialog. Because I also have something like CListCtrl or CListView on that dialog. Could anybody help me ? Or I should use win32 SDK instead ? And, are "list control" and "list view" same ? Any response would be appreciated.
-
Yo Yo~~ wrote: Though my DLL is made MFC static linked, when I use MFC classes to create a dialog (pop-up or child), I always got a window creation error. Post your code, pls ... The code is more likely to be wrong than the MFC dll :) ~RaGE();
/// on MyGUIDlg.cpp; a dialog based application void CGUIDlg::OnViewLog() { DWORD r = 0; r = ViewLogFile( this->m_hWnd ); } /// on MyDLL.cpp; a MFC static linked DLL DLLExport DWORD WINAPI ViewLogFile( HWND hParendWnd ) { CWnd* pParentWnd = new CWnd(); pParentWnd->Attach( hParendWnd ); CEventLogDlg* pLogDlg = new CEventLogDlg( pParentWnd ); if ( pLogDlg != NULL ) { pLogDlg->Create( IDD_EVENT_LOG, pParentWnd ); ///IDD_EVENT_LOG is resource ID pLogDlg->ShowWindow( SW_SHOW ); } } /// class CEventLogDlg class CEventLogDlg : public CDialog { ........ }
-
/// on MyGUIDlg.cpp; a dialog based application void CGUIDlg::OnViewLog() { DWORD r = 0; r = ViewLogFile( this->m_hWnd ); } /// on MyDLL.cpp; a MFC static linked DLL DLLExport DWORD WINAPI ViewLogFile( HWND hParendWnd ) { CWnd* pParentWnd = new CWnd(); pParentWnd->Attach( hParendWnd ); CEventLogDlg* pLogDlg = new CEventLogDlg( pParentWnd ); if ( pLogDlg != NULL ) { pLogDlg->Create( IDD_EVENT_LOG, pParentWnd ); ///IDD_EVENT_LOG is resource ID pLogDlg->ShowWindow( SW_SHOW ); } } /// class CEventLogDlg class CEventLogDlg : public CDialog { ........ }
There may be a confusion about the resource you try to load. Called from an application, your DLL will try to load application's resource. To be sure what you are loading, try this: In the dialog constructor: CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/) : CDialog(/*CMyDlg::IDD*/) { HINSTANCE hInstance = GetModuleHandle(_T("MyDLL")); VERIFY(hInstance); HRSRC hResource = ::FindResource(hInstance, MAKEINTRESOURCE(IDD_MYRESOURCE), RT_DIALOG); VERIFY(hResource); HGLOBAL hTemplate = LoadResource(hInstance, hResource); VERIFY(hTemplate); InitModalIndirect((HGLOBAL)hTemplate, pParent); FreeResource(hTemplate); } We do not inherit the Earth from our ancestors. We borrow it from our children. Antoine de Saint Exupéry (1900-1944)
-
There may be a confusion about the resource you try to load. Called from an application, your DLL will try to load application's resource. To be sure what you are loading, try this: In the dialog constructor: CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/) : CDialog(/*CMyDlg::IDD*/) { HINSTANCE hInstance = GetModuleHandle(_T("MyDLL")); VERIFY(hInstance); HRSRC hResource = ::FindResource(hInstance, MAKEINTRESOURCE(IDD_MYRESOURCE), RT_DIALOG); VERIFY(hResource); HGLOBAL hTemplate = LoadResource(hInstance, hResource); VERIFY(hTemplate); InitModalIndirect((HGLOBAL)hTemplate, pParent); FreeResource(hTemplate); } We do not inherit the Earth from our ancestors. We borrow it from our children. Antoine de Saint Exupéry (1900-1944)
I tried your suggestion, put that code in my dialog's constructor and compiled the dll. Then in my app, I added the dialog's *.h file, but when I tried to compile my app, I got and error about an undefined symbol in the dialog box's header file: #ifdef _USRDLL #define DllExport __declspec( dllexport ) #else #define DllExport __declspec( dllimport ) #endif class DllExport CMyDlg : public CDialog { // Construction public: enum { IDD = IDD_MYDIALOG_DIALOG }; I know that IDD_MYDIALOG_DIALOG is defined in the dll's resource.h file. Do I also have to include it in my app's *.cpp ?
-
There may be a confusion about the resource you try to load. Called from an application, your DLL will try to load application's resource. To be sure what you are loading, try this: In the dialog constructor: CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/) : CDialog(/*CMyDlg::IDD*/) { HINSTANCE hInstance = GetModuleHandle(_T("MyDLL")); VERIFY(hInstance); HRSRC hResource = ::FindResource(hInstance, MAKEINTRESOURCE(IDD_MYRESOURCE), RT_DIALOG); VERIFY(hResource); HGLOBAL hTemplate = LoadResource(hInstance, hResource); VERIFY(hTemplate); InitModalIndirect((HGLOBAL)hTemplate, pParent); FreeResource(hTemplate); } We do not inherit the Earth from our ancestors. We borrow it from our children. Antoine de Saint Exupéry (1900-1944)
Thanks ! It really helps. I tried your codes and added something in my DLL as follows. Then it worked ! /// in MyDLL.cpp CMyDlg* InitMyDialog( HWND hParent ) { CMyDlg* pMyDlg = NULL; AFX_MANAGE_STATE( AfxGetStaticModuleState() ); pMyDlg = new CMyDlg( CWnd::FromHandle( hParent ) ); if ( pMyDlg != NULL ) pMyDlg->ShowWindow( SW_HIDE ); return pMyDlg; }