embed an MFC dialog-based app in a DLL and run using RunDll32


/ Published in: C++
Save to your folder(s)

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) ;


Copy this code and paste it in your HTML
  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. }

URL: http://www.codeproject.com/KB/DLL/eventrecorder.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.