Win32(EvtSubscribe)回调在.exe中有效,但在dll中无效



我有一个小代码,它解析系统的事件查看器以查找所需的数据。现在,当创建了.exe但不在.dll中时,此代码可以正常工作。如何收听我在dll中订阅的事件。

有没有更好的方法在.dll中实现EvtSubscribe()和SubscriptionCallback()?

Github上的完整源代码

这种行为是因为我跳过了DllMain()函数吗?

main()

DWORD status = ERROR_SUCCESS;
EVT_HANDLE hSubscription = NULL;
hSubscription = EvtSubscribe(NULL, NULL, pwsPath, pwsQuery, NULL, NULL,
    (EVT_SUBSCRIBE_CALLBACK)SubscriptionCallback, EvtSubscribeStartAtOldestRecord);
if (NULL == hSubscription)
{       
    //some code
    return;
}

回调在生成.exe但不在.dll 中时调用

// The callback that receives the events that match the query criteria. 
DWORD WINAPI SubscriptionCallback(EVT_SUBSCRIBE_NOTIFY_ACTION action, PVOID pContext, EVT_HANDLE hEvent)
{
    DWORD status = ERROR_SUCCESS;
    switch (action)
    {
        //some code
    case EvtSubscribeActionDeliver:
        if (ERROR_SUCCESS != (status = PrintEvent(hEvent)))
        {
            goto cleanup;
        }
        break;
    }
cleanup:
        return status; // The service ignores the returned status.
}
 DWORD PrintEvent(EVT_HANDLE hEvent)
{
 // print
}

我认为您需要仔细检查调用EvtClose。如果调用此操作,则不会调用订阅回调。

与DllMain无关。

相关内容

最新更新