有没有办法在 C 中写入 Windows 事件日志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6571979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:17:25  来源:igfitidea点击:

Is there a way to write to the windows event log in C?

cwindowsevent-log

提问by simon

I have a requirement to modify a simple windows service written in win32 C (not c++).

我需要修改一个用 win32 C(不是 C++)编写的简单 Windows 服务。

Is there a library I can use to write event log entries to the windows event log without using eventcreate.exe? Or do I have to modify it to be compiled as a c++ program?

是否有一个库可以用来在不使用 eventcreate.exe 的情况下将事件日志条目写入 Windows 事件日志?还是我必须修改它才能编译为 C++ 程序?

回答by emboss

Yes, see the function ReportEventand its example.

是的,请参阅函数ReportEvent及其示例

The event provider source file with .mc extension looks like this:

带有 .mc 扩展名的事件提供程序源文件如下所示:

; // MyEventProvider.mc 
; // This is the header section.
   SeverityNames=(Success=0x0:STATUS_SEVERITY_SUCCESS
               Informational=0x1:STATUS_SEVERITY_INFORMATIONAL
               Warning=0x2:STATUS_SEVERITY_WARNING
               Error=0x3:STATUS_SEVERITY_ERROR
              )
   FacilityNames=(System=0x0:FACILITY_SYSTEM
               Runtime=0x2:FACILITY_RUNTIME
               Stubs=0x3:FACILITY_STUBS
               Io=0x4:FACILITY_IO_ERROR_CODE
              )
   LanguageNames=(English=0x409:MSG00409)
; // The following are the categories of events.
   MessageIdTypedef=WORD
   MessageId=0x1
   SymbolicName=NETWORK_CATEGORY
   Language=English
   Network Events
   ... rest of file omitted

The .mc file is compiled into a .res file which is linked into a .dll:

.mc 文件被编译成一个链接到 .dll 的 .res 文件:

To compile the message text file, use the following command:

要编译消息文本文件,请使用以下命令:

 mc -U provider.mc

To compile the resources that the message compiler generated, use the following command:

要编译消息编译器生成的资源,请使用以下命令:

rc provider.rc

To create the resource-only DLL that contains the message table string resources, use the following command (you can run the command from a Visual Studio Command Prompt):

要创建包含消息表字符串资源的纯资源 DLL,请使用以下命令(您可以从 Visual Studio 命令提示符运行该命令):

   link -dll -noentry provider.res

...

...