Return to Snippet

Revision: 17141
at August 24, 2009 18:52 by jimfred


Initial Code
BOOL CAppInDllApp::InitInstance()
{
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

   CWinApp::InitInstance();

	return TRUE;
}

void CALLBACK AppInDll_Entry(HWND hwnd, HINSTANCE hinst, LPCSTR lpCmdLine, int nCmdShow)
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState());

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	/// SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	CDlgInDll dlg;
   theApp.m_pMainWnd = &dlg;
	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

}

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

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

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

Initial Tags
windows

Initial Language
C++