在DLL中嵌入基于MFC对话框的应用程序,并使用RunDll32运行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在DLL中嵌入基于MFC对话框的应用程序,并使用RunDll32运行相关的知识,希望对你有一定的参考价值。

This approach allows an application to be embedded in a DLL. This is handy for diagnostic/maintenance utilities that are used with the DLL.

Steps to create:
* Create a MFC DLL project
* Add a dialog
* Add an entry point function such as void CALLBACK AppInDll_Entry(HWND hwnd, HINSTANCE hinst, LPCSTR lpCmdLine, int nCmdShow)
* In this new function, do this:
CDlgInDll dlg;
theApp.m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
* in CAppInDllApp::InitInstance(), do this
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);

SetRegistryKey(_T("Local AppWizard-Generated Applications"));

CWinApp::InitInstance();
* Launch the 'app' using runDll32, for example:
rundll32 AppInDll.dll AppInDll_Entry a b c
or
ShellExecute( NULL, "open", "c:\windows\system32\rundll32.exe", "AppInDll.dll AppInDll_Entry a b c", NULL, SW_SHOWNORMAL) ;
  1. BOOL CAppInDllApp::InitInstance()
  2. {
  3. // InitCommonControlsEx() is required on Windows XP if an application
  4. // manifest specifies use of ComCtl32.dll version 6 or later to enable
  5. // visual styles. Otherwise, any window creation will fail.
  6. INITCOMMONCONTROLSEX InitCtrls;
  7. InitCtrls.dwSize = sizeof(InitCtrls);
  8. // Set this to include all the common control classes you want to use
  9. // in your application.
  10. InitCtrls.dwICC = ICC_WIN95_CLASSES;
  11. InitCommonControlsEx(&InitCtrls);
  12.  
  13. // Standard initialization
  14. // If you are not using these features and wish to reduce the size
  15. // of your final executable, you should remove from the following
  16. // the specific initialization routines you do not need
  17. // Change the registry key under which our settings are stored
  18. // TODO: You should modify this string to be something appropriate
  19. // such as the name of your company or organization
  20. SetRegistryKey(_T("Local AppWizard-Generated Applications"));
  21.  
  22. CWinApp::InitInstance();
  23.  
  24. return TRUE;
  25. }
  26.  
  27. void CALLBACK AppInDll_Entry(HWND hwnd, HINSTANCE hinst, LPCSTR lpCmdLine, int nCmdShow)
  28. {
  29. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  30.  
  31. // Standard initialization
  32. // If you are not using these features and wish to reduce the size
  33. // of your final executable, you should remove from the following
  34. // the specific initialization routines you do not need
  35. // Change the registry key under which our settings are stored
  36. // TODO: You should modify this string to be something appropriate
  37. // such as the name of your company or organization
  38. /// SetRegistryKey(_T("Local AppWizard-Generated Applications"));
  39.  
  40. CDlgInDll dlg;
  41. theApp.m_pMainWnd = &dlg;
  42. INT_PTR nResponse = dlg.DoModal();
  43. if (nResponse == IDCANCEL)
  44. {
  45. // TODO: Place code here to handle when the dialog is
  46. // dismissed with Cancel
  47. }
  48.  
  49. }

以上是关于在DLL中嵌入基于MFC对话框的应用程序,并使用RunDll32运行的主要内容,如果未能解决你的问题,请参考以下文章

从应用程序对话框访问的 MFC 自定义键盘 DLL

MFC中,如何连接sqlite3的数据库,并对此数据库操作??

MFC 如何将一个对话框嵌入到视图中

创建共享 MFC 对话框:常规 DLL 或 MFC 扩展 DLL

MFC DLL导出对话框问题

从另一个 DLL 加载 MFC 应用程序对话框