// wmsoapsvc.cpp : Implementation of WinMain

#include "stdafx.h"
#include "resource.h"
#include "wmsoapsvc.h"
#include <stdio.h>
#include "..\soap_source\wmsoaplistener.h"

class WMSOAPListener;

HRESULT CwmsoapsvcModule::Run(int nShowCmd)
{
	HRESULT hr = S_OK;

	hr = PreMessageLoop(nShowCmd);

	if (hr == S_OK)
	{
		if (m_bService)
		{
			LogEvent(_T("Service started"));
			SetServiceStatus(SERVICE_RUNNING);
		    // create server object
		    m_pSOAPListener = new WMSOAPListener();
		    if (m_pSOAPListener != NULL)
		    {
			    m_pSOAPListener->Start();
		    }
		}

		RunMessageLoop();
	}

	if (SUCCEEDED(hr))
	{
		hr = PostMessageLoop();
	}

	return hr;
}

HRESULT CwmsoapsvcModule::PreMessageLoop(int nShowCmd)
{
    HRESULT hr = __super::PreMessageLoop(nShowCmd);
// this is a workaround for a bug in ATL 7.0
// see http://www.faqchest.com/msdn/ATL/atl-02/atl-0204/atl-020400/atl02040105_09977.html 
#if _ATL_VER == 0x0700
    if (SUCCEEDED(hr) && !m_bDelayShutdown)
        hr = CoResumeClassObjects();
#endif

    if (SUCCEEDED(hr))
    {
        m_pSOAPListener = NULL;
    }

    return hr;
}

HRESULT CwmsoapsvcModule::PostMessageLoop()
{
	if (m_bService)
	{
		// delete server
		if (m_pSOAPListener != NULL)
		{
			delete m_pSOAPListener;
			m_pSOAPListener = NULL;
		}
	}
    return __super::PostMessageLoop();
}

CwmsoapsvcModule _AtlModule;

//
extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, 
                                LPTSTR /*lpCmdLine*/, int nShowCmd)
{
    return _AtlModule.WinMain(nShowCmd);
}

