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

📄 snmptrlg.cpp

📁 windows的snmp api源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*++

Copyright (c) 1994  Microsoft Corporation

Module Name:

	SNMPTRLG.CPP


Abstract:

	This module is the tracing and logging routines for the SNMP Event Log
	Extension Agent DLL.

Author:

	Randy G. Braze (Braze Computing Services) Created 7 February 1996


Revision History:


--*/

extern "C" {

#include <windows.h>		// windows definitions
#include <stdio.h>			// standard I/O functions
#include <stdlib.h>			// standard library definitions
#include <stdarg.h>			// variable length arguments stuff
#include <string.h>			// string declarations
#include <time.h>			// time declarations

#include <snmp.h>			// snmp definitions
#include "snmpelea.h"		// global dll definitions
#include "snmptrlg.h"		// module specific definitions
#include "snmpelmg.h"		// message definitions

}


VOID
TraceWrite(
	IN CONST BOOL  fDoFormat,	// flag for message formatting
	IN CONST BOOL  fDoTime,		// flag for date/time prefixing
    IN CONST LPSTR szFormat,	// trace message to write
    IN OPTIONAL ...				// other printf type operands
    )

/*++

Routine Description:

	TraceWrite will write information provided to the trace file. Optionally,
	it will prepend the date and timestamp to the information. If requested,
	printf type arguments can be passed and they will be substituted just as
	printf builds the message text. Sometimes this routine is called from
	WriteTrace and sometimes it is called from other functions that need to
	generate a trace file record. When called from WriteTrace, no formatting
	is done on the buffer (WriteTrace has already performed the required
	formatting). When called from other functions, the message text may or
	may not require formatting, as specified by the calling function.


Arguments:

	fDoFormat	-	TRUE or FALSE, indicating if the message text provided
					requires formatting as a printf type function.

	fDoTime		-	TRUE or FALSE, indicating if the date/timestamp should be
					added to the beginning of the message text.

	szFormat	-	NULL terminated string containing the message text to be
					written to the trace file. If fDoFormat is true, then this
					text will be in the format of a printf statement and will
					contain substitution parameters strings and variable names
					to be substituted will follow.

	...			-	Optional parameters that are used to complete the printf
					type statement. These are variables that are substituted
					for strings specified in szFormat. These parameters will
					only be specified and processed if fDoFormat is TRUE.


Return Value:

	None
	

--*/

{
    static CHAR  szBuffer[4096];
    static FILE  *FFile;
    static SYSTEMTIME NowTime;
    va_list arglist;

    // don't even attempt to open the trace file if
    // the name is ""
    if (szTraceFileName[0] == TEXT('\0'))
        return;

    FFile = fopen(szTraceFileName,"a");     // open trace file in append mode
    if ( FFile != NULL )                    // if file opened okay
    {
        if ( fDoTime )                      // are we adding time?
        {
            GetLocalTime(&NowTime);         // yep, get it
            fprintf(FFile, "%02i/%02i/%02i %02i:%02i:%02i ",
                NowTime.wMonth,
                NowTime.wDay,
                NowTime.wYear,
                NowTime.wHour,
                NowTime.wMinute,
                NowTime.wSecond);           // file printf to add date/time
        }

        if ( fDoFormat )                    // if we need to format the buffer
        {
          va_start(arglist, szFormat);
          vsprintf(szBuffer, szFormat, arglist);  // perform substitution
          va_end(arglist);
          fwrite(szBuffer, strlen(szBuffer), 1, FFile);           // write data to the trace file
        }
        else                                // if no formatting required
        {
            fwrite(szFormat, strlen(szFormat), 1, FFile);   // write message to the trace file
        }

		fflush(FFile);						// flush buffers first
        fclose(FFile);                      // close the trace file
    }
}                                           // end TraceWrite function


VOID LoadMsgDLL(
    IN VOID
    )

/*++

Routine Description:

	LoadMsgDLL is called to load the SNMPELMG.DLL module which contains the
	message and format information for all messages in the SNMP extension agent DLL.
	It is necessary to call this routine only in the event that an event log
	record cannot be written. If this situation occurs, then the DLL will be
	loaded in an attempt to call FormatMessage and write this same information
	to the trace file. This routine is called only once and only if the
	event log write fails.


Arguments:

	None


Return Value:

	None
	

--*/

