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

📄 snmptrlg.cpp

📁 windows的snmp api源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    {
        TraceWrite(FALSE, TRUE,			// write trace record
			"WriteLog: Unable to write to system event log; handle is null\n");

        if ( (lpszEventString[0] != (TCHAR *) NULL) &&
             (lpszEventString[1] != (TCHAR *) NULL) )	// if storage allocated
        {
            strcpy(lpszEventString[0],lpszText);		// copy the string
            _ultoa(dwCode, lpszEventString[1], 10);		// convert to string
            FormatTrace(nMsgNumber, lpszEventString);	// format trace information
        }
        else							// if we can't allocate memory
        {
            TraceWrite(FALSE, TRUE,		// write trace record
				"WriteLog: Error allocating memory for system event log write\n");
            FormatTrace(nMsgNumber, NULL);  // format trace information
        }
    }
	delete lpszEventString[0];			// free storage
	delete lpszEventString[1];			// free storage
    return;								// exit function
}


VOID
WriteLog(
    IN NTSTATUS nMsgNumber,
    IN LPTSTR lpszText
    )

/*++

Routine Description:

	WriteLog is called to write message text to the system event log. This is
	a C++ overloaded function. In case a log record cannot be written
	to the system event log, TraceWrite is called to write the appropriate
	message text to the trace file.


Arguments:

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

	lpszText	-	This contains a string parameter that is to be substituted
					into the message text.


Return Value:

	None

--*/

{
    static USHORT wLogType;				// to hold event log type
    static TCHAR  *lpszEventString[1];	// array of strings to pass to event logger
    static BOOL   fReportEvent;			// return flag from report event

	lpszEventString[0] = new TCHAR[MAX_PATH+1];	// allocate space for string conversion

    if (hWriteEvent != NULL)			// if we have previous log access ability
    {
        if ( lpszEventString[0] != (TCHAR *) NULL )	// if storage allocated
        {
            wLogType = MessageType(nMsgNumber);		// get message type

            strcpy(lpszEventString[0],lpszText);	// copy the string

            fReportEvent = ReportEvent(	// write message
                hWriteEvent,			// handle to log file
                wLogType,				// message type
                0,						// message category
                nMsgNumber,				// message number
                NULL,					// user sid
                1,						// number of strings
                0,						// data length
                (const char **) lpszEventString,		// pointer to string array
                NULL);					// data address

            if ( !fReportEvent )		// did the event log okay?
            {							// not if we get here.....
                TraceWrite(TRUE, TRUE,	// write trace file record
					"WriteLog: Error writing to system event log is %lu\n",
                    GetLastError() );
                FormatTrace(nMsgNumber, lpszEventString);	// format trace information
            }
        }
        else							// if we can't allocate memory
        {
            TraceWrite(FALSE, TRUE,		// write trace record
				"WriteLog: Error allocating memory for system event log write\n");
            FormatTrace(nMsgNumber, NULL);	// format trace information
        }
    }
    else								// if we can't write to system log
    {
        TraceWrite(FALSE, TRUE,			// write trace record
			"WriteLog: Unable to write to system event log; handle is null\n");

        if ( lpszEventString[0] != (TCHAR *) NULL )	// if storage allocated
        {
            strcpy(lpszEventString[0],lpszText);		// copy the string
            FormatTrace(nMsgNumber, lpszEventString);	// format trace information
        }
        else
        {
            TraceWrite(FALSE, TRUE,		// write trace record
				"WriteLog: Error allocating memory for system event log write\n");
            FormatTrace(nMsgNumber, NULL);	// format trace information
        }
    }
	delete lpszEventString[0];			// free storage
    return;								// exit function
}


VOID
WriteLog(
    IN	NTSTATUS	nMsgNumber,
    IN	LPCTSTR		lpszText1,
    IN	LPCTSTR		lpszText2
    )

/*++

Routine Description:

	WriteLog is called to write message text to the system event log. This is
	a C++ overloaded function. In case a log record cannot be written
	to the system event log, TraceWrite is called to write the appropriate
	message text to the trace file.


Arguments:

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

	lpszText	-	This contains a string parameter that is to be substituted
					into the message text.


Return Value:

	None

--*/

