win32api.DuplicateHandle returning "(5, 'DuplicateHandle', 'Access is denied.')"

626 views Asked by At

I am trying to close a singleton event handle from another process. Process explorer does this and I want to be able to achieve that too. I am getting the error

(5, 'DuplicateHandle', 'Access is denied.')

Am I passing the correct parameters?

Initial try:

win32api.DuplicateHandle(Proc, Singleton_event, win32api.GetCurrentProcess(), 1, True, 0)

Edit:

window = win32gui.FindWindow(None, "Game")
pid = win32process.GetWindowThreadProcessId(window)
print(pid)
Proc = win32api.OpenProcess(1, True, pid[1])
Singleton = win32event.CreateEvent(None, 0, 0, "singletonEvent")
dup_handle = win32api.DuplicateHandle(Proc, Singleton, win32api.GetCurrentProcess(), 0, False, win32con.DUPLICATE_SAME_ACCESS)
dup_handle.Close()

Here is it's docs

1

There are 1 answers

2
YangXiaoPo-MSFT On

The desiredAccess parameter of DuplicateHandle is incorrect. And here is the MSDN:Example.

dwDesiredAccess

The access requested for the new handle. For the flags that can be specified for each object type, see the following Remarks section.