2008年(909)
分类:
2008-05-06 21:27:58
HKEY hk; ULONG disposition, allowed; char szName[256]; strcpy(szName,"SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\"); strcat(szName,"CMCard"); if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,szName,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, NULL,&hk,&disposition))//为事件源建一个键 { printf("Unable to create registry key"); return; } strcpy (szName,"%SystemRoot%\\System\\MYDLL.DLL"); if(RegSetValueEx(hk,"EventMessageFile",0,REG_EXPAND_SZ,(LPBYTE)szName,strlen(szName) 1)) {//为事件源指定一个解释事件的事件dll. printf("Unable to create/set registryvalue (message DLL name)"); return; } allowed=EVENTLOG_ERROR_TYPE|EVENTLOG_WARNING_TYPE|EVENTLOG_INFORMATION_TYPE; if (RegSetValueEx(hk,"TypesSupported",0,REG_DWORD,(LPBYTE)&allowed,sizeof(DWORD))) {//为事件源指定类型 printf("Unable to create/set registry value (message types)"); return; } RegCloseKey(hk);
;//begin============================================================== ;#ifndef __CMCARD_H__ ;#define __CMCARD_H__ LanguageNames=(Chinese=2052:MSG0052) SeverityNames=(Success=0x0:STATUS_SEVERITY_SUCCESS Informational=0x1:STATUS_SEVERITY_INFORMATIONAL Warning=0x2:STATUS_SEVERITY_WARNING Error=0x3:STATUS_SEVERITY_ERROR ) MessageId=1000 Severity=Success SymbolicName=CM_CARD_STATUS_OK Language=Chinese CMCard的状态良好. . MessageId=1001 Severity=Success SymbolicName=CM_CARD_STATUS_BED Language=Chinese CMCard出错了,原因可能是%1. . ;#endif //__CMCARD_H__ ;//end================================================================
//vc BOOL syslog(DWORD dwID,char*str,WORD wType)//参数:事件id;事件附加信息;事件类型 { HANDLE hd=RegisterEventSource(NULL,"CMCard");//指定/打开事件源 char* buff[1]; buff[0]=str; int i; if(hd){ i=ReportEvent(hd,wType,0,dwID,NULL,1,0,(LPCTSTR*)buff,NULL);//写日志 DeregisterEventSource(hd);//关闭事件源 if(i)return TRUE; } return FALSE; }
''vb Private Declare Function ReportEvent Lib "advapi32" Alias "ReportEventA" (ByVal hEventLog As Long, _ ByVal wType As Long, ByVal wCategory As Long, ByVal dwEventID As Long, ByVal lpUserSid As Long, _ ByVal wNumStrings As Long, ByVal dwDataSize As Long, lpStrings As Any, lpRawData As Any) As Long ''注意这个声明,与api text viewer的是不同的.注意As Any的妙用. Function sysLog(byval lngID as long,byval strMsg As String,byval lngType as long) As Boolean Dim hd As Long Dim ret As Integer hd = RegisterEventSource("", "CMCard") If hd <> 0 Then ret = ReportEvent(hd, lngType, 0, lngID, 0&, 1, 0, strMsg, 0) DeregisterEventSource hd End If If ret <> 0 Then sysLog = True Else: sysLog = False End If End Function
void CEventDlg::OnButton3() {//参考 HANDLE hdle; EVENTLOGRECORD *ptr; BYTE buff[4096]; DWORD read_len, next_len; ptr=(EVENTLOGRECORD *)&buff; hdle=OpenEventLog("", "Application");// System if (hdle==NULL) { MessageBox("打开日志失败"); } else { long mRet; char lpszSourceName[255]={0}; char lpszComputerName[255]={0}; unsigned uStepOfString; char* pStrings; char szExpandedString[1024]={0}; while(ReadEventLog(hdle,EVENTLOG_FORWARDS_READ|EVENTLOG_SEQUENTIAL_READ, 1,ptr,sizeof(buff),&read_len,&next_len)) { mRet=ptr->EventID;//事件id mRet=ptr->EventType;//事件类型 mRet=ptr->TimeWritten;// mRet=ptr->NumStrings;// mRet=ptr->Length;// mRet=sizeof(EVENTLOGRECORD); strcpy(lpszSourceName, (LPTSTR)((LPBYTE)ptr mRet));//事件源 mRet = strlen(lpszSourceName) 1; strcpy(lpszComputerName, (LPTSTR)((LPBYTE)ptr mRet));//机器名 mRet = strlen(lpszComputerName) 1; if(ptr->UserSidLength>0){;}// mRet=ptr->DataOffset-ptr->StringOffset; if(mRet>0)//事件描述 { pStrings=new char[mRet]; memcpy(pStrings,(LPBYTE)ptr ptr->StringOffset,mRet); uStepOfString=0; for(int x=0;x五.源代码的使用与运行结果NumStrings;x ) { if(x==0) { strcpy(szExpandedString, (TCHAR *)pStrings uStepOfString); if(x<(UINT)ptr->NumStrings - 1)strcat(szExpandedString, ","); } else strcat(szExpandedString, pStrings uStepOfString); uStepOfString = strlen(pStrings uStepOfString) 1; } delete [] pStrings; } MessageBox(lpszSourceName,szExpandedString); } CloseEventLog(hdle); } }