Posted By

jimfred on 08/24/09


Tagged

windows dll mfc


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

khouser


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


 / Published in: C++
 

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

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 AppInDllEntry(HWND hwnd, HINSTANCE hinst, LPCSTR lpCmdLine, int nCmdShow) * In this new function, do this: CDlgInDll dlg; theApp.mpMainWnd = &dlg; INTPTR nResponse = dlg.DoModal(); * in CAppInDllApp::InitInstance(), do this INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); InitCtrls.dwICC = ICCWIN95_CLASSES; InitCommonControlsEx(&InitCtrls);

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

CWinApp::InitInstance(); * Launch the 'app' using runDll32, for example: rundll32 AppInDll.dll AppInDllEntry a b c or ShellExecute( NULL, "open", "c:\windows\system32\rundll32.exe", "AppInDll.dll AppInDllEntry 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. }

Report this snippet  

You need to login to post a comment.