/************************************************************************************************************
Methods:
1. Add()		add a system DSN
2. Configure()	configure an existing system DSN
3. Remove()		remove an existing system DSN

Properties:
1. GetErrorReason() reports any errors that occur during the request
2. GetIsError()		returns 1 if any error occur during the request otherwise 0
************************************************************************************************************/

/***********************************/
/*include all required header files*/
/***********************************/
#include <stdio.h>
#include <conio.h>

/************************************************************************/
/*import AdvDirectory COM library here to get its methods and properties*/
/************************************************************************/
#import "C:\AdvDSN.dll" no_namespace

/*************************/
/*prototype for functions*/
/*************************/
void Add();//Add function
void Configure();//Configure function
void Remove();//Remove function

/**************/
/*main program*/
/**************/
void main(void)
{
	char Ch;
	do
	{
		printf("\r\nAdd		[A]");
		printf("\r\nConfigure	[C]");
		printf("\r\nRemove		[R]");
		printf("\r\nQuit		[Q]");
		printf("\r\n\r\nEnter Choice = ");
		scanf("%c", &Ch);
		getchar();
		Ch = toupper(Ch);
		switch (Ch)
		{
			case 'A':Add();break;//call Add function
			case 'C':Configure();break;//call Configure function
			case 'R':Remove();break;//call Remove function
			case 'Q':printf("\r\nGood Bye\r\n");break;
			default: printf("\r\nError: Please enter valid choice\r\n");
		}
	}
	while (Ch != 'Q');
}

/********************************************************/
/*Add function: The function is used to add a system DSN*/
/********************************************************/
void Add()
{
	char m_Driver[256], m_DSN[256];//define charecter buffer of 256 to read driver and dsn connection string
	printf("\r\nEnter Driver Name = ");
	gets(m_Driver);//read driver name
	printf("\r\nEnter DSN Connection String = ");
	gets(m_DSN);//read DSN connection string name
	try//for COM library call, try...catch is used to handle exceptions
	{
		IDSNPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IDSNPtr("AdvDSN.DSN");
		HRESULT hr = obj->Add(m_Driver, m_DSN);//call Add method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Added\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/***************************************************************************/
/*Configure function: The function is used to configure exisintg system DSN*/
/***************************************************************************/
void Configure()
{
	char m_Driver[256], m_DSN[256];//define charecter buffer of 256 to read driver and dsn connection string
	printf("\r\nEnter Driver Name = ");
	gets(m_Driver);//read driver name
	printf("\r\nEnter DSN Connection String = ");
	gets(m_DSN);//read DSN connection string name
	try//for COM library call, try...catch is used to handle exceptions
	{
		IDSNPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IDSNPtr("AdvDSN.DSN");
		HRESULT hr = obj->Configure(m_Driver, m_DSN);//call Configure method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Configured\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/*********************************************************************/
/*Remove function: The function is used to remove existing system DSN*/
/*********************************************************************/
void Remove()
{
	char m_Driver[256], m_DSN[256];//define charecter buffer of 256 to read driver and dsn connection string
	printf("\r\nEnter Driver Name = ");
	gets(m_Driver);//read driver name
	printf("\r\nEnter DSN Connection String = ");
	gets(m_DSN);//read DSN connection string name
	try//for COM library call, try...catch is used to handle exceptions
	{
		IDSNPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IDSNPtr("AdvDSN.DSN");
		HRESULT hr = obj->Remove(m_Driver, m_DSN);//call Remove method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Removed\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}
