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

📄 command.cpp

📁 windows的snmp api源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	
	DWORD length = ExpandEnvironmentStrings(buff, buff2, 1);
	
	if (!length)
	{
		delete buff;
		return Dll;
	}

	delete buff2;
	buff2 = new char[length + 1];
	buffsz = ExpandEnvironmentStrings(buff, buff2, length);

	delete buff;

	if(!buffsz)
	{
		delete buff2;
		return Dll;
	}
	
	
	//	Create the CString for the return
	//	=================================
	
	Dll = new CString(buff2);
	delete buff2;
	return Dll;
}


//============================================================================
//  SourceItem::EnumerateEventIDMap
//
//  This public method is called to get read and enumerate the message dll.
//	This is so EventId values may be validated.
//
//
//  Parameters:
//
//		CommandItem* Cmnd	A pointer to a CommandItem which has this event
//							source
//
//
//  Returns:
//
//      BOOL				An indication of the success of this method. FALSE
//							if there was an error.
//
//============================================================================

BOOL SourceItem::EnumerateEventIDMap(CommandItem* Cmnd)
{
	CString * dllname;
	dllname = GetDllName(Cmnd);

	if (!dllname)
		return FALSE;

    
	//	Load the message dll
	//	====================

    HINSTANCE hInstMsgFile = LoadLibraryEx(*dllname, NULL, 
        LOAD_LIBRARY_AS_DATAFILE);
	delete dllname;

    if (hInstMsgFile == NULL)
        return FALSE;
  
    
	//	Enumerate the message dll
	//	=========================
    
    if (EnumResourceNames(hInstMsgFile, RT_MESSAGETABLE, 
        (ENUMRESNAMEPROC)ProcessMsgTable, (LONG)this) == FALSE)
    {
        FreeLibrary(hInstMsgFile);
        return FALSE;
    }

    FreeLibrary(hInstMsgFile);
    return TRUE;
}


//============================================================================
//  BOOL CALLBACK ProcessMsgTable
//
//  This callback function is used to read and process an open message dll for
//	event IDs. See WINDOWS API for EnumResourceNames and EnumResNameProc.
//
//============================================================================

BOOL CALLBACK ProcessMsgTable(HANDLE hModule, LPCTSTR lpszType,
    LPTSTR lpszName, LONG lParam)
{

    SourceItem* src = (SourceItem*)(LPVOID)lParam;


    //	Found a message table.  Process it!
	//	===================================

    HRSRC hResource = FindResource((HINSTANCE)hModule, lpszName,
        RT_MESSAGETABLE);
    if (hResource == NULL)
        return TRUE;

    HGLOBAL hMem = LoadResource((HINSTANCE)hModule, hResource);
    if (hMem == NULL)
        return TRUE;

    PMESSAGE_RESOURCE_DATA pMsgTable =
        (PMESSAGE_RESOURCE_DATA)::LockResource(hMem);
    if (pMsgTable == NULL)
        return TRUE;

    ULONG ulBlock, ulId, ulOffset;

    for (ulBlock=0; ulBlock<pMsgTable->NumberOfBlocks; ulBlock++)
    {
        ulOffset = pMsgTable->Blocks[ulBlock].OffsetToEntries;
        for (ulId = pMsgTable->Blocks[ulBlock].LowId;
            ulId <= pMsgTable->Blocks[ulBlock].HighId; ulId++)
                
        {
            src->eventIDs.SetAt((DWORD)ulId, 0);
        }
    }

    return TRUE;
}

#endif //EVENTCMT_VALIDATE_ID


//============================================================================
//  SourceItem::~SourceItem
//
//  This is the SourceItem class's only desstructor. It frees any memory
//	to the source name, the OID and the map of valid eventids.
//
//
//  Parameters:
//
//      none
//
//  Returns:
//
//      none
//
//============================================================================

SourceItem::~SourceItem()
{
	if (source)
		delete source;

	if (EntOID)
		delete EntOID;

#ifdef EVENTCMT_VALIDATE_ID
	eventIDs.RemoveAll();
#endif //EVENTCMT_VALIDATE_ID

}


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

