// [!output PROJECT_NAME].cpp : main source file for [!output PROJECT_NAME].exe
//
#include "stdafx.h"
#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>
[!if USE_CMDBAR]
#include <atlctrlw.h>
[!endif]
[!if USE_VIEW_CLASS]
[!if VIEWTYPE_DHTML]
#include <atldhtml.h>
[!endif]
[!endif]
[!if ATL_USE_ATTRIBUTES]
#include <atlplus.h>
[!endif]
#include "resource.h"
[!if CREATE_COM_SERVER]
[!if ATL_USE_ATTRIBUTES]
#include "[!output PROJECT_NAME]idl.h"
[!else]
// Note: Proxy/Stub Information
// To build a separate proxy/stub DLL,
// run nmake -f [!output PROJECT_NAME]ps.mk in the project directory.
#include "initguid.h"
#include "[!output SAFE_PROJECT_NAME].h"
#include "[!output SAFE_PROJECT_NAME]_i.c"
[!endif]
[!endif]
[!if USE_VIEW_CLASS]
#include "[!output VIEW].h"
[!endif]
[!if !ATL_PROJTYPE_DLGMODAL]
#include "aboutdlg.h"
[!endif]
[!if ATL_PROJTYPE_MDI]
#include "[!output CHILD_FRAME].h"
[!endif]
[!if ATL_PROJTYPE_DLG || ATL_PROJTYPE_DLGMODAL]
#include "maindlg.h"
[!else]
#include "[!output FRAME].h"
[!endif]
[!if ATL_USE_ATTRIBUTES]
[ module(application) ]
class TESTModule
{
public:
};
[!endif]
[!if CREATE_COM_SERVER]
CServerAppModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()
[!else]
CAppModule _Module;
[!endif]
#if _DEBUG
ATL::CTraceCategory atlTraceUI( _T("atlTraceUI") );
#endif
[!if ATL_PROJTYPE_DLG]
int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);
[!output ATL_MAINDLG_CLASS] dlgMain;
if(dlgMain.Create(NULL) == NULL)
{
ATLTRACE(_T("Main dialog creation failed!\n"));
return 0;
}
[!if CREATE_COM_SERVER]
_Module.Lock();
[!endif]
dlgMain.ShowWindow(nCmdShow);
int nRet = theLoop.Run();
_Module.RemoveMessageLoop();
return nRet;
}
[!endif ]
[!if ATL_PROJTYPE_MTSDI]
class C[!output SAFE_PROJECT_NAME]ThreadManager
{
public:
// thread init param
struct _RunData
{
LPTSTR lpstrCmdLine;
int nCmdShow;
};
// thread proc
static DWORD WINAPI RunThread(LPVOID lpData)
{
CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);
_RunData* pData = (_RunData*)lpData;
[!output FRAME_CLASS] wndFrame;
if(wndFrame.CreateEx() == NULL)
{
ATLTRACE(_T("Frame window creation failed!\n"));
return 0;
}
[!if CREATE_COM_SERVER]
_Module.Lock();
[!endif] //CREATE_COM_SERVER
wndFrame.ShowWindow(pData->nCmdShow);
::SetForegroundWindow(wndFrame); // Win95 needs this
delete pData;
int nRet = theLoop.Run();
_Module.RemoveMessageLoop();
return nRet;
}
DWORD m_dwCount;
HANDLE m_arrThreadHandles[MAXIMUM_WAIT_OBJECTS - 1];
C[!output SAFE_PROJECT_NAME]ThreadManager() : m_dwCount(0)
{ }
// Operations
DWORD AddThread(LPTSTR lpstrCmdLine, int nCmdShow)
{
if(m_dwCount == (MAXIMUM_WAIT_OBJECTS - 1))
{
::MessageBox(NULL, _T("ERROR: Cannot create ANY MORE threads!!!"), _T("[!output PROJECT_NAME]"), MB_OK);
return 0;
}
_RunData* pData = new _RunData;
pData->lpstrCmdLine = lpstrCmdLine;
pData->nCmdShow = nCmdShow;
DWORD dwThreadID;
HANDLE hThread = ::CreateThread(NULL, 0, RunThread, pData, 0, &dwThreadID);
if(hThread == NULL)
{
::MessageBox(NULL, _T("ERROR: Cannot create thread!!!"), _T("[!output PROJECT_NAME]"), MB_OK);
return 0;
}
m_arrThreadHandles[m_dwCount] = hThread;
m_dwCount++;
return dwThreadID;
}
void RemoveThread(DWORD dwIndex)
{
::CloseHandle(m_arrThreadHandles[dwIndex]);
if(dwIndex != (m_dwCount - 1))
m_arrThreadHandles[dwIndex] = m_arrThreadHandles[m_dwCount - 1];
m_dwCount--;
}
int Run(LPTSTR lpstrCmdLine, int nCmdShow)
{
MSG msg;
// force message queue to be created
::PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
AddThread(lpstrCmdLine, nCmdShow);
int nRet = m_dwCount;
DWORD dwRet;
while(m_dwCount > 0)
{
dwRet = ::MsgWaitForMultipleObjects(m_dwCount, m_arrThreadHandles, FALSE, INFINITE, QS_ALLINPUT);
if(dwRet == 0xFFFFFFFF)
::MessageBox(NULL, _T("ERROR: Wait for multiple objects failed!!!"), _T("[!output PROJECT_NAME]"), MB_OK);
else if(dwRet >= WAIT_OBJECT_0 && dwRet <= (WAIT_OBJECT_0 + m_dwCount - 1))
RemoveThread(dwRet - WAIT_OBJECT_0);
else if(dwRet == (WAIT_OBJECT_0 + m_dwCount))
{
::GetMessage(&msg, NULL, 0, 0);
if(msg.message == WM_USER)
AddThread("", SW_SHOWNORMAL);
else
::MessageBeep((UINT)-1);
}
else
::MessageBeep((UINT)-1);
}
return nRet;
}
};
[!endif]
[!if !ATL_PROJTYPE_DLGMODAL && !ATL_PROJTYPE_DLG ]
int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);
[!output FRAME_CLASS] wndMain;
if(wndMain.CreateEx() == NULL)
{
ATLTRACE(_T("Main window creation failed!\n"));
return 0;
}
[!if CREATE_COM_SERVER]
_Module.Lock();
[!endif]
wndMain.ShowWindow(nCmdShow);
int nRet = theLoop.Run();
_Module.RemoveMessageLoop();
return nRet;
}
[!endif]
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
HRESULT hRes = ::CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call instead to
// make the EXE free threaded. This means that calls come in on a random RPC thread.
// HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
ATLASSERT(SUCCEEDED(hRes));
#if (_WIN32_IE >= 0x0300)
INITCOMMONCONTROLSEX iccx;
iccx.dwSize = sizeof(iccx);
[!if !USE_TOOLBAR || !USE_REBAR]
iccx.dwICC = ICC_BAR_CLASSES; // change to support other controls
[!else]
iccx.dwICC = ICC_COOL_CLASSES | ICC_BAR_CLASSES;
[!endif]
BOOL bRet = ::InitCommonControlsEx(&iccx);
bRet;
ATLASSERT(bRet);
#else
::InitCommonControls();
#endif
[!if CREATE_COM_SERVER]
hRes = _Module.Init(ObjectMap, hInstance, &LIBID_[!output SAFE_PROJECT_NAME]Lib);
ATLASSERT(SUCCEEDED(hRes));
[!else]
#ifdef _DEBUG
hRes = _Module.Init(NULL, hInstance, &LIBID_ATLLib); //Prevents ATL 7 ASSERTION
#else
hRes = _Module.Init(NULL, hInstance);
#endif
ATLASSERT(SUCCEEDED(hRes));
[!endif]
[!if USE_VIEW_CLASS]
[!if VIEWTYPE_RICHEDIT]
HINSTANCE hInstRich = ::LoadLibrary(CRichEditCtrl::GetLibraryName());
ATLASSERT(hInstRich != NULL);
[!endif]
[!endif]
[!if USE_AXWIN]
AtlAxWinInit();
[!endif]
[!if CREATE_COM_SERVER]
int nRet = 0;
TCHAR szTokens[] = _T("-/");
bool bRun = true;
bool bAutomation = false;
LPCTSTR lpszToken = _Module.FindOneOf(::GetCommandLine(), szTokens);
while(lpszToken != NULL)
{
if(lstrcmpi(lpszToken, _T("UnregServer")) == 0)
{
nRet = _Module.UnregisterServer();
bRun = false;
break;
}
else if(lstrcmpi(lpszToken, _T("RegServer")) == 0)
{
nRet = _Module.RegisterServer();
bRun = false;
break;
}
else if((lstrcmpi(lpszToken, _T("Automation")) == 0) ||
(lstrcmpi(lpszToken, _T("Embedding")) == 0))
{
bAutomation = true;
break;
}
lpszToken = _Module.FindOneOf(lpszToken, szTokens);
}
if(bRun)
{
_Module.StartMonitor();
hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);
ATLASSERT(SUCCEEDED(hRes));
hRes = ::CoResumeClassObjects();
ATLASSERT(SUCCEEDED(hRes));
if(bAutomation)
{
CMessageLoop theLoop;
nRet = theLoop.Run();
}
else
[!if ATL_PROJTYPE_DLGMODAL]
{
[!output ATL_MAINDLG_CLASS] dlgMain;
nRet = dlgMain.DoModal();
}
[!endif]
[!if ATL_PROJTYPE_MTSDI]
{
C[!output SAFE_PROJECT_NAME]ThreadManager mgr;
nRet = mgr.Run(lpstrCmdLine, nCmdShow);
}
[!else]
{
nRet = Run(lpstrCmdLine, nCmdShow);
}
[!endif]
_Module.RevokeClassObjects();
::Sleep(_Module.m_dwPause);
}
[!else]
[!if ATL_PROJTYPE_DLGMODAL]
[!output ATL_MAINDLG_CLASS] dlgMain;
int nRet = dlgMain.DoModal();
[!endif]
[!if ATL_PROJTYPE_MTSDI]
C[!output SAFE_PROJECT_NAME]ThreadManager mgr;
int nRet = mgr.Run(lpstrCmdLine, nCmdShow);
[!else]
[!if !ATL_PROJTYPE_DLGMODAL && !CREATE_COM_SERVER]
int nRet = Run(lpstrCmdLine, nCmdShow);
[!endif]
[!endif]
[!endif]
[!if USE_VIEW_CLASS]
[!if VIEWTYPE_RICHEDIT]
::FreeLibrary(hInstRich);
[!endif]
[!endif]
[!if !ATL_USE_ATTRIBUTES]
_Module.Term();
::CoUninitialize();
[!endif]
return nRet;
}