void MyReportEvent(LPCTSTR *szMsg)
{
HANDLE h;
h = RegisterEventSource(NULL, // uses local computer
TEXT("SamplApp")); // source name
if (h == NULL)
ErrorExit("Could not register the event source.");
if (!ReportEvent(h, // event log handle
EVENTLOG_ERROR_TYPE, // event type
0, // category zero
MSG_ERR_EXIST, // event identifier
NULL, // no user security identifier
1, // one substitution string
0, // no data
szMsg, // pointer to string array
NULL)) // pointer to data
ErrorExit(TEXT("Could not report the event."));
DeregisterEventSource(h);
}
{
HANDLE h;
h = RegisterEventSource(NULL, // uses local computer
TEXT("SamplApp")); // source name
if (h == NULL)
ErrorExit("Could not register the event source.");
if (!ReportEvent(h, // event log handle
EVENTLOG_ERROR_TYPE, // event type
0, // category zero
MSG_ERR_EXIST, // event identifier
NULL, // no user security identifier
1, // one substitution string
0, // no data
szMsg, // pointer to string array
NULL)) // pointer to data
ErrorExit(TEXT("Could not report the event."));
DeregisterEventSource(h);
}
//EventLog.dll //
//Writed by zhjh //
//Date:2002.4 //
//function LogEvent(ApplicationName,FUserName,EventType:pchar; //
// IncludeUserName:word;EventCategory:word;EventID: DWord;//
// const Line: pchar) :word; Export; //
//参数说明: //
//ApplicationName: 应用程序名称 //
// EventType: 事件类型 //
// etError:错误 //
// etWarning:警告 //
// etInformation:信息 //
// etAuditSuccess:成功审核 //
// etAuditFailure):失败审核 //
//IncludeUserName: 是否包括操作系统登陆用户名 1:包括;0:不包括 //
//EventCategory:事件种类 //
//EventID:事件ID //
//Line:写入日志的具体附加信息 //
//正确返回非零,否则返回0 //
//注:请参照windows的事件查看器 //
////////////////////////////////////////////////////////////////////////////
unit LogEventDLL;interface
uses
Windows, Registry, SysUtils;function LogEvent(ApplicationName,FUserName,EventType:pchar;
IncludeUserName:word;EventCategory:word;EventID: DWord;
const Line: pchar) :word; Export;stdcall;implementationfunction LogEvent(ApplicationName,FUserName,EventType:pchar;
IncludeUserName:Word;EventCategory:word;EventID: DWord;
const Line: pchar) :word;
const
cRegPath = '\SYSTEM\CurrentControlSet\Services\EventLog\Application';
var
LogHandle: THandle;
OK,OK2Run: Boolean;
eType: Word;
eMsg, aName: PChar;
Reg: TRegistry;
nSize: Dword;
VersionInfo : TOSVersionInfo;
sUserName,sLine:string;
begin
VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );
Ok2Run := False;
if Windows.GetVersionEx( VersionInfo ) then
if VersionInfo.dwPlatformId >= VER_PLATFORM_WIN32_NT then
Ok2Run := True;
if Ok2Run then
begin
Reg := TRegistry.Create;
try
with Reg do
begin
RootKey := HKEY_LOCAL_MACHINE;
OpenKey( cRegPath + '\' + strpas(ApplicationName), True );
CloseKey;
end;
finally
Reg.Free;
end; LogHandle:= OpenEventLog( NIL, PChar(ApplicationName) );
if Loghandle<>0 then
begin
eType := 0;
if EventType= 'etError' then eType := 1;
if EventType= 'etWarning'then eType := 2;
if EventType= 'etInformation'then eType := 4;
if EventType= 'etAuditSuccess'then eType := 8;
if EventType= 'etAuditFailure'then eType := 16; FUsername := #13#10;
If IncludeUserName=1 then
begin
nSize := 20 ; // Max UserName
aName := stralloc ( nSize+1 );
OK := GetUserName( aName, nSize );
if not OK then strcopy( aName, 'N/A' );
sUserName := strpas(FUserName) + 'User: ' + aName + #13#10;
strDispose( aName );
end;
sLine:=strpas(Line);
eMsg := Pchar(sUserName + sLine);
if ReportEvent(LogHandle, eType, EventCategory, EventID, NIL, 1, 0, @eMsg, NIL) then
result:=1
else
result:=0;
CloseEventLog(LogHandle);
end;
end;
end;
end.