Python Forum

Full Version: Windows - Get Handle to Pass as Parameter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm not sure about how to proceed when dealing with Windows (W10 1803, if that helps). Here's the code I'm trying to work with:
token = ctypes.create_unicode_buffer(1024)
handle = ctypes.windll.Kernel32.GetModuleHandleW()
print(ctypes.windll.Advapi32.OpenProcessToken(handle, 'WRITE_OWNER', token))
print(ctypes.windll.Kernel32.GetLastError())
sys.exit()
The error I get is 6 - unknown handle. So, my question is: how do I get the handle. Eventually, what I want to do is enable the SE_TAKE_OWNERSHIP_NAME privilege and open a registry key with WRITE_OWNER access. Microsoft makes you go through 10 steps to do one simple thing. So, I'm trying to change the privileges of the access token of the script (Python.exe, I guess) so that I can access an administrator level registry key.
Well, I slept on it, woke up and started Googling. Here's what I have:
handle = ctypes.windll.Kernel32.GetCurrentProcess()
print(ctypes.windll.Advapi32.OpenProcessToken(handle, 'TOKEN_QUERY', token))
print(ctypes.windll.Kernel32.GetLastError())
I needed the GetCurrentProcess() function to get the handle. But now I get an ErrorCode 5 - Acess Denied return. I'm an administrator account. The only account on this computer. I would really appreciate a Windows guru or two here. How can I access the token? I've read I can impersonate the LOCAL_SYSTEM account...

By the way: sorry. I know this isn't specifically a Python issue; I just figured there are some experienced programmers here. Where else could I go for help?