⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 command.cpp

📁 windows的snmp api源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//								process the item.
//
//
//      ReturnVal * success		An indication of the success of the operation.
//								Currently values reported are:
//								RET_OK			- success
//								RET_BAD			- failure
//
//  Returns:
//
//      CString * 				A pointer to a CString containing a message
//								to be written in the log. This should never be
//								NULL and the CString object being pointed to
//								should be deleted after use.
//
//============================================================================

CString * CommandItem::DoAdd(SourceItem * srcItem, ReturnVal * success)
{
	CString * retchar = NULL;
	HKEY hkey;
	DWORD createtype = REG_OPENED_EXISTING_KEY;
 	CString keyname;
	keyname.LoadString(IDS_SNMP_AGENT);
	CString DirSep;
	DirSep.LoadString(IDS_DIRSEP);
	keyname += DirSep;
	keyname += *eventSource;


	//	Open the registry at the place where event config is
	//	====================================================

	LONG result = RegOpenKeyEx(hkey_machine, keyname,
								0, KEY_ALL_ACCESS, &hkey);

    if (result != ERROR_SUCCESS)
	{
	
		//	Open or create the event source key to be modified
		//	==================================================

		result = RegCreateKeyEx(hkey_machine, keyname, 0,
								NULL, REG_OPTION_NON_VOLATILE,
								KEY_ALL_ACCESS, NULL, &hkey, &createtype);
		
		if(result != ERROR_SUCCESS)
		{
			retchar = WriteToBuff(IDS_MSG15);
			*success = RET_BAD;
			return retchar;
		}


		//	Make sure this has the correct settings
		//	=======================================

		CString ValueApp;
		DWORD app = 1;
		ValueApp.LoadString(IDS_APPEND);
		result = RegSetValueEx(hkey, ValueApp, 0, REG_DWORD,
								(CONST BYTE *)&app, 4);

		if(result != ERROR_SUCCESS)
		{
			retchar = WriteToBuff(IDS_MSG15);
		}

		CString ValueEntOID;
		ValueEntOID.LoadString(IDS_ENTOID);
		CMyString * entoid = (CMyString *)srcItem->GetEntOID();
		LPTSTR buff2 = entoid->GetBuffer(1);
		DWORD buff2sz = entoid->GetBufferSize();
		result = RegSetValueEx(hkey, ValueEntOID, 0, REG_SZ,
								(CONST BYTE *)buff2, buff2sz);
		entoid->ReleaseBuffer();

		if(result != ERROR_SUCCESS)
		{
			retchar = WriteToBuff(IDS_MSG15);
		}

		if(retchar)
		{
			RegCloseKey(hkey);

			if(createtype == REG_CREATED_NEW_KEY)
				RegDeleteKey(hkey_machine, keyname);

			*success = RET_BAD;
			return retchar;
		}
	}

	DWORD createtype2 = REG_OPENED_EXISTING_KEY;
	char evidstr[34];
	_ultoa(eventID, evidstr, 10); 
	HKEY hkey2;


	//	Open or create the event ID key to be modified
	//	==============================================

	result = RegCreateKeyEx(hkey, evidstr, 0,
							NULL, REG_OPTION_NON_VOLATILE,
							KEY_ALL_ACCESS, NULL, &hkey2, &createtype2);
		
	if(result != ERROR_SUCCESS)
	{
		RegCloseKey(hkey);

		if(createtype == REG_CREATED_NEW_KEY)
			RegDeleteKey(hkey_machine, keyname);
	
		*success = RET_BAD;
		return(WriteToBuff(IDS_MSG15));
	}

	CString ValueCnt;
	ValueCnt.LoadString(IDS_COUNT);

	
	//	Set the count
	//	=============
	
	result = RegSetValueEx(hkey2, ValueCnt, 0, REG_DWORD,
							(CONST BYTE *)&count, 4);

	if(result != ERROR_SUCCESS)
	{
		RegCloseKey(hkey2);

		if(createtype2 == REG_CREATED_NEW_KEY)
			RegDeleteKey(hkey, evidstr);

		RegCloseKey(hkey);

		if(createtype == REG_CREATED_NEW_KEY)
			RegDeleteKey(hkey_machine, keyname);

		*success = RET_BAD;
		return(WriteToBuff(IDS_MSG15));
	}

	CString ValueT;
	ValueT.LoadString(IDS_TIME);


	//	If necessary, set the time
	//	==========================

	if ((count > 1) && time)
	{
		result = RegSetValueEx(hkey2, ValueT, 0, REG_DWORD,
								(CONST BYTE *)&time, 4);
		if(result != ERROR_SUCCESS)
		{
			RegCloseKey(hkey2);
		
			if(createtype2 == REG_CREATED_NEW_KEY)
				RegDeleteKey(hkey, evidstr);

			RegCloseKey(hkey);

			if(createtype == REG_CREATED_NEW_KEY)
				RegDeleteKey(hkey_machine, keyname);

			*success = RET_BAD;
			return(WriteToBuff(IDS_MSG15));
		}
	}
	else
		RegDeleteValue(hkey2, ValueT);

	CString ValueID;
	ValueID.LoadString(IDS_FULLID);

	
	//	Set the fullid value
	//	====================
	
	result = RegSetValueEx(hkey2, ValueID, 0, REG_DWORD,
							(CONST BYTE *)&eventID, 4);

	if(result != ERROR_SUCCESS)
	{
		RegCloseKey(hkey2);

		if(createtype2 == REG_CREATED_NEW_KEY)
			RegDeleteKey(hkey, evidstr);

		RegCloseKey(hkey);

		if(createtype == REG_CREATED_NEW_KEY)
			RegDeleteKey(hkey_machine, keyname);

		*success = RET_BAD;
		return(WriteToBuff(IDS_MSG15));
	}
	
	RegCloseKey(hkey);
	RegCloseKey(hkey2);

	return (WriteToBuff(IDS_MSG16));
}


