- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的 Qt 有点问题,这是我的代码:
SessionSnatcher.h:
#ifndef SESSIONSNATCHER_H
#define SESSIONSNATCHER_H
#include <Windows.h>
#include <QThread>
#include <QtCore>
#include <QDebug>
class SessionSnatcher : public QThread
{
public:
SessionSnatcher();
~SessionSnatcher();
void run();
void SetPID(int pid);
void SetTID(int tid);
int GetPID();
int GetTID();
HWND GetCOProductControl();
private:
//HWND GetCOProductControl();
void CALLBACK SessionSnatcher::handler(HWINEVENTHOOK hook , DWORD event , LONG idChild , LONG idObject , DWORD dwEventThread , DWORD dwmsEventTime);
HWINEVENTHOOK hook;
int PID , TID;
};
#endif // SESSIONSNATCHER_H
session 捕捉器.cpp :
#include "sessionsnatcher.h"
SessionSnatcher::SessionSnatcher()
{
}
SessionSnatcher::~SessionSnatcher()
{
UnhookWinEvent(hook);
}
void CALLBACK SessionSnatcher::handler(HWINEVENTHOOK hook , DWORD event , LONG idChild , LONG idObject , DWORD dwEventThread , DWORD dwmsEventTime)
{
}
void SessionSnatcher::run()
{
hook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND , EVENT_SYSTEM_FOREGROUND , NULL , &SessionSnatcher::handler , PID , TID , WINEVENT_OUTOFCONTEXT);
}
HWND SessionSnatcher::GetCOProductControl()
{
HWND TPanel = FindWindow(L"TPanel" , NULL);
return TPanel;
}
void SessionSnatcher::SetPID(int pid)
{
PID = pid;
}
void SessionSnatcher::SetTID(int tid)
{
TID = tid;
}
int SessionSnatcher::GetPID()
{
return PID;
}
int SessionSnatcher::GetTID()
{
return TID;
}
现在我一直收到这个错误:
D:\TT\sessionsnatcher.cpp:25: error: C2664: 'HWINEVENTHOOK SetWinEventHook(DWORD,DWORD,HMODULE,WINEVENTPROC,DWORD,DWORD,DWORD)' : cannot convert argument 4 from 'void (__stdcall SessionSnatcher::* )(HWINEVENTHOOK,DWORD,LONG,LONG,DWORD,DWORD)' to 'WINEVENTPROC'
There is no context in which this conversion is possible
在此之前,我使用的是“handler”而不是“&SessionSnatcher::handler”,但我说它是成员时遇到了问题,不能那样使用。现在我无法解决。它在 Visual Studio 上运行良好当我尝试它时,我所要做的就是像 (WINEVENTPROC)handler 一样进行转换,但我认为自从我使用单独的类以来我不能再这样做了。
有什么建议吗?
最佳答案
您已经成为每个非静态类方法中隐藏的 this
参数的牺牲品。现代解决方案是使用 std::bind
,但这对基于 C 的 API 或早于 C++11 的 C++ API 没有帮助。
std::bind
不可用时的典型解决方案是 free function或静态方法
通常 Win32 API 调用有一个指向用户参数的指针,您可以使用它来简化此操作:只需将指针转换为 SessionSnatcher *
并调用该函数。我在 WinEventProc
的参数列表中没有看到用户参数,因此您必须发挥创意并映射其中一个事件 ID 或句柄 WinEventProc
确实提供并且唯一标识一个 SessionSnatcher
。
或者,如果您只有一个 SessionSnatcher
,您可以编写函数以始终使用那个 SessionSnatcher
。
这是一个通用的例子:
#include <iostream>
#include <map>
typedef int (*funcp)(int id, int p1, int p2);
// using typedef instead of using on purpose because that's what'll be in
// the Windows API
class test
{
private:
static std::map<int, test*> tests; // our map of id -> test objects
int id; // this test's id. Only included for proof
public:
test(int id):id(id)
{
tests[id] = this; // put in map. In the real world test for collision first
}
// function that will eventually be called
int func(int p1, int p2)
{
(void) p1;
(void) p2;
std::cout << "Called func for " << id << std::endl;
return 0;
}
// static method that can be called by API
static int staticfunc(int id, int p1, int p2)
{
// look up object and call non-static method
// I recommend at() instead of [] because you get a catchable exception
// rather than a crash over null pointer if id is unknown.
return tests.at(id)->func(p1, p2);
}
};
// allocating storage for static map
std::map<int, test*> test::tests;
// function mocking up API. Takes a function and an identifier used by called function
int caller(funcp func,
int id)
{
return func(id, 100, 20); // just calls the function
}
// tester
int main(void)
{
test a(1); // create a test
test b(2); // create a test
try
{
caller(test::staticfunc, 2); // call test 2 through API
caller(test::staticfunc, 1); // call test 1 through API
caller(test::staticfunc, 10); // throw exception over unknown id
}
catch (std::exception &e)
{
std::cout << "Failed call: " << e.what() << std::endl;
}
}
典型输出:
Called func for 2
Called func for 1
Failed call: map::at
“map::at”不是异常消息,但它可以转换为有用的日志消息。无论如何,它在没有任何信息的情况下击败了一个段错误。
关于c++ - 在单独的类上设置 WinEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758716/
当我运行以下命令按 ID 列出日志时,它显示 Get-WinEvent : No events were found that match the specified selection criter
我一直在到处寻找,只是想弄清楚这个“级别”意味着运行 Get-WinEvent。 例如, Get-WinEvent –FilterHashtable @{logname=’application’;
我的 Qt 有点问题,这是我的代码: SessionSnatcher.h: #ifndef SESSIONSNATCHER_H #define SESSIONSNATCHER_H #include
当我运行下面的脚本来检索日志文件时,get-winevent“消息”字段是空白的,但如果我运行 get-eventlog 有数据。任何想法为什么? #has message data Get-Even
这项工作对我来说: Get-WinEvent -FilterHashTable @{Logname = "ForwardedEvents" ; ID = 4625,4740} (...我期望的结果..
我试图让这个 PS 脚本从多台机器上提取安全日志,并且只搜索事件 ID 4624,并且只显示包含“登录类型:2”或交互式登录的日志。除了仅获取那些仅用于交互式登录的日志的部分外,我还有其他所有工作。这
get-winevent 开始和结束日期不是过滤记录。谁能告诉我为什么?我期望从最近 2 天事件下面的代码中获得数据,但我得到的日期可以追溯到 2010 年(我的 Windows 时钟日期是正确的)
我试图在 Qt 中获取鼠标单击事件,此鼠标单击必须是全局的,并且即使鼠标不在我的应用程序窗口上也必须接收,我在这些链接中看到: Receive WM_COPYDATA messages in a Qt
我正在尝试查询 DNS 日志以查看哪台本地计算机请求了包含 38.93.53.202-in-addr.arpa-nettlinx.com 的网站地址。我不知道这将在日志中采用什么形式,使用事件日志进行
我对服务器有非管理员权限。我被允许通过 RDP 连接,并使用 PowerShell 远程处理。当我从 RDP session 调用以下 PowerShell 命令时: Get-WinEvent -Ma
我正在尝试查找在服务器上卸载程序的用户。这是我正在使用的脚本和结果。从事件查看器中,我能够看到用户,但看起来 Get-WinEvent 返回 UserId 但没有用户名。有没有办法从 Get-WinE
我正在尝试从 get-winevent 命令中获取一些(高级?)属性。 我正在处理 Windows-Server-Backup 事件。 我可以获得属性的数据和 xml 模板,但我看不到将它们干净地结合
我的Qt 应用程序是从外部设备读取数据,分析它,然后将结果显示在屏幕上。因为我需要使用 GUI,所以我选择了 Qt 而不是 Winforms。我不知道该设备只能处理 Windows 消息。该设备带有静
我想做什么? 我使用 -FilterHashTable 运行 Get-WinEvent 函数,为 ID 参数提供一组有趣的事件 ID。 $IDS = 4720,4722,4723,4724,4725,
我使用 ::winEvent() 方法来监听 Windows 上的系统级通知。在 Qt5 中,这个方法不再存在;所以我的应用程序没有收到任何关于添加/删除设备的系统级消息。 最佳答案 事实证明,QWi
在特定日期后检索事件时 Get-WinEvent 似乎比 Get-EventLog 慢: $SourceComputer = "MyServer" $LogName = "Security" $Sta
我正在按时间顺序从事件日志中抓取一些事件 不想管道到哪里 想使用 get-winevent 在我获得 Event1 之后,我需要获得另一个事件的第一个实例,该事件发生在 Event1 之后的某个未知时
我正在尝试从 get-winevent cmdlet 的消息输出中提取特定行,但未能找到执行此操作的方法(我可能搜索不正确,但仍在学习更高级的脚本方法)。我正在运行的是: Get-WinEvent -
我想在鼠标光标下获取单词,但是当用户在任何窗口中单击鼠标右键时,会出现默认的窗口上下文菜单,我如何才能防止在 Qt 的 winEvent 中显示默认的窗口上下文菜单? bool EventReceiv
是否可以在不重新实现 QWidget::winEvent 的情况下拦截 QWidget 的 Win 消息?是否有类似 installEventFilter 但适用于 Windows 本地消息的东西?
我是一名优秀的程序员,十分优秀!