/********************************************************************\
| main( ) function for MFC Console Applications                      |
|   Ðàçðàáîò÷èê Äþêîâ Ñ. Í. \   ÍÍÖ  ÕÔÒÈ    \   1996 - 2004 ãã.     |
\********************************************************************/
#include "StdAfx.h"
#include "Trace.h"
#include "afxmain.tmh"

int __cdecl _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    HINSTANCE hInstance     = HINSTANCE(GetModuleHandle( NULL ));
    HINSTANCE hPrevInstance = NULL;
    LPTSTR  lpCmdLine = ::GetCommandLine( );
    int     nCmdShow = 0;
	int nReturnCode = -1;
	CWinThread* pThread = AfxGetThread();
	CWinApp* pApp = AfxGetApp();
    TraceEvents(
        TRACE_LEVEL_INFORMATION,
        TRACE_AFXMAIN,
        "%!FUNC! line:%!LINE! %!LEVEL! Enter"
        );
    TraceEvents(
        TRACE_LEVEL_INFORMATION,
        TRACE_AFXMAIN,
        "%!FUNC! line:%!LINE! %!LEVEL!    argc = %d",
        argc
        );
    TraceEvents(
        TRACE_LEVEL_INFORMATION,
        TRACE_AFXMAIN,
        "%!FUNC! line:%!LINE! %!LEVEL!    argv = %p",
        argv
        );
    TraceEvents(
        TRACE_LEVEL_INFORMATION,
        TRACE_AFXMAIN,
        "%!FUNC! line:%!LINE! %!LEVEL!    envp = %p",
        envp
        );
    // AFX internal initialization
    if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
    {
        TraceEvents(
            TRACE_LEVEL_ERROR,
            TRACE_AFXMAIN,
            "%!FUNC! line:%!LINE! %!LEVEL! Call \"AfxWinInit\" failure!"
            );
        goto InitFailure;
    }
	// App global initializations (rare)
	if (pApp != NULL && !pApp->InitApplication())
    {
        TraceEvents(
            TRACE_LEVEL_ERROR,
            TRACE_AFXMAIN,
            "%!FUNC! line:%!LINE! %!LEVEL! Call \"InitApplication\" failure!"
            );
        goto InitFailure;
    }
	// Perform specific initializations
	if (!pThread->InitInstance())
	{
		if (pThread->m_pMainWnd != NULL)
		{
            TraceEvents(
                TRACE_LEVEL_WARNING,
                TRACE_AFXMAIN,
                "%!FUNC! line:%!LINE! %!LEVEL! Destroying non-NULL m_pMainWnd = %p",
                pThread->m_pMainWnd
                );
			pThread->m_pMainWnd->DestroyWindow();
            pThread->m_pMainWnd = NULL;
		}
		nReturnCode = pThread->ExitInstance();
		goto InitFailure;
	}
	nReturnCode = pThread->Run();
InitFailure:
#ifdef _DEBUG
	// Check for missing AfxLockTempMap calls
	if (AfxGetModuleThreadState()->m_nTempMapLock != 0)
	{
        TraceEvents(
            TRACE_LEVEL_WARNING,
            TRACE_AFXMAIN,
            "%!FUNC! line:%!LINE! %!LEVEL! Temp map lock count non-zero (%ld).",
            AfxGetModuleThreadState()->m_nTempMapLock
            );
	}
	AfxLockTempMaps();
	AfxUnlockTempMaps(-1);
#endif
	AfxWinTerm();
    TraceEvents(
        TRACE_LEVEL_INFORMATION,
        TRACE_AFXMAIN,
        "%!FUNC! line:%!LINE! %!LEVEL! Exit with nReturnCode = %!WINERROR!",
        nReturnCode
        );
    return nReturnCode;
}