//============================================================================
//  CommandItem::WriteToBuff
//
//  This public method is called to format a message to be written to the log.
//	It's only parameter is the resource id of a string to be appended to the
//	command line from which this CommandItem was built.
//
//
//  Parameters:
//
//      UINT mssgid  	A pointer to a structure containing additional
//								information about this command. This needed to
//								process the item.
//
//  Returns:
//
//      CString * 		A pointer to a CString containing a message
//						to be written in the log. This should never be
//						NULL and the CString object being pointed to
//						should be deleted after use.
//
//============================================================================

CString * CommandItem::WriteToBuff(UINT mssgid)
{
	CString * s = new CString;
	CString temp;
	temp.LoadString(IDS_PRAGMA);
	CString tab;
	tab.LoadString(IDS_TAB);
	CString quote;
	quote.LoadString(IDS_QUOTE);


	//	First build the command line that is CommandItem was made from
	//	==============================================================
	
	*s += temp;
	*s += tab;
	temp.Empty();
	BOOL trap = FALSE;
	
	switch (command)
	{
		case Add:
		{
			temp.LoadString(IDS_ADD);
			break;
		}

		case Delete:
		{
			temp.LoadString(IDS_DEL);
			break;
		}

		case AddTrap:
		{
			trap = TRUE;
			temp.LoadString(IDS_ADDTRAP);
			break;
		}

		case DeleteTrap:
		{
			trap = TRUE;
			temp.LoadString(IDS_DELTRAP);
			break;
		}

		default:  //huh!?!
			break;
	}
	
	*s += temp;
	*s += tab;
	temp.Empty();

	*s += quote;
	*s += *eventLog;
	*s += quote;
	*s += tab;
	*s += quote;
	*s += *eventSource;
	*s += quote;

	if (!trap)	//not a trap command, has eventid, count and time
	{
		*s += tab;
		char tmp[34];
		_ultoa(eventID, tmp, 10); 
		*s += tmp;

		if (count)
		{
			*s += tab;
			char tmp[34];
			_ultoa(count, tmp, 10); 
			*s += tmp;

			if (time)
			{
				*s += tab;
				char tmp[34];
				_ultoa(time, tmp, 10); 
				*s += tmp;
			}
		}
	}
	

	//	Add the message from the resource specified by this method's parameter
	//	======================================================================

	if (mssgid)
	{
		CString messg;
		messg.LoadString(mssgid);
		*s += tab;
		*s += messg;
	}

	CString NL;

#ifdef EVENTCMT_OLD_LOG

	NL.LoadString(IDS_LFCR);

#else	//EVENTCMT_OLD_LOG

	NL.LoadString(IDS_NL);

#endif	//EVENTCMT_OLD_LOG

	*s += NL;

	
	//	Return the CString object that we've built
	//	==========================================
	
	return s;
}


//============================================================================
//  CommandItem::~CommandItem
//
//  This is the CommandItem class's only desstructor. It frees the memory used
//	to the two CString members	eventLog and eventSource. (These are the same
//	as commname and address for the  TrapCommandItem so they are not deleted in
//	the TrapCommandItem destructor.)
//
//
//  Parameters:
//
//      none
//
//  Returns:
//
//      none
//
//============================================================================

CommandItem::~CommandItem()
{
	delete eventLog;
	delete eventSource;	
}


//============================================================================
//  CommandItemQueue::~CommandItemQueue
//
//  This is the CommandItemQueue class's only desstructor. If there are any
//	items in the queue they are deleted.
//
//
//  Parameters:
//
//      none
//
//  Returns:
//
//      none
//
//============================================================================