SourceList::~SourceList()
{
	while(!IsEmpty())
	{
		SourceItem * src = RemoveHead();
		delete src;
	}
}


//============================================================================
//  SourceList::FindItem
//
//  This public method is called to find a particular SourceItem in this
//	SourceList. The search is done by name.
//
//
//  Parameters:
//
//		CString * src	A pointer to a CString which has the event source
//						name being sought.
//
//		SourceItem * i	A pointer to the source item if it was found. This is
//						NULL if the item wasn't found.
//
//
//  Returns:
//
//      int				An indication of the success of this method. This is
//						one of three values:
//						RET_OK			- The item is a valid event source
//						RET_BAD			- The item is an invalid event source
//						RET_NOT_FOUND	- The item was not found
//
//============================================================================

int SourceList::FindItem(CString * src, SourceItem *& i)
{
	POSITION p = GetHeadPosition();
	ReturnVal ret = RET_NOTFOUND;
	i = NULL;


	//	Step through the list searching for the event source
	//	====================================================
		
	while (src && p) 
	{
		SourceItem * item = GetNext(p);
		CString * temp = item->GetSource();

		if(*temp == *src)
		{
			
			//	The item has been found!
			//	========================
			
			i = item;

			if(item->GetExists())
				ret = RET_OK;	//valid event source
			else
				ret = RET_BAD;	//invalid event source

			break;
		}
	}

	return ret;
}


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

UniqueList::~UniqueList()
{
	while(!IsEmpty())
	{
		ListItem * item = RemoveHead();
		delete item;
	}
}


//============================================================================
//  UniqueList::FindItem
//
//  This public method is called to find a particular ListItem in this
//	UniqueList. The search is done by comparing the contents of the ListItem
//	pointed to by the first parameter (using ListItem::CompareFunc).
//
//
//  Parameters:
//
//		ListItem * item		A pointer to a ListItem to be found. 
//
//		int dummy			A dummy variable to distinguish this overloaded
//							function.
//
//  Returns:
//
//      ListItem *			A pointer to the item found in the list. NULL if
//							the item is not found.
//
//============================================================================

ListItem * UniqueList::FindItem(ListItem * item, int dummy)
{
	
	//	Reset the list and step through it trying to find the item
	//	==========================================================	
	
	POSITION p = GetHeadPosition();
	ListItem * ret = NULL;

	while (item && p) 
	{
		ListItem * i = GetNext(p);

		if(i->CompareFunc(item) == 0)
		{
			ret = i;	//found it!
			break;
		}
	}

	return ret;
}


//============================================================================
//  UniqueList::FindItem
//
//  This public method is called to find a particular ListItem in this
//	UniqueList. The search is done by comparing the contents of the ListItem
//	pointed to by the first parameter (using ListItem::CompareFunc).
//
//
//  Parameters:
//
//		ListItem * item		A pointer to a ListItem to be found. 
//
//  Returns:
//
//      POSITION			The POSITION of the item in the list. NULL if the
//							item is not found.
//
//============================================================================

POSITION UniqueList::FindItem(ListItem * item)
{
	POSITION p = GetHeadPosition();
	POSITION ret = p;

	if (!item)
		return NULL;

	
	//	Reset the list and step through it trying to find the item
	//	==========================================================	
	
	while (item && p) 
	{
		ret = p;  //the current position
		ListItem * i = GetNext(p);

		if(i->CompareFunc(item) == 0)
		{
			//the current position will be returned
			break;
		}
		
		ret = p; //if not found, p will be null and be returned.
	}

	return ret;
}


//============================================================================
//  UniqueList::Add
//
//  This public method is called to add a ListItem into this UniqueList. The
//	ListItem is not added if it is already present in the list. If this is the
//	case a pointer to the matching item in the list is returned.
//
//
//  Parameters:
//
//		ListItem * newItem	A pointer to the ListItem to be added. 
//
//  Returns:
//
//      ListItem *			A pointer to the item if is in the list. NULL if
//							the item was not found and was added.
//
//============================================================================