{
    static USHORT wLogType;				// to hold event log type
    static TCHAR  *lpszEventString[2];	// array of strings to pass to event logger
    static BOOL   fReportEvent;			// return flag from report event

	lpszEventString[0] = new TCHAR[MAX_PATH+1];	// allocate space for string conversion
	lpszEventString[1] = new TCHAR[MAX_PATH+1];	// allocate space for string conversion

    if (hWriteEvent != NULL)			// if we have previous log access ability
    {
        if ( (lpszEventString[0] != (TCHAR *) NULL ) &&
			 (lpszEventString[1] != (TCHAR *) NULL ) )	// if storage allocated
        {
            wLogType = MessageType(nMsgNumber);		// get message type

            strcpy(lpszEventString[0],lpszText1);	// copy the string
            strcpy(lpszEventString[1],lpszText2);	// copy the string

            fReportEvent = ReportEvent(	// write message
                hWriteEvent,			// handle to log file
                wLogType,				// message type
                0,						// message category
                nMsgNumber,				// message number
                NULL,					// user sid
                2,						// number of strings
                0,						// data length
                (const char **) lpszEventString,		// pointer to string array
                NULL);					// data address

            if ( !fReportEvent )		// did the event log okay?
            {							// not if we get here.....
                TraceWrite(TRUE, TRUE,	// write trace file record
					"WriteLog: Error writing to system event log is %lu\n",
                    GetLastError() );
                FormatTrace(nMsgNumber, lpszEventString);	// format trace information
            }
        }
        else							// if we can't allocate memory
        {
            TraceWrite(FALSE, TRUE,		// write trace record
				"WriteLog: Error allocating memory for system event log write\n");
            FormatTrace(nMsgNumber, NULL);	// format trace information
        }
    }
    else								// if we can't write to system log
    {
        TraceWrite(FALSE, TRUE,			// write trace record
			"WriteLog: Unable to write to system event log; handle is null\n");

        if ( (lpszEventString[0] != (TCHAR *) NULL) &&	// if storage allocated
			 (lpszEventString[1] != (TCHAR *) NULL ) )
        {
            strcpy(lpszEventString[0],lpszText1);		// copy the string
            strcpy(lpszEventString[1],lpszText2);		// copy the string
            FormatTrace(nMsgNumber, lpszEventString);	// format trace information
        }
        else
        {
            TraceWrite(FALSE, TRUE,		// write trace record
				"WriteLog: Error allocating memory for system event log write\n");
            FormatTrace(nMsgNumber, NULL);	// format trace information
        }
    }
	delete lpszEventString[0];			// free storage
	delete lpszEventString[1];			// free storage
    return;								// exit function
}


extern "C" {
VOID
WriteTrace(
    IN CONST UINT  nLevel,           // level of trace message
    IN CONST LPSTR szFormat,         // trace message to write
    IN ...                           // other printf type operands
    )

/*++

Routine Description:

	WriteTrace is called to write the requested trace information to the trace
	file specified in the configuration registry. The key to the trace file
	name is \SOFTWARE\Microsoft\SNMP_EVENTS\EventLog\Parameters\TraceFile.
	The registry information is only read for the first time WriteTrace is called.

	The TraceLevel parameter is also used to determine if the level of this
	message is part of a group of messages being traced. If the level of this
	message is greater than or equal the TraceLevel parameter, then this
	message will be sent to the file, otherwise the message is ignored.


Arguments:

	nLevel		-	This is the trace level of the message being logged.

	szFormat	-	This is the string text of the message to write to the
					trace file. This string is in the format of printf strings
					and will be formatted accordingly.


Return Value:

	None

--*/

{

    static	CHAR 	szBuffer[4096];
    static	TCHAR	szFile[MAX_PATH+1];
    static	DWORD	nFile = MAX_PATH+1;
    static	DWORD	dwLevel;
    static	DWORD	dwType;
    static	DWORD	nLvl = sizeof(DWORD);
    static	DWORD	status;
    static	HKEY	hkResult;
	static	DWORD	cbExpand;
			va_list arglist;

    if ( !fTraceFileName )           // if we haven't yet read registry
    {
        fTraceFileName = TRUE;       // set flag to not open registry info again
        if ( (status = RegOpenKeyEx(
            HKEY_LOCAL_MACHINE,
            EXTENSION_PARM,
            0,
            KEY_READ,
            &hkResult) ) != ERROR_SUCCESS)
        {
            WriteLog(SNMPELEA_NO_REGISTRY_PARAMETERS,status);    // write log/trace event record
        }
        else
        {
            if ( (status = RegQueryValueEx(         // look up trace file name
                hkResult,
                EXTENSION_TRACE_FILE,
                0,
                &dwType,
                (LPBYTE) szFile,
                &nFile) ) == ERROR_SUCCESS)
            {
				if (dwType != REG_SZ)		// we have a bad value.
				{
					WriteLog(SNMPELEA_REGISTRY_TRACE_FILE_PARAMETER_TYPE, szTraceFileName);  // write log/trace event record
				}
				else
					strcpy(szTraceFileName, szFile);
            }
            else
            {
                WriteLog(SNMPELEA_NO_REGISTRY_TRACE_FILE_PARAMETER,szTraceFileName);  // write log/trace event record
            }

            if ( (status = RegQueryValueEx(         // look up trace level
                hkResult,
                EXTENSION_TRACE_LEVEL,
                0,
                &dwType,
                (LPBYTE) &dwLevel,
                &nLvl) ) == ERROR_SUCCESS)
            {
                if (dwType == REG_DWORD)
                	nTraceLevel = dwLevel;	// copy registry trace level
                else
					WriteLog(SNMPELEA_REGISTRY_TRACE_LEVEL_PARAMETER_TYPE, nTraceLevel);  // write log/trace event record
            }
            else
            {
                WriteLog(SNMPELEA_NO_REGISTRY_TRACE_LEVEL_PARAMETER,nTraceLevel); // write log/trace event record
            }

            status = RegCloseKey(hkResult);

        } // end else registry lookup successful

    } // end Trace information registry processing

    // return if we are not supposed to trace this message
    if ( nLevel < nTraceLevel )      // are we tracing this message
    {
        return;                      // nope, just exit
    }

    // if the value could not be read from the registry (we still have the default value)
    // then we have no file name, so return.
    if (szTraceFileName[0] == TEXT('\0'))
        return;

   va_start(arglist, szFormat);
   vsprintf(szBuffer, szFormat, arglist);
   va_end(arglist);

   if (nLevel == MAXDWORD)
	{
		TraceWrite(FALSE, FALSE, szBuffer);
	}
	else
	{
		TraceWrite(FALSE, TRUE, szBuffer);
	}
}

}

⌨️ 快捷键说明

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