CommandItemQueue::~CommandItemQueue()
{
	while(!IsEmpty())
	{
		CommandItem * com = RemoveHead();
		delete com;
	}
}


//============================================================================
//  SourceItem::SourceItem
//
//  This is the SourceItem class's only constructor. This class is derived
//	from the CObject class. This is so the MFC template storage classes can be
//	used. It is used to store and obtain event source information so that SNMP
//	trap translation configuration requests may be processed.
//
//
//  Parameters:
//
//		CString * src	The event source name.
//
//		HKEY key		The registry key HKEY_LOCAL_MACHINE for the	machine
//						that is being configured.
//
//		BOOL E			A boolean indicating whether this Source is valid.
//
//
//  Returns:
//
//      none
//
//============================================================================

SourceItem::SourceItem(CString * src, HKEY key, BOOL E)
{
	EntOID = NULL;
	hkey_machine = key;

	if(src)	
	{
		source = new CString(*src);
	}
	else
		source = NULL;

	
	//	This sets the member Exists variable and if necessary creates an OID for the source
	//	===================================================================================
	
	SetExists(E);
}


//============================================================================
//  SourceItem::SetExists
//
//  This public method is called to set the Exist member variable. It takes
//	one parameter, the value to be set. If needed, it creates an OID string
//	for the event source.
//
//
//  Parameters:
//
//		BOOL E			A boolean indicating whether this Source is valid.
//
//
//  Returns:
//
//      none
//
//============================================================================

void SourceItem::SetExists(BOOL E)
{

	//	delete the old OID if there was one
	//	===================================

	if (EntOID)
		delete EntOID;

	
	//	if needed, create a new OID
	//	===========================

	if (E)
	{
		CString OID;
		_ultoa(source->GetLength(), OID.GetBuffer(20), 10);
		OID.ReleaseBuffer();
		OID += TEXT(".");
		char * c = source->GetBuffer(1);

		while (c && (*c != '\0'))
		{
			int a = *c;
			char str[34];
			_ultoa(a, str, 10); 
			OID += str;
			OID += TEXT(".");
			c++;
		}

		source->ReleaseBuffer();
		EntOID = new CString(OID);
	}
	else
		EntOID = NULL;

	
	//	set the Exists member
	//	=====================

	Exists = E;
}

#ifdef EVENTCMT_VALIDATE_ID


//============================================================================
//  SourceItem::GetDllName
//
//  This public method is called to get the name of the message dll for this
//	event source.
//
//
//  Parameters:
//
//		CommandItem* Cmnd	A pointer to a CommandItem which has this event
//							source
//
//
//  Returns:
//
//      CString *			A pointer to a CString containing the dll name
//
//============================================================================

CString * SourceItem::GetDllName(CommandItem* Cmnd)
{

	//	Open the registry and get the key for the event source specified
	//	================================================================

	CString * Dll = NULL;
	HKEY hkeyOpen;
	CString keyName;
	CString keyValue;
	CString DirSep;
	DirSep.LoadString(IDS_DIRSEP);
	DWORD valType;
	char * buff;
	char * buff2;
	DWORD buffsz = 0;

	keyName.LoadString(IDS_KEYNAME);
	CString * evlog = Cmnd->GetEventLog(); 
	keyName += *evlog;
	keyName += DirSep; 
	CString * evsrc = Cmnd->GetEventSource(); 
	keyName += *evsrc;


	//	Get the key to the source from the registry
	//	===========================================

	LONG Result = RegOpenKeyEx(hkey_machine, keyName,
								0, KEY_ALL_ACCESS, &hkeyOpen);

    if (Result != ERROR_SUCCESS)
        return Dll;

	
	//	Get the name and path to the message dll
	//	========================================

	keyValue.LoadString(IDS_VALUENAME);
	Result = RegQueryValueEx(hkeyOpen, keyValue, NULL, &valType, (unsigned char*)buff, &buffsz);

    if (!buffsz)//(Result != ERROR_MORE_DATA)
	{
		RegCloseKey(hkeyOpen);        
        return Dll;
	}

	
	//	first we found the size of the buffer needed, now get the name of the dll
	//	=========================================================================

	buff = new char[buffsz + 1];
	Result = RegQueryValueEx(hkeyOpen, keyValue, NULL, &valType, (unsigned char*)buff, &buffsz);
	RegCloseKey(hkeyOpen);        
    
    if (Result != ERROR_SUCCESS)
	{
		delete buff;
		return Dll;
	}
	
	buff2 = new char[1];
	
	
	//	Remove any environment strings (e.g. %SystemRoot%)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -