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

📄 snmptrlg.cpp

📁 windows的snmp api源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        }
        else							// if we can't allocate memory
        {
            TraceWrite(FALSE, TRUE,		// write trace file 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 file record
			"WriteLog: Unable to write to system event log; handle is null\n");

        if ( (lpszEventString[0] != (TCHAR *) NULL) &&
             (lpszEventString[1] != (TCHAR *) NULL) &&
             (lpszEventString[2] != (TCHAR *) NULL) &&
             (lpszEventString[3] != (TCHAR *) NULL) )	// if storage allocated
        {
            _ultoa(dwCode1, lpszEventString[0], 10);	// convert to string
            strcpy(lpszEventString[1],lpszText1);		// copy the string
            strcpy(lpszEventString[2],lpszText2);		// copy the string
            _ultoa(dwCode2, lpszEventString[3], 10);	// convert to string
            FormatTrace(nMsgNumber, lpszEventString);	// format trace information
        }
        else							// if we can't allocate memory
        {
            TraceWrite(FALSE, TRUE,		// write trace file 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
	delete lpszEventString[2];			// free storage
	delete lpszEventString[3];			// free storage
    return;								// exit function
}


VOID
WriteLog(
    IN NTSTATUS nMsgNumber,
    IN DWORD dwCode1,
    IN LPTSTR lpszText,
    IN DWORD dwCode2,
    IN DWORD dwCode3
    )

/*++

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.

	dwCode1		-	This is a double word code that is to be converted to a
					string and substituted appropriately in the message text.

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

	dwCode2		-	This is a double word code that is to be converted to a
					string and substituted appropriately in the message text.

	dwCode3		-	This is a double word code that is to be converted to a
					string and substituted appropriately in the message text.


Return Value:

	None

--*/

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

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

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

            _ultoa(dwCode1, lpszEventString[0], 10);	// convert to string
            strcpy(lpszEventString[1],lpszText);		// copy the string
            _ultoa(dwCode2, lpszEventString[2], 10);	// convert to string
            _ultoa(dwCode3, lpszEventString[3], 10);	// convert to string

            fReportEvent = ReportEvent(	// write message
                hWriteEvent,			// handle to log file
                wLogType,				// message type
                0,						// message category
                nMsgNumber,				// message number
                NULL,					// user sid
                4,						// 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 file 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 file record
			"WriteLog: Unable to write to system event log; handle is null\n");

        if ( (lpszEventString[0] != (TCHAR *) NULL) &&
             (lpszEventString[1] != (TCHAR *) NULL) &&
             (lpszEventString[2] != (TCHAR *) NULL) &&
             (lpszEventString[3] != (TCHAR *) NULL) )	// if storage allocated
        {
            _ultoa(dwCode1, lpszEventString[0], 10);	// convert to string
            strcpy(lpszEventString[1],lpszText);		// copy the string
            _ultoa(dwCode2, lpszEventString[2], 10);	// convert to string
            _ultoa(dwCode3, lpszEventString[3], 10);	// convert to string
            FormatTrace(nMsgNumber, lpszEventString);	// format trace information
        }
        else							// if we can't allocate memory
        {
            TraceWrite(FALSE, TRUE,		// write trace file 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
	delete lpszEventString[2];			// free storage
	delete lpszEventString[3];			// free storage
    return;								// exit the function
}


VOID
WriteLog(
    IN NTSTATUS nMsgNumber,
    IN LPTSTR lpszText,
    IN DWORD dwCode1,
    IN DWORD dwCode2
    )

/*++

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.

	dwCode1		-	This is a double word code that is to be converted to a
					string and substituted appropriately in the message text.

	dwCode2		-	This is a double word code that is to be converted to a
					string and substituted appropriately in the message text.


Return Value:

	None

--*/

{
    static USHORT wLogType;				// to hold event log type
    static TCHAR  *lpszEventString[3];	// 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[34];	// allocate space for string conversion
	lpszEventString[2] = new TCHAR[34];	// allocate space for string conversion

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

            strcpy(lpszEventString[0],lpszText);		// copy the string
            _ultoa(dwCode1, lpszEventString[1], 10);	// convert to string
            _ultoa(dwCode2, lpszEventString[2], 10);	// convert to string

            fReportEvent = ReportEvent(	// write message
                hWriteEvent,			// handle to log file
                wLogType,				// message type
                0,						// message category
                nMsgNumber,				// message number
                NULL,					// user sid
                3,						// 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 file 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 file record
			"WriteLog: Unable to write to system event log; handle is null\n");

        if ( (lpszEventString[0] != (TCHAR *) NULL) &&
             (lpszEventString[1] != (TCHAR *) NULL) &&
             (lpszEventString[2] != (TCHAR *) NULL) )	// if storage allocated
        {
            strcpy(lpszEventString[0],lpszText);		// copy the string
            _ultoa(dwCode1, lpszEventString[1], 10);	// convert to string
            _ultoa(dwCode2, lpszEventString[2], 10);	// convert to string
            FormatTrace(nMsgNumber, lpszEventString);	// format trace information
        }
        else							// if we can't allocate memory
        {
            TraceWrite(FALSE, TRUE,		// write trace file 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
	delete lpszEventString[2];			// free storage
    return;								// exit the function
}


VOID
WriteLog(
    IN NTSTATUS nMsgNumber,
    IN LPTSTR lpszText,
    IN DWORD dwCode
    )

/*++

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.

	dwCode		-	This is a double word code that is to be converted to a
					string and substituted appropriately in 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[34];	// 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],lpszText);	// copy the string
            _ultoa(dwCode, lpszEventString[1], 10);	// convert to 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 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

⌨️ 快捷键说明

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