// atlhelloau.cpp : Implementation of WinMain


// Note: Proxy/Stub Information
//		To build a separate proxy/stub DLL, 
//		run nmake -f atlhelloaups.mk in the project directory.

#include "stdafx.h"
#include "resource.h"
#include "initguid.h"
#include "atlhelloau.h"

#include "atlhelloau_i.c"
#include "AtlHelloDisp.h"


long CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,
    LPARAM lParam)
{
    switch(message) 
    {
    	case WM_DESTROY:
    	{
	   		PostQuitMessage(0);
	   		return 0;
	   	}
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

LONG CExeModule::Unlock()
{
	LONG l = CComModule::Unlock();
	if (l == 0)
	{
#if _WIN32_WINNT >= 0x0400
		if (CoSuspendClassObjects() == S_OK)
			PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
#else
		PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
#endif
	}
	return l;
}

CExeModule _Module;

BEGIN_OBJECT_MAP(ObjectMap)
	OBJECT_ENTRY(CLSID_AtlHelloDisp, CAtlHelloDisp)
END_OBJECT_MAP()


LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
{
	while (*p1 != NULL)
	{
		LPCTSTR p = p2;
		while (*p != NULL)
		{
			if (*p1 == *p++)
				return p1+1;
		}
		p1++;
	}
	return NULL;
}

HWND hMainWnd ;

/////////////////////////////////////////////////////////////////////////////
//
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, 
	HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
	lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
	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);
	_ASSERTE(SUCCEEDED(hRes));
	_Module.Init(ObjectMap, hInstance);
	_Module.dwThreadID = GetCurrentThreadId();
	TCHAR szTokens[] = _T("-/");

	int nRet = 0;
	BOOL bRun = TRUE;
	LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens);
	while (lpszToken != NULL)
	{
		if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
		{
			_Module.UpdateRegistryFromResource(IDR_Atlhelloau, FALSE);
			nRet = _Module.UnregisterServer();
			bRun = FALSE;
			break;
		}
		if (lstrcmpi(lpszToken, _T("RegServer"))==0)
		{
			_Module.UpdateRegistryFromResource(IDR_Atlhelloau, TRUE);
			nRet = _Module.RegisterServer(TRUE);
			bRun = FALSE;
			break;
		}
		lpszToken = FindOneOf(lpszToken, szTokens);
	}

	if (bRun)
	{
		hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, 
			REGCLS_MULTIPLEUSE);
		_ASSERTE(SUCCEEDED(hRes));
			
/*
Register window class
*/

		char *AppName = "EssenceOfOle.AtlAutomationServer" ;
   		WNDCLASS  wc;

   		wc.style		= CS_HREDRAW | CS_VREDRAW;
   		wc.lpfnWndProc	= WndProc;
   		wc.cbClsExtra	= 0;
   		wc.cbWndExtra	= DLGWINDOWEXTRA;
  		wc.hInstance	= hInstance;
		wc.hIcon		= NULL ;	// Loads hello.ico
   		wc.hCursor		= LoadCursor(NULL, IDC_ARROW);	// Normal arrow pointer
   		wc.hbrBackground    = (HBRUSH) (COLOR_APPWORKSPACE+1);
   		wc.lpszMenuName	= NULL;				// No menus.
		wc.lpszClassName= AppName;

   		if(!RegisterClass(&wc))
   		{
   			return FALSE;
   		}

		
/*
Create the main window and show it.
*/
	      
		hMainWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), 0, NULL);
		ShowWindow(hMainWnd, nShowCmd);
	      

		MSG msg;
		while (GetMessage(&msg, 0, 0, 0))
		{
	    	TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		_Module.RevokeClassObjects();
	}

	CoUninitialize();
	return nRet;
}
