📄 eventcmt.cpp
字号:
WriteToLog(buffer);
return newCom;
}
//now the count if there is one
evcnt = MyStrtok(comline);
if(evcnt)
{
BOOL badstr = StrToDword(evcnt, &evtcount);
if (badstr || !evtcount)
{
delete evlog;
delete evsrc;
delete evcnt;
//Add it to the list of bad lines - invalid or missing command
CString add;
add.LoadString(IDS_MSG31);
*buffer += add;
*buffer += NL;
WriteToLog(buffer);
return newCom;
}
//now get the time if there is one specified...
evtm = MyStrtok(comline);
if (evtm)
{
badstr = StrToDword(evtm, &evttime);
if (badstr || (evttime && (evtcount < 2)))
{
delete evlog;
delete evsrc;
delete evcnt;
delete evtm;
//Add it to the list of bad lines - invalid or missing command
CString add;
add.LoadString(IDS_MSG32);
*buffer += add;
*buffer += NL;
WriteToLog(buffer);
return newCom;
}
// if there are more arguments, that's too many...
CString * extra = CStringStrtok(comline);
if(extra)
{
delete evlog;
delete evsrc;
delete evcnt;
delete evtm;
delete extra;
//Add it to the list of bad lines - invalid or missing command
CString add;
add.LoadString(IDS_MSG26);
*buffer += add;
*buffer += NL;
WriteToLog(buffer);
return newCom;
}
}
else
{
evtm = NULL;
}
}
else
{
// if there are more arguments, that's too many...
CString * extra = CStringStrtok(comline);
if(extra)
{
delete evlog;
delete evsrc;
delete extra;
//Add it to the list of bad lines - invalid or missing command
CString add;
add.LoadString(IDS_MSG26);
*buffer += add;
*buffer += NL;
WriteToLog(buffer);
return newCom;
}
evcnt = NULL;
evtm = NULL;
}
// If we get here we have a valid set of aguments so create a CommandItem
// ======================================================================
newCom = new CommandItem(Add, //just for a default value
evlog, evsrc, evtid,
evtcount, evttime, hkey_machine);
// Delete the temporary storage for the count and time
// ===================================================
if(evcnt)
{
delete evcnt;
if (evtm)
delete evtm;
}
CString add;
add.LoadString(IDS_MSG27);
*buffer += add;
*buffer += NL;
WriteToLog(buffer);
return newCom;
}
//============================================================================
// EventConfigModifier::StrToDword
//
// This private method is used to convert a CString into a DWORD.
//
//
// Parameters:
//
// CString * str A pointer to the CString which to be converted
//
// DWORD * num A pointer to the DWORD which will contain the
// result.
//
// Returns:
//
// BOOL A boolean indicating whether the string converted
// had any non numeric characters. TRUE if there are.
//
//
//============================================================================
BOOL EventConfigModifier::StrToDword(CString * str, DWORD * num)
{
char * str1 = str->GetBuffer(1);
char * tmp = str1;
BOOL badstr = FALSE;
// The following loop steps through the CString checking for non numerics
// ======================================================================
while (tmp && (*tmp != '\0'))
{
if ((*tmp < '0') || (*tmp > '9'))
{
badstr = TRUE;
break;
}
tmp++;
}
if (badstr)
*num = 0;
else
{
tmp = str1;
*num = strtoul(str1, &tmp, 10);
}
str->ReleaseBuffer();
return badstr;
}
//============================================================================
// EventConfigModifier::WriteToLog
//
// This private method is used to write a CString to the log file.
//
//
// Parameters:
//
// CString * inbuf A pointer to the CString which to be converted
//
//
// Returns:
//
// none
//
//
//============================================================================
void EventConfigModifier::WriteToLog(CString * inbuf)
{
if(!LogWanted) //command switch stated no log wanted.
return;
#ifdef EVENTCMT_OLD_LOG
DWORD errnum = 0;
LPTSTR buff = inbuf->GetBuffer(1);
CMyString * inbuf2 = (CMyString *)inbuf;
DWORD buffsz = inbuf2->GetBufferSize() - 1;
DWORD justwritten = 0;
if (!WriteFile(hFile,
buff,
buffsz,
&justwritten,
NULL))
{
// It didn't work get the error condition
// ======================================
errnum = GetLastError();
}
inbuf->ReleaseBuffer();
#else //EVENTCMT_OLD_LOG
// SMSCliLog(*inbuf);
#endif //EVENTCMT_OLD_LOG
}
//============================================================================
// EventConfigModifier::Main
//
// This public method is called after constructing a EventConfigModifier.
// This method drives the process of changing the trap destinations and the
// configuration of which events are translated into traps.
//
//
// Parameters:
//
// none
//
//
// Returns:
//
// DWORD An error code which can report multiple errors at once.
// See EVENTCMT.H for the meanings.
//
//
//============================================================================
DWORD EventConfigModifier::Main()
{
// Set the error code to success to start
// ======================================
EvCmtReturnCode = EVCMT_SUCCESS;
// Get the config file name and any command switches
// =================================================
if (!ProcessCommandLine())
{
StatusMif(IDS_INVALIDARGS, FALSE);
return EvCmtReturnCode;
}
if (Printhelp)
{
PrintHelp();
return EvCmtReturnCode;
}
// Checks to see if SNMPELEA.DLL and the SNMP service are installed
// (This also LOCKS our bit of the registry so nobody else may edit)
// =================================================================
if (CheckInstallations()) //locks our bit of the registry if TRUE
{
// Processes the config file producing the list of trap destinations
// and the list of translation events to be modified in the registry
// =================================================================
Load();
// Process the list of translation events
// ======================================
if(!CommandQ.IsEmpty())
ProcessCommandQ();
// Unlock our bit of the registry so others may edit it
// ====================================================
CString keyName;
keyName.LoadString(IDS_LOCK_REG);
RegDeleteKey(hkey_machine, keyName);
// Process the list of trap destinations
// =====================================
if(!TrapQ.IsEmpty())
ProcessTrapQ();
#ifdef EVENTCMT_OLD_LOG
if(closeHandle) //to our log file
CloseHandle(hFile);
#endif //EVENTCMT_OLD_LOG
StatusMif(IDS_SUCCESS, TRUE);
}
else //CheckInstallations failed
StatusMif(IDS_INSTALL, FALSE);
// Disconnect from the registry and return the error code
// ======================================================
RegCloseKey(hkey_machine);
return EvCmtReturnCode;
}
void EventConfigModifier::PrintHelp()
{
CString msg;
msg.LoadString(IDS_HELP_MSG);
CString title;
title.LoadString(IDS_HELP_BOX);
MessageBox(NULL, msg, title, MB_ICONINFORMATION|MB_OK|MB_SETFOREGROUND
|MB_DEFAULT_DESKTOP_ONLY|MB_SYSTEMMODAL);
}
//============================================================================
// EventConfigModifier::StatusMif
//
// This private method is called to write a no ID status mif indicating the
// success of the process. This method may be called many times during the
// course of this application but, the mif written will be the FIRST mif
// reporting a failure or if there is no failure the FIRST mif reporting an
// event (not a serious error) which reports success.
//
//
// Parameters:
//
// UINT mess_id The resource ID for the message to be written into
// the mif.
//
// BOOL status This boolean indicates whether there were any
// problems. FALSE if there were, TRUE if there were no
// (serious) problems.
//
//
// Returns:
//
// none
//
//
//============================================================================
void EventConfigModifier::StatusMif(UINT mess_id, BOOL status)
{
// If a status mif has been written, return if the the previous
// type was an error or the current call is not for an error
// ===============================================================
if(StatusMifWritten)
{
if(status || !PrevStatus)
return;
}
else //no previous mif, indicate there is.
{
StatusMifWritten = TRUE;
}
PrevStatus = status; //set the status for the 'previous' mif
// Load up all the arguments that will be written to the mif
// =========================================================
CString filename;
CString manufacturer;
CString product;
CString version;
CString locale;
CString serno;
CString message;
filename.LoadString(IDS_FILENAME);
manufacturer.LoadString(IDS_MANUFAC);
product.LoadString(IDS_PROD);
version.LoadString(IDS_VER);
locale.LoadString(IDS_LOC);
serno.LoadString(IDS_SERNO);
message.LoadString(mess_id);
CString statmifdll;
statmifdll.LoadString(IDS_STATMIFDLL);
HINSTANCE hInstLibrary;
// Declare the function we will use to write the mif
// =================================================
DWORD (WINAPI *InstallStatusMIF) (const char *, const char*, const char*,
const char*, const char *, const char *,
const char *, BOOL);
// Load the dll which will write the mif for us
// ============================================
if (hInstLibrary = LoadLibrary(statmifdll))
{
InstallStatusMIF = (DWORD (WINAPI *)(const char *, const char*, const char*,
const char*, const char *, const char *,
const char *, BOOL))
GetProcAddress(hInstLibrary, "InstallStatusMIF");
if (InstallStatusMIF) //we loaded the dll and have the function we need!
{
// Write the mif
// =============
if (!InstallStatusMIF(filename, manufacturer, product, version,
locale, serno, message, status ))
{
#ifdef EVENTCMT_OLD_LOG
if(closeHandle)
#endif //EVENTCMT_OLD_LOG
{
//we failed to write the mif, log it.
CString add;
add.LoadString(IDS_MSG33);
add += NL;
WriteToLog(&add);
}
}
else //we wrote the mif, log it.
{
#ifdef EVENTCMT_OLD_LOG
if(closeHandle)
#endif //EVENTCMT_OLD_LOG
{
CString add;
add.LoadString(IDS_MSG53);
add += NL;
add += message;
add += NL;
WriteToLog(&add);
}
}
}
else //failed to load the function we need, log it.
{
#ifdef EVENTCMT_OLD_LOG
if(closeHandle)
#endif //EVENTCMT_OLD_LOG
{
CString add;
add.LoadString(IDS_MSG34);
add += NL;
WriteToLog(&add);
}
}
FreeLibrary(hInstLibrary);
}
else //failed to load the dll, log it.
{
#ifdef EVENTCMT_OLD_LOG
if(closeHandle)
#endif //EVENTCMT_OLD_LOG
{
CString add;
add.LoadString(IDS_MSG35);
add += statmifdll;
add += NL;
WriteToLog(&add);
}
}
}
//============================================================================
// EventConfigModifier::CheckInstallations
//
// This private method is called to check whether the SNMP service and the
// SNMPELEA.DLL are installed. If the SNMP service is not installed the
// application will still run. If the SNMPELEA.DLL is not installed, this will
// return FALSE. This function also creates a volatile reg key to indicate it
// is editing our bit of the registry, this program will return FALSE if this
// already present or it cannot be created. This function also check and if
// necessary changes the config mode. the current supported modes are:
//
// EVENTCMT_SYSTEM_MODE Client machine is using the site-wide event sources
// EVENTCMT_CUSTOM_MODE Client machine is using a custom set of event sources
// EVENTCMT_REQUST_MODE Client machine was previously using a custom set of
// event sources but is now waiting for clicfg.exe to
// reset the event sources to the site-wide default.
//
//
// Parameters:
//
// none
//
//
// Returns:
//
// BOOL TRUE if there were no errors, FALSE if there were!
//
//
//============================================================================
BOOL EventConfigModifier::CheckInstallations()
{
HKEY hkeyOpen;
CString keyName;
DWORD createtype;
keyName.LoadString(IDS_SNMP);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -