简体   繁体   English

为什么由“ CreateEvent”创建的HANDLE在另一个进程中无效?

[英]Why HANDLE created by 'CreateEvent' isn't valid in another process?

I am writing an ant-cheat Win32 loader and in it I need to create an event, wait for it to get signaled by another process in which I have stored it - but it's failing with ERROR_INVALID_HANDLE. 我正在编写一个蚂蚁作弊Win32加载程序,并且在其中需要创建一个事件,等待它被存储它的另一个进程发出信号-但由于ERROR_INVALID_HANDLE而失败。 I am creating an unnamed event and just passing it's HANDLE value into the other process which should set it to signaled state in certain condition. 我正在创建一个未命名的事件,只是将其HANDLE值传递给另一个进程,该进程应在​​特定条件下将其设置为信号状态。 Any ideas why is this - isn't the HANDLE created by 'CreateEvent' valid for all proceses. 任何想法为什么会这样-由'CreateEvent'创建的HANDLE是否对所有过程均无效。 Pseudo code illustrating this: 伪代码说明了这一点:

Process1, Thread1: 进程1,线程1:

extern LPVOID pExternalMemory;

extern HANDLE hExternalProcess; //Process2 Handle

extern HANDLE hExternalThread; //In suspended state (Thread1)

extern HANDLE hEventDuplicate;

HANDLE hEvent = CreateEvent(nullptr, true, false, nullptr);

DuplicateHandle(GetCurrentProcess(), hEvent, hExternalProcess, &hEventDuplicate, STANDARD_RIGHTS_ALL, false, 0); //Wrong, check EDIT1

WriteProcessMemory(hProcess, pExternalMemory, &hEventDuplicate, sizeof(HANDLE), nullptr);

ResumeThread(hExternalThread);

WaitForSingleObject(hEvent, INFINITE);

Process2, Thread1: Process2,线程1:

EIP-> EIP->

if(SomeCondition) SetEvent((HANDLE)ExternalMemory); //fails with 'ERROR_INVALID_HANDLE'

//Other code

EDIT: I used 'DuplicateHandle' to fix the problem but now 'SetEvent' call on second process fails with 'ERROR_ACCESS_DENIED'. 编辑:我使用'DuplicateHandle'来解决问题,但是现在第二个进程上的'SetEvent'调用失败,并显示'ERROR_ACCESS_DENIED'。

EDIT1: Solved the problem - it was with the 'DuplicateHandle' function call, it should be EDIT1:解决了问题-它与“ DuplicateHandle”函数调用有关,应该是

DuplicateHandle(GetCurrentProcess(), hEvent, hExternalProcess, &hEventDuplicate, 0, false, DUPLICATE_SAME_ACCESS)

For some strange reason - anyone can explain why is this? 由于某些奇怪的原因-任何人都可以解释为什么会这样?

Handles are kinda like pointers in that they're usually process-specific. 句柄有点像指针,因为它们通常是特定于进程的。 It takes special actions (like using DuplicateHandle) to share them. 它需要特殊的操作(例如使用DuplicateHandle)来共享它们。

Depending on the situation, it might just be easier to name the object and then have the other process access it that way. 根据情况,命名对象然后让其他进程以这种方式访问​​对象可能会更容易。 See the fourth parameter of CreateEvent . 请参阅CreateEvent的第四个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM