写系统日志中注意的问题

本文介绍了如何在Windows系统中使用API进行事件日志记录,包括注册事件源、编写及使用消息文件等步骤。通过具体代码示例展示了如何创建不同类型的事件记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HANDLE m_hEventSource = ::RegisterEventSource(NULL,  // local machine
        ("NT Service Demonstration")); // source name

 

在使用RegisterEventSource这个函数,第二个参数必须是

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application]

中sources中含有的。

 

下图点击放大。

image

 

 

自我感悟,每个消息的是从source中找他的消息ID的。所以应用程序改是没有什么用的。

用自带的.mc经消息编译器编译后,生成的那些都没有用。

.mc文件经过编译生成三个文件 .h, .rc, msg00001.bin文件。

 

void LogEvent(WORD wType, DWORD dwID,
              const char* pszS1 = NULL,
              const char* pszS2 = NULL,
              const char* pszS3 = NULL);

int main( )
{
    LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_DEMO1, "WMIAdapter");
    return 0;

}

void LogEvent(WORD wType, DWORD dwID,
                          const char* pszS1,
                          const char* pszS2,
                          const char* pszS3)
{
    const char* ps[3];
    ps[0] = pszS1;
    ps[1] = pszS2;
    ps[2] = pszS3;

    int iStr = 0;
    for (int i = 0; i < 3; i++) {
        if (ps[i] != NULL) iStr++;
    }

    // Check the event source has been registered and if
    // not then register it now

    HANDLE m_hEventSource = ::RegisterEventSource(NULL,  // local machine
            ("WMIAdapter")); // source name

    bool bi;
    if (m_hEventSource) {
         bi = ::ReportEvent(m_hEventSource,
            wType,
            0,
            dwID,
            NULL, // sid
            iStr,
            0,
            ps,
            NULL);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值