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

📄 snmpelpt.cpp

📁 windows的snmp api源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
{
	LONG	status; 					// registry read results
	HKEY	hkResult;					// handle returned from API
	DWORD	iValue; 					// temporary counter
	DWORD	dwType; 					// type of the parameter read
	DWORD	nameSize;					// length of parameter name
	DWORD	nReadBytes = 0; 			// number of bytes read from profile information
	LPTSTR	lpszSourceKey;				// temporary string for registry source key
	LPTSTR	lpszEventKey;				// temporary string for registry event key
	TCHAR	temp[2*MAX_PATH+1]; 		// temporary string

	WriteTrace(0x0a,"GetRegistryValue: Entering GetRegistryValue function\n");

	if (fThresholdEnabled && fThreshold)
	{
		WriteTrace(0x0a,"GetRegistryValue: Performance threshold flag is on. No data will be processed.\n");
		WriteTrace(0x0a,"GetRegistryValue: Exiting GetRegistryValue function with FALSE\n");
		return(FALSE);
	}

	if ( (lpszSourceKey = (LPTSTR) SNMP_malloc(strlen(EXTENSION_SOURCES)+strlen(sourceName)+2)) == NULL )
	{
		WriteTrace(0x14,"GetRegistryValue: Unable to allocate registry source key storage. Trap not sent.\n");
		WriteTrace(0x0a,"GetRegistryValue: Exiting GetRegistryValue function with FALSE\n");
		return(FALSE);
	}

	if ( (lpszEventKey = (LPTSTR) SNMP_malloc(strlen(EXTENSION_SOURCES)+strlen(sourceName)+strlen(eventID)+3)) == NULL )
	{
		WriteTrace(0x14,"GetRegistryValue: Unable to allocate registry event key storage. Trap not sent.\n");
		SNMP_free(lpszSourceKey);
		WriteTrace(0x0a,"GetRegistryValue: Exiting GetRegistryValue function with FALSE\n");
		return(FALSE);
	}

	strcpy(lpszSourceKey,EXTENSION_SOURCES);	// start with root
	strcat(lpszSourceKey,sourceName);			// append the source name
	strcpy(lpszEventKey,lpszSourceKey); 		// build prefix for event key
	strcat(lpszEventKey,TEXT("\\"));			// add the backslash
	strcat(lpszEventKey,eventID);				// complete it with the event ID

	WriteTrace(0x00,"GetRegistryValue: Opening registry key for %s\n",lpszEventKey);

	if ((status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszEventKey, 0,
		(KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS), &hkResult))
		!= ERROR_SUCCESS)					// open registry information
	{
		WriteTrace(0x00,"GetRegistryValue: No registry entry exists for %s. RegOpenKeyEx returned %lu\n",
			lpszEventKey, status);
		SNMP_free(lpszSourceKey);			// free storage
		SNMP_free(lpszEventKey);				// free storage
		WriteTrace(0x0a,"GetRegistryValue: Exiting GetRegistryValue function with FALSE\n");
		return(FALSE);					// show nothing exists
	}

	nameSize = sizeof(iValue);			// set field length
	if ( (status = RegQueryValueEx( 	// look up count
		hkResult,						// handle to registry key
		EXTENSION_COUNT,				// key to look up
		0,								// ignored
		&dwType,						// address to return type value
		(LPBYTE) &iValue,				// where to return count field
		&nameSize) ) != ERROR_SUCCESS)	// size of count field
	{
		WriteTrace(0x00,"GetRegistryValue: No registry entry exists for %s. RegOpenKeyEx returned %lu\n",
			EXTENSION_COUNT, status);
		regStruct->nCount = 0;			// set default value
	}
	else
	{
		regStruct->nCount = iValue; 	// save returned value
		WriteTrace(0x00,"GetRegistryValue: Count field is %lu\n", regStruct->nCount);
	}

	if ( (status = RegQueryValueEx( 	// look up local trim
		hkResult,						// handle to registry key
		EXTENSION_TRIM, 				// key to look up
		0,								// ignored
		&dwType,						// address to return type value
		(LPBYTE) &iValue,				// where to return count field
		&nameSize) ) != ERROR_SUCCESS)	// size of count field
	{
		WriteTrace(0x00,"GetRegistryValue: No registry entry exists for %s. RegOpenKeyEx returned %lu\n",
			EXTENSION_TRIM, status);
		WriteTrace(0x00,"GetRegistryValue: Using default of global trim message flag of %lu\n",
			fGlobalTrim);
		regStruct->fLocalTrim = fGlobalTrim;	// set default value
	}
	else
	{
		regStruct->fLocalTrim = ((iValue == 1) ? TRUE : FALSE); // save returned value
		WriteTrace(0x00,"GetRegistryValue: Local message trim field is %lu\n", regStruct->fLocalTrim);
	}

	if ( (status = RegQueryValueEx( 	// look up time
		hkResult,						// handle to registry key
		EXTENSION_TIME, 				// key to look up
		0,								// ignored
		&dwType,						// address to return type value
		(LPBYTE) &iValue,				// where to return time field
		&nameSize) ) != ERROR_SUCCESS)	// size of time field
	{
		WriteTrace(0x00,"GetRegistryValue: No registry entry exists for %s. RegOpenKeyEx returned %lu\n",
			EXTENSION_TIME, status);
		regStruct->nTime = 0;			// set default value
	}
	else
	{
		regStruct->nTime = iValue;		// save returned value
		WriteTrace(0x00,"GetRegistryValue: Time field is %lu\n", regStruct->nTime);
	}

	RegCloseKey(hkResult);				// close registry key for event
	SNMP_free(lpszEventKey);					// free the storage for the event key

	WriteTrace(0x00,"GetRegistryValue: Opening registry key for %s\n",lpszSourceKey);

	if ((status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSourceKey, 0,
		(KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS), &hkResult))
		!= ERROR_SUCCESS)					// open registry information
	{
		WriteTrace(0x00,"GetRegistryValue: No registry entry exists for %s. RegOpenKeyEx returned %lu\n",
			lpszSourceKey, status);
		SNMP_free(lpszSourceKey);			// free storage
		WriteTrace(0x0a,"GetRegistryValue: Exiting GetRegistryValue function with FALSE\n");
		return(FALSE);					// show nothing exists
	}

	nameSize = sizeof(regStruct->szOID)-1;	// set field length
	if ( (status = RegQueryValueEx( 	// look up EnterpriseOID
		hkResult,						// handle to registry key
		EXTENSION_ENTERPRISE_OID,		// key to look up
		0,								// ignored
		&dwType,						// address to return type value
		(LPBYTE) regStruct->szOID,		// where to return OID string field
		&nameSize) ) != ERROR_SUCCESS)	// size of OID string field
	{
		WriteTrace(0x00,"GetRegistryValue: No registry entry exists for %s. RegOpenKeyEx returned %lu\n",
			EXTENSION_ENTERPRISE_OID, status);
		SNMP_free(lpszSourceKey);			// free storage
		RegCloseKey(hkResult);			// close the registry key
		WriteTrace(0x0a,"GetRegistryValue: Exiting GetRegistryValue function with FALSE\n");
		return(FALSE);					// indicate error
	}

	WriteTrace(0x00,"GetRegistryValue: EnterpriseOID field is %s\n", regStruct->szOID);

	nameSize = sizeof(iValue);			// set field length
	if ( (status = RegQueryValueEx( 	// look up time
		hkResult,						// handle to registry key
		EXTENSION_APPEND,				// key to look up
		0,								// ignored
		&dwType,						// address to return type value
		(LPBYTE) &iValue,				// where to return time field
		&nameSize) ) != ERROR_SUCCESS)	// size of time field
	{
		WriteTrace(0x00,"GetRegistryValue: No registry entry exists for %s. RegOpenKeyEx returned %lu\n",
			EXTENSION_APPEND, status);
		regStruct->fAppend = TRUE;		// default to true
	}
	else
	{
		regStruct->fAppend = ((iValue == 1) ? TRUE : FALSE);		// reflect append flag
		WriteTrace(0x00,"GetRegistryValue: Append field is %lu\n", regStruct->fAppend);
	}

	RegCloseKey(hkResult);				// close registry key for source
	SNMP_free(lpszSourceKey);				// free the storage for the source key

	if (regStruct->fAppend)
	{
		strcpy(temp,regStruct->szOID);							// copy enterprise suffix temporarily
		strcpy(regStruct->szOID, szBaseOID);					// copy base enterprise oid first
		strcpy(regStruct->szOID+strlen(szBaseOID), TEXT("."));	// add the .
		strcpy(regStruct->szOID+strlen(szBaseOID)+1, temp); 	// now add the suffix
		WriteTrace(0x0a,"GetRegistryValue: Appended enterprise OID is %s\n", regStruct->szOID);
	}

	if ((regStruct->nCount > 1) || regStruct->nTime)
	{
		WriteTrace(0x0a,"GetRegistryValue: Values found for Count and/or Time for this entry\n");

		if (regStruct->nCount == 0)
		{
			regStruct->nCount = 2;		// set a default value of 2
		}

		if (!CheckCountTime(logFile, sourceName, atol(eventID), timeGenerated, regStruct))
		{
			WriteTrace(0x0a,"GetRegistryValue: Count/Time values not met for this entry\n");
			WriteTrace(0x0a,"GetRegistryValue: Exiting ReadRegistryValue with FALSE\n");
			return(FALSE);				// indicate nothing to send
		}
	}
	else
	{
		TidyCountTimeTable(logFile, sourceName, atol(eventID));
	}

	WriteTrace(0x0a,"GetRegistryValue: Exiting ReadRegistryValue with TRUE\n");
	return(TRUE);						// indicate got all of the data
}


