// CLcom.cpp : Implementation of DLL Exports.

#include "stdafx.h"
#include "native/resource.h"
#include "gen/CLcom.h"

#include "CLucene.h"
//#include "bindings.h"

class CCLcomModule : public CAtlDllModuleT< CCLcomModule >
{
public :
	DECLARE_LIBID(LIBID_CLcom)
	DECLARE_REGISTRY_APPID_RESOURCEID(IDR_CLCOM, "{5625368B-92FE-411E-8C52-A0F8D4DAACEF}")
};

CCLcomModule _AtlModule;


// DLL Entry Point
extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
	hInstance;

	#ifdef _CLCOMPILER_MSVC
	#ifdef _DEBUG
		if (dwReason == DLL_PROCESS_ATTACH)
			_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );//| _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_CHECK_CRT_DF );
		else if (dwReason == DLL_PROCESS_DETACH){
			_lucene_shutdown(); //clears all static memory
		}
	#endif
	#endif
	//Debuggin techniques:
	//For msvc, use this for breaking on memory leaks: 
	//	_crtBreakAlloc
	//to break at this clucene item:
	//	_lucene_counter_break
	//run a memory check before deleting objects:
	//	_lucene_run_objectcheck
	//if LUCENE_ENABLE_CONSTRUCTOR_LOG is on, dont do log if this is true:
	//	_lucene_disable_debuglogging

    return _AtlModule.DllMain(dwReason, lpReserved); 
}


// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow(void)
{
    return _AtlModule.DllCanUnloadNow();
}


// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    return _AtlModule.DllGetClassObject(rclsid, riid, ppv);
}


// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer(void)
{
    // registers object, typelib and all interfaces in typelib
    HRESULT hr = _AtlModule.DllRegisterServer();
	return hr;
}


// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer(void)
{
	HRESULT hr = _AtlModule.DllUnregisterServer();
	return hr;
}

void _Cnd_OutDebug( const char* FormattedMsg, const char* StrTitle, const char* File, int32_t Line, int32_t Title, const char* Mes2, int32_t fatal ){
	TCHAR* tFormattedMsg = STRDUP_AtoT(FormattedMsg);
	TCHAR* tStrTitle = STRDUP_AtoT(StrTitle);
	MessageBox(NULL, tFormattedMsg, tStrTitle, (fatal==1 ? MB_ICONSTOP:MB_ICONEXCLAMATION) | MB_OK | MB_TASKMODAL);
	_CLDELETE_CARRAY(tFormattedMsg);
	_CLDELETE_CARRAY(tStrTitle);

	if ( fatal )
		debugFatalExit(1);
}
