// AutoCadTestApp.cpp : Initialization functions
#include "StdAfx.h"
#include "StdArx.h"
#include "resource.h"

HINSTANCE _hdllInstance =NULL ;
 
// This command registers an ARX command.
void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
				const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal = -1);


// NOTE: DO NOT edit the following lines.
//{{AFX_ADS_FUNC_TABLE
typedef struct {
	char    *name;
	int     (*fptr)();
	BOOL    regFunc;
	BOOL    renderCmd;
} ftblent;

#define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))

ftblent exfun[] = {
	{"", NULL, FALSE, FALSE },
}; //}}AFX_ADS_FUNC_TABLE


//{{AFX_ARX_MSG
void InitApplication();
void UnloadApplication();
//}}AFX_ARX_MSG

// NOTE: DO NOT edit the following lines.
//{{AFX_ARX_ADDIN_FUNCS
//}}AFX_ARX_ADDIN_FUNCS


/////////////////////////////////////////////////////////////////////////////
// DLL Entry Point
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
	if (dwReason == DLL_PROCESS_ATTACH)
	{
        _hdllInstance = hInstance;
	} else if (dwReason == DLL_PROCESS_DETACH) {

	}
	return TRUE;    // ok
}



/////////////////////////////////////////////////////////////////////////////
// ObjectARX EntryPoint
extern "C" AcRx::AppRetCode 
acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
{
	switch (msg) {
	case AcRx::kInvkSubrMsg:
		dofun();
		break;
	case AcRx::kUnloadDwgMsg:
		funcunload();
		break;
	case AcRx::kLoadDwgMsg:
		funcload();
		break;
	case AcRx::kInitAppMsg:
		// Comment out the following line if your
		// application should be locked into memory
		acrxDynamicLinker->unlockApplication(pkt);
		acrxDynamicLinker->registerAppMDIAware(pkt);
		InitApplication();
		break;
	case AcRx::kUnloadAppMsg:
		UnloadApplication();
		break;
	}
	return AcRx::kRetOK;
}

// Init this application. Register your
// commands, reactors...
void InitApplication()
{
	// NOTE: DO NOT edit the following lines.
	//{{AFX_ARX_INIT
	AddCommand("S4", "TEST", "TEST", ACRX_CMD_TRANSPARENT | ACRX_CMD_NOINTERNALLOCK, S4TEST);
	//}}AFX_ARX_INIT

	// TODO: add your initialization functions

}

// Unload this application. Unregister all objects
// registered in InitApplication.
void UnloadApplication()
{
	// NOTE: DO NOT edit the following lines.
	//{{AFX_ARX_EXIT
	acedRegCmds->removeGroup("S4");
	//}}AFX_ARX_EXIT

	// TODO: clean up your application
}

// This functions registers an ARX command.
// It can be used to read the localized command name
// from a string table stored in the resources.
void AddCommand(const char* cmdGroup, const char* cmdInt, const char* cmdLoc,
				const int cmdFlags, const AcRxFunctionPtr cmdProc, const int idLocal)
{
	char cmdLocRes[65];

	// If idLocal is not -1, it's treated as an ID for
	// a string stored in the resources.
	if (idLocal != -1) {

		// Load strings from the string table and register the command.
		::LoadString(_hdllInstance, idLocal, cmdLocRes, 64);
		acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLocRes, cmdFlags, cmdProc);

	} else
		// idLocal is -1, so the 'hard coded'
		// localized function name is used.
		acedRegCmds->addCommand(cmdGroup, cmdInt, cmdLoc, cmdFlags, cmdProc);
}

/////////////////////////////////////////////////////////////////////
// funcload(internal)
// This function is called to define all function names in the ADS
// function table.  Each named function will be callable from lisp or
// invokable from another ADS application.
/////////////////////////////////////////////////////////////////////
static int funcload(void)
{
	int i;

	for (i = 1; i < ELEMENTS(exfun); i++) {
		if (!acedDefun(exfun[i].name, i))
			return RTERROR;
		if (exfun[i].regFunc)
			acedRegFunc(exfun[i].fptr, i);
	}

	return RTNORM;
}

/////////////////////////////////////////////////////////////////////
// funcunload(internal)
// This function is called to undefine all function names in the ADS
// function table.  Each named function will be removed from the
// AutoLISP hash table.
/////////////////////////////////////////////////////////////////////
static int funcunload(void)
{
	int i;

	// Undefine each function we defined

	for (i = 1; i < ELEMENTS(exfun); i++) {
		acedUndef(exfun[i].name,i);
	}

	return RTNORM;
}

/////////////////////////////////////////////////////////////////////
// dofun(internal)
// This function is called to invoke the function which has the
// registerd function code that is obtained from acedGetFunCode.
/////////////////////////////////////////////////////////////////////
static int dofun(void)
{
	int val,
	    rc;

	acedRetVoid();

	if ((val = acedGetFunCode()) < 1 || val > ELEMENTS(exfun))
		return RTERROR;

#ifdef RENDER
	if (exfun[val].renderCmd)
		if (!InitRender(false))
			return RTERROR;
#endif

	rc = (*exfun[val].fptr)();

	return ((rc == RTNORM) ? RSRSLT:RSERR);
}