VOID
StopAll(
	 IN VOID
	 )

/*++

Routine Description:

	This routine is called to write trace and log records and notify the
	other DLL threads that this thread is terminating.

Arguments:

	None

Return Value:

	None


--*/

{
	LONG	lastError;				// for GetLastError()

	WriteTrace(0x0a,"StopAll: Signaling DLL shutdown event %08X from Event Log Processing thread.\n",
		hStopAll);

	if ( !SetEvent(hStopAll) )
	{
		lastError = GetLastError(); // save error code status
		WriteTrace(0x14,"StopAll: Error signaling DLL shutdown event %08X in SNMPELPT; code %lu\n",
			hStopAll, lastError);
		WriteLog(SNMPELEA_ERROR_SET_AGENT_STOP_EVENT,
			HandleToUlong(hStopAll), lastError);  // log error message
	}
}


VOID
DoExitLogEv(
	 IN DWORD dwReturn
	)

/*++

Routine Description:

	This routine is called to write trace and log records when SnmpEvLogProc is
	terminating.

Arguments:

	dwReturn	-	Value to return in ExitThread.

Return Value:

	None

Notes:

	ExitThread is used to return control to the caller. A return code of 1 is
	supplied to indicate that a problem was encountered. A return code of 0
	is supplied to indicate that no problems were encountered.

--*/