ListItem * UniqueList::Add(ListItem* newItem)
{
	ListItem * ret = NULL;
	ret = FindItem(newItem, 0);

	if(ret)
		return ret;
	else
		AddTail(newItem);

	return ret;
}


//============================================================================
//  ListItem::ListItem
//
//  This is the ListItem 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 a CString value in a list.
//
//
//  Parameters:
//
//      CString * str  	A pointer to the CString object to be stored.
//
//  Returns:
//
//      none
//
//============================================================================

ListItem::ListItem(CString * str)
{
	string = str;
}


//============================================================================
//  ListItem::~ListItem
//
//  This is the ListItem class's only destructor. It frees the memory used
//	to store the CString members.
//
//
//  Parameters:
//
//      none
//
//  Returns:
//
//      none
//
//============================================================================

ListItem::~ListItem()
{
	delete string;
}


//============================================================================
//  ListItem::CompareFunc
//
//  This public method is used to compare the contents of the ListItem pointed
//	to by the first parameter to the contents of this ListItem.
//
//
//  Parameters:
//
//      ListItem * item		A pointer to the ListItem whose contents will be
//							compared to this ListItem's contents.
//
//  Returns:
//
//      int					An indication of the outcome of the comparison. 0
//							if there is a match, 1 otherwise.
//
//============================================================================

int ListItem::CompareFunc(ListItem * item)
{
	CString * temp = item->GetString();

	if(*temp == *string)
		return 0;
	else
		return 1;
}


//============================================================================
//  CommListItem::CommListItem
//
//  This is the CommListItem class's only constructor. This class is derived
//	from the ListItem class. It is basically a ListItem with a ListItem member
//	as well as a CString. It is used to store a CString value and a list of
//	associated CStrings in a list (a list of lists). This class is used to
//	store the SNMP trap destination registry image. The CString being a
//	community name and the list of CStrings being the destination addresses. A
//	special destructor is not needed as the ListItem destructor will be used
//	once for each member in the list and is adequate. 
//
//
//  Parameters:
//
//      CString * str  	A pointer to the CString object to be stored.
//
//  Returns:
//
//      none
//
//============================================================================

CommListItem::CommListItem(CString * str)
			:ListItem(str)
{
//nothing to do
}


//============================================================================
//  CommListItem::GetAddresses
//
//  This public method is called to get the trap destination addresses for the
//	community name that is stored in this object's CString member.
//
//
//  Parameters:
//
//      HKEY * hkey  	A pointer to the open registry key of the SNMP trap
//						destination root key.
//
//  Returns:
//
//      BOOL 			A boolean indicating the success of the method. TRUE
//						if there were no errors, FALSE otherwise.
//
//============================================================================

BOOL CommListItem::GetAddresses(HKEY * hkey)
{
	HKEY hkeyOpen;

	CString *temp = GetString();

	//	Open the registry key for this community name
	//	=============================================
	LONG Result = RegOpenKeyEx(*hkey, *temp,
								0, KEY_READ, &hkeyOpen);
    
    if (Result != ERROR_SUCCESS)
        return FALSE;

    char Name[1024 + 1];
    DWORD NameLength;
    char Data[1024 + 1];
    DWORD DataLength;
    DWORD type;

    int i = 0;


	//	Enumerate all the values (addresses) under this key
	//	===================================================

    while (TRUE)
    {
        NameLength = 1024 + 1;
        DataLength = 1024 + 1;
        Result = RegEnumValue(hkeyOpen, i, Name, &NameLength, NULL,
            					&type, (unsigned char*)Data, &DataLength);
        
        if (Result != ERROR_SUCCESS)
            break;
        
        if ((NameLength > 0) && (DataLength > 0) && (type == REG_SZ)) 
		{
			CString * str = new CString(Data); 
			ListItem * item = new ListItem(str);
			
			if(addresses.Add(item)) //shouldn't be duplicates but check anyway
				delete item;
        }
        
        i++;
    }

	RegCloseKey(hkeyOpen);


    //	Did we find a normal end condition?
	//	===================================
    if (Result == ERROR_NO_MORE_ITEMS)
        return TRUE;

    return FALSE;

}

⌨️ 快捷键说明

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