Article ID: 173974
Article Last Modified on 6/2/2005
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation extensions
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
protected:
BOOL m_bRun;
};
BOOL CMyApp::InitInstance()
{
// Initialize OLE libraries.
if (!AfxOleInit())
{
AfxMessageBox(_T("OLE Initialization Failed!"));
return FALSE;
}
// Initialize CcomModule.
_Module.Init(ObjectMap, m_hInstance);
_Module.dwThreadID = GetCurrentThreadId();
// Check command line arguments.
TCHAR szTokens[] = _T("-/");
m_bRun = TRUE;
LPCTSTR lpszToken = FindOneOf(m_lpCmdLine, szTokens);
while (lpszToken != NULL)
{
// Register ATL and MFC class factories.
if (lstrcmpi(lpszToken, _T("Embedding"))==0 ||
lstrcmpi(lpszToken, _T("Automation"))==0)
{
AfxOleSetUserCtrl(FALSE);
break;
}
// Unregister servers.
// There is no unregistration code for MFC
// servers. Refer to <WWLINK TYPE="ARTICLE" VALUE="Q186212">Q186212</WWLINK> "How To Unregister MFC
// Automation Servers" for adding unregistration
// code.
else if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
{
VERIFY(SUCCEEDED(_Module.UpdateRegistryFromResource(IDR_ServerS2B, FALSE)));
//Replace IDR_ServerS2B with your project specific resource ID for the registry script resource
VERIFY(SUCCEEDED(_Module.UnregisterServer(TRUE)));
m_bRun = FALSE;
break;
}
// Register ATL and MFC objects in the registry.
else if (lstrcmpi(lpszToken, _T("RegServer"))==0)
{
VERIFY(SUCCEEDED(_Module.UpdateRegistryFromResource(IDR_ServerS2B, TRUE)));
VERIFY(SUCCEEDED(_Module.RegisterServer(TRUE)));
COleObjectFactory::UpdateRegistryAll();
m_bRun = FALSE;
break;
}
lpszToken = FindOneOf(lpszToken, szTokens);
}
if (m_bRun)
{
// Comment out the next line if not using VC 6-generated
// code.
_Module.StartMonitor();
VERIFY(SUCCEEDED(_Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE)));
VERIFY(COleObjectFactory::RegisterAll());
// To run the EXE standalone, you need to create a window
// and assign the CWnd* to m_pMainWnd.
LPCTSTR szClass = AfxRegisterWndClass(NULL);
m_pMainWnd = new CWnd;
m_pMainWnd->CreateEx(0, szClass, _T("SomeName"), 0, CRect(0, 0, 0, 0), NULL, 1234);
}
return TRUE;
}
int CMyApp::ExitInstance()
{
// MFC's class factories registration is
// automatically revoked by MFC itself.
if (m_bRun)
{
_Module.RevokeClassObjects();
Sleep(dwPause); //wait for any threads to finish
}
_Module.Term();
return 0;
}
125750 You receive an "unresolved external symbol _WinMain@16" error message when you try to build an MFC application with Unicode support in Visual C++
AFX_MANAGE_STATE(AfxGetAppModuleState());For more information on AFX_MANAGE_STATE, consult the VC++ online documentation.
181505 PRB: ATL COM AppWizard Doesn't Offer MFC Support for .EXE
BOOL CMyApp::InitInstance()
{
_Module.Init(ObjectMap, m_hInstance);
return CWinApp::InitInstance();
}
int CMyApp::ExitInstance()
{
// MFC's class factories registration is
// automatically revoked by MFC itself.
if (m_bRun)
_Module.RevokeClassObjects();
AFX_MANAGE_STATE(AfxGetStaticModuleState());For more information, click the following article number to view the article in the Microsoft Knowledge Base:
140850 How to properly export functions by using the MFC Shared Library
186212 How to unregister MFC automation servers
Additional query words: checkbox listbox
Keywords: kbhowto kbinfo KB173974