{
	PCOUNTTABLE lpTable;			// pointer to count table address

	if (dwReturn)
	{
		WriteTrace(0x14,"DoExitLogEv: SnmpEvLogProc has encountered an error.\n");
	}

	if (lpCountTable != NULL)
	{
		WriteTrace(0x0a,"DoExitLogEv: Count/Time table has storage allocated. Freeing table.\n");
		lpTable = lpCountTable; 	// start at first entry

		while (lpCountTable != NULL)
		{
			WriteTrace(0x00,"DoExitLogEv: Freeing Count/Time table entry at %08X\n", lpCountTable);
			lpTable = lpCountTable->lpNext; 			// get pointer to next entry
			SNMP_free(lpCountTable);							// free this storage
			lpCountTable = lpTable; 					// set to next entry
		}
	}

	WriteTrace(0x0a,"DoExitLogEv: Exiting SnmpEvLogProc routine.....\n");
	ExitThread(dwReturn);
}


VOID
CloseEvents(
	 IN PHANDLE phWaitEventPtr
	 )

/*++

Routine Description:

	This routine is called to close event handles that are open and to free
	the storage currently allocated to those handles.

Arguments:

	phWaitEventPtr	-	This is the pointer to the array of event handles used
						for notification of a log event.

Return Value:

	None


--*/

{
	UINT	i;						// temporary loop counter
	LONG	lastError;				// last API error code

	for (i = 0; i < uNumEventLogs; i++)
	{
		WriteTrace(0x0a,"CloseEvents: Closing handle for wait event %lu - %08X\n",
			i, *(phWaitEventPtr+i));

		if ( !CloseHandle(*(phWaitEventPtr+i)) )
		{
			lastError = GetLastError(); 	// save error status
			WriteTrace(0x14,"CloseEvents: Error closing event handle %08X is %lu\n",
				*(phWaitEventPtr+i), lastError);	// trace error message
			WriteLog(SNMPELEA_ERROR_CLOSE_WAIT_EVENT_HANDLE,
				HandleToUlong(*(phWaitEventPtr+i)), lastError);	// trace error message
		}
	}

	WriteTrace(0x0a,"CloseEvents: Freeing memory for wait event list %08X\n",
		phWaitEventPtr);
	SNMP_free( (LPVOID) phWaitEventPtr );		 // Free the memory
}


BOOL
ReopenLog(
	IN DWORD	dwOffset,		// offset into event handle array
	IN PHANDLE	phWaitEventPtr	// event handle array pointer
	)

/*++

Routine Description:

	This routine is called to close and reopen an event log that has been
	cleared. When this happens, the handle becomes invalid and the log must
	be reopened and the NotifyChangeEventLog API must be called again.

Arguments:

	dwOffset	-	This field contains the index into the handle pointer
					array of the currently invalid handle. This invalid
					handle will be replaced with the valid handle if the
					function is successful.

Return Value:

	TRUE	-	If the log was successfully reopened and a new NotifyChangeEventLog
				was issued successfully.

	FALSE	-	If the log could not be opened or the NotifyChangeEventLog failed.


--*/

{
	HANDLE		hLogHandle; 		// temporary for log file handle
	LPTSTR		lpszLogName;		// name of this log file
	LONG		lastError;			// temporary for GetLastError;

	hLogHandle = *(phEventLogs+dwOffset);	// load the current handle
	lpszLogName = lpszEventLogs+dwOffset*(MAX_PATH+1);

⌨️ 快捷键说明

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