简体   繁体   English

你如何在 Python 中创建一个 IntPtr?

[英]How do you make an IntPtr in Python?

I've been translating a powershell script into python, mostly to learn how to do it.我一直在将一个 powershell 脚本翻译成 python,主要是为了学习如何去做。 I've gotten stuck on these lines here:我在这里被困在这些行上:

$lpTargetHandle = [IntPtr]::Zero
$CallResult = [Kernel32]::DuplicateHandle(
              $ProcessInfo.hProcess, 0x4,
              [Kernel32]::GetCurrentProcess(),
              [ref]$lpTargetHandle, 0, $false, 0x00000002)

echo $lpTargetHandle

This is what I have in python:这是我在 python 中所拥有的:

lpTargetHandle = HANDLE()
CallResult = kernel32.DuplicateHandle(ProcessInfo.hProcess, 0x4,
             kernel32.GetCurrentProcess(),byref(lpTargetHandle), 0, False, 0x00000002)

print(lpTargetHandle)

Here is the output I am getting:这是我得到的输出:

>>> lpTargetHandle = HANDLE()
>>> CallResult = kernel32.DuplicateHandle(ProcessInfo.hProcess, 0x4, kernel32.GetCurrentProcess(),byref(lpTargetHandle), 0, False, 0x00000002)
>>>
>>> print(lpTargetHandle)
c_void_p(None)
>>> lpTargetHandle.value
>>> type(lpTargetHandle.value)
<type 'NoneType'>

What is supposed to happen, is the lpTargetHandle pointer should return back the Thread ID number, but I'm just getting Nones.应该发生的事情是 lpTargetHandle 指针应该返回线程 ID 号,但我只是得到了 Nones。 I've seen that IntPtr's are handled in IronPython, but my goal is to learn vanilla python.我已经看到 IntPtr 是在 IronPython 中处理的,但我的目标是学习 vanilla python。 My includes are:我的包括:
from ctypes import * from ctypes.wintypes import *

How do you duplicate an IntPtr in normal Python (CPython)?如何在普通 Python (CPython) 中复制 IntPtr?

Specifically, how do you write $var = [IntPtr]::Zero in python?具体来说,你如何在python中编写$var = [IntPtr]::Zero

I've also tried this, but it did not work:我也试过这个,但没有用:

tid = POINTER(c_int)
num = c_int(0)
addr = addressof(num)
ptr = cast(addr,tid)
CallResult = = kernel32.DuplicateHandle(ProcessInfo.hProcess, 0x4,
         kernel32.GetCurrentProcess(),ptr, 0, False, 0x00000002)

Here is a pastebin of the full python code I have这是我拥有的完整python 代码的 pastebin
Here is a pastebin of the powershell function I am working on duplicating.这是我正在复制的powershell 函数的 pastebin。

Edit: Here is the relevant function that I am trying to duplicate in C编辑:这是我试图在 C 中复制的相关功能

 HANDLE hThread = nullptr;  
 DuplicateHandle(procInfo.hProcess, (HANDLE)0x4, 
     GetCurrentProcess(), &hThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
 return hThread;

According to the Windows documentation , this is the function prototype:根据Windows 文档,这是函数原型:

BOOL WINAPI DuplicateHandle(
  _In_  HANDLE   hSourceProcessHandle,
  _In_  HANDLE   hSourceHandle,
  _In_  HANDLE   hTargetProcessHandle,
  _Out_ LPHANDLE lpTargetHandle,
  _In_  DWORD    dwDesiredAccess,
  _In_  BOOL     bInheritHandle,
  _In_  DWORD    dwOptions
);

The equivalent in python for the LPHANDLE type would be wintypes.LPHANDLE LPHANDLE类型在 python 中的等价物是wintypes.LPHANDLE

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

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