{
    TCHAR szXMsgModuleName[MAX_PATH+1]; // space for DLL message module
    DWORD nFile = MAX_PATH+1;           // max size for DLL message module name
    DWORD dwType;                       // type of message module name
    DWORD status;                       // status from registry calls
	DWORD cbExpand;						// byte count for REG_EXPAND_SZ parameters
    HKEY  hkResult;                     // handle to registry information

    if ( (status = RegOpenKeyEx(		// open the registry to read the name
        HKEY_LOCAL_MACHINE,				// of the message module DLL
        EVENTLOG_SERVICE,
        0,
        KEY_READ,
        &hkResult) ) != ERROR_SUCCESS)
    {
        TraceWrite(TRUE, TRUE,			// if we can't find it
			"LoadMessageDLL: Unable to open EventLog service registry key; RegOpenKeyEx returned %lu\n",
			status);					// write trace event record
        hMsgModule = (HMODULE) NULL;	// set handle null
        return;							// return
    }
    else
    {
        if ( (status = RegQueryValueEx(	// look up module name
            hkResult,					// handle to registry key
            EXTENSION_MSG_MODULE,		// key to look up
            0,							// ignored
            &dwType,					// address to return type value
            (LPBYTE) szXMsgModuleName,	// where to return message module name
            &nFile) ) != ERROR_SUCCESS)	// size of message module name field
        {
            TraceWrite(TRUE, TRUE,		// if we can't find it
				"LoadMessageDLL: Unable to open EventMessageFile registry key; RegQueryValueEx returned %lu\n",
				status);				// write trace event record
            hMsgModule = (HMODULE) NULL;	// set handle null
			RegCloseKey(hkResult);		// close the registry key
            return;						// return
        }

		RegCloseKey(hkResult);		// close the registry key

        cbExpand = ExpandEnvironmentStrings(	// expand the DLL name
            szXMsgModuleName,					// unexpanded DLL name
            szelMsgModuleName,					// expanded DLL name
            MAX_PATH+1);						// max size of expanded DLL name

        if (cbExpand > MAX_PATH+1)		// if it didn't expand correctly
        {
            TraceWrite(TRUE, TRUE,		// didn't have enough space
				"LoadMessageDLL: Unable to expand message module %s; expanded size required is %lu bytes\n",
                szXMsgModuleName, cbExpand);	// log error message
            hMsgModule = (HMODULE) NULL;	// set handle null
            return;							// and exit
        }

        if ( (hMsgModule = (HMODULE) LoadLibraryEx(szelMsgModuleName, NULL, LOAD_LIBRARY_AS_DATAFILE) )   // load the message module name
            == (HMODULE) NULL )			// if module didn't load
        {
            TraceWrite(TRUE, TRUE,		// can't load message dll
				"LoadMessageDLL: Unable to load message module %s; LoadLibraryEx returned %lu\n",
                szelMsgModuleName, GetLastError() );  // log error message
        }
	}

    return;								// exit routine

}


VOID
FormatTrace(
    IN CONST NTSTATUS nMsg,         // message number to format
    IN CONST LPVOID   lpArguments   // strings to insert
    )

/*++

Routine Description:

	FormatTrace will write the message text specified by nMsg to the trace
	file. If supplied, the substitution arguments supplied by lpArguments
	will be inserted in the message. FormatMessage is called to format the
	message text and insert the substitution arguments into the text. The
	text of the message is loaded from the SNMPELMG.DLL message module as
	specified in the Eventlog\Application\Snmpelea registry entry under the key of
	EventMessageFile. This information is read, the file name is expanded and
	the message module is loaded. If the message cannot be formatted, then
	a record is written to the trace file indicating the problem.


Arguments:

	nMsg		-	This is the message number in SNMPELMG.H in NTSTATUS format
					that is to be written.

	lpArguments	-	This is a pointer to an array of strings that will be
					substituted in the message text specified. If this value
					is NULL, there are no substitution values to insert.


Return Value:

	None
	

--*/

{
    static DWORD nBytes;            // return value from FormatMessage
    static LPTSTR lpBuffer;         // temporary message buffer

    if ( !fMsgModule ) {            // if we don't have dll loaded yet
        fMsgModule = TRUE;          // indicate we've looked now
        LoadMsgDLL();				// load the DLL
    }

    if ( hMsgModule ) {

        nBytes = FormatMessage(     // see if we can format the message
            FORMAT_MESSAGE_ALLOCATE_BUFFER |    // let api build buffer
   			FORMAT_MESSAGE_ARGUMENT_ARRAY |		// indicate an array of string inserts
            FORMAT_MESSAGE_FROM_HMODULE,        // look thru message DLL
            (LPVOID) hMsgModule,                // handle to message module
            nMsg,                               // message number to get
            (ULONG) NULL,                       // specify no language
            (LPTSTR) &lpBuffer,                 // address for buffer pointer
            80,                                 // minimum space to allocate
            (va_list* )lpArguments);            // address of array of pointers

        if (nBytes == 0) {              // format is not okay
            TraceWrite(TRUE, TRUE,
				"FormatTrace: Error formatting message number %08X is %lu\n",
                nMsg, GetLastError() ); // trace the problem
        }
        else {                          // format is okay
            TraceWrite(FALSE, TRUE, lpBuffer);       // log the message in the trace file
        }

        if ( LocalFree(lpBuffer) != NULL ) {    // free buffer storage
            TraceWrite(TRUE, TRUE,
				"FormatTrace: Error freeing FormatMessage buffer is %lu\n",
                GetLastError() );
        }
    }
    else {
        TraceWrite(TRUE, TRUE,
			"FormatTrace: Unable to format message number %08X; message DLL handle is null.\n",
            nMsg); // trace the problem
    }

    return;                         // exit routine
}


USHORT
MessageType(
    IN CONST NTSTATUS nMsg
    )

/*++

Routine Description:

	MessageType is used to return the severity type of an NTSTATUS formatted
	message number. This information is needed to log the appropriate event
	log information when writing a record to the system event log. Acceptable
	message types are defined in NTELFAPI.H.


Arguments:

	nMsg		-	This is the message number in SNMPELMG.H in NTSTATUS format
					that is to be analyzed.


Return Value:

	Unsigned short integer containing the message severity as described in
	NTELFAPI.H. If no message type is matched, the default of informational
	is returned.

--*/

{
    switch ((ULONG) nMsg >> 30) {           // get message type
    case (SNMPELEA_SUCCESS) :
        return(EVENTLOG_SUCCESS);           // success message

    case (SNMPELEA_INFORMATIONAL) :
        return(EVENTLOG_INFORMATION_TYPE);  // informational message

    case (SNMPELEA_WARNING) :
        return(EVENTLOG_WARNING_TYPE);      // warning message

⌨️ 快捷键说明

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