用Python中的ctypes实现NtCreateNamedPipeFile

2024-09-30 03:25:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用ctypes实现NTAPI中的NtCreateNamedPipeFile未记录的函数,但我无法使其工作(我对ctypes的经验很少)。在

首先,如果我提供了函数定义中的所有参数,就会得到一个错误“ValueError:Procedure may called with too many arguments(4 bytes exceeds)”(ValueError:Procedure may called with too many arguments(4 bytes exceeds)),那么我会得到一个。在

如果我删除NamedPipeFileHandle参数(假设句柄可能是这个函数返回的),我得到“OSError:[winerror87]参数不正确”。在

我试图在我的实现中调整类型和参数,但是不管我做什么,我得到的都是相同的winerror87。 这不是很好的描述,所以很难理解我在这里做错了什么(可能,很多事情)。在

定义如下:

ULONG_PTR = ctypes.wintypes.WPARAM
FILE_READ_DATA = 0x00000001
FILE_WRITE_DATA = 0x00000002
SYNCHRONIZE = 0x00100000
FILE_READ_ATTRIBUTES = 0x80
FILE_CREATE = 0x00000002

def NtError(status):
    err = ntdll.RtlNtStatusToDosError(status)
    return ctypes.WinError(err)


class IO_STATUS_BLOCK(ctypes.Structure):
    class _STATUS(ctypes.Union):
        _fields_ = (('Status',  LONG),
                    ('Pointer', LPVOID))
    _anonymous_ = '_Status',
    _fields_ = (('_Status', _STATUS),
                ('Information', ULONG_PTR))


class UNICODE_STRING(ctypes.Structure):
    _fields_ = (('Length', USHORT),
                ('MaximumLength', USHORT),
                ('Buffer', LPWSTR))

class OBJECT_ATTRIBUTES(ctypes.Structure):
    _fields_ = (
        ('Length', ULONG),
        ('RootDirectory', HANDLE),
        ('ObjectName', UNICODE_STRING),
        ('Attributes', ULONG),
    )

ntdll = ctypes.WinDLL('ntdll')

def _decl(name, ret=None, args=()):
    fn = getattr(ntdll, name)
    fn.restype = ret
    fn.argtypes = args
    return fn

NtCreateNamedPipeFile = _decl('NtCreateNamedPipeFile', LONG,
                              (
                                   POINTER(HANDLE),
                                   DWORD,
                                   POINTER(OBJECT_ATTRIBUTES),
                                   POINTER(IO_STATUS_BLOCK),
                                   ULONG,
                                   ULONG,
                                   ULONG,
                                   BOOL,
                                   BOOL,
                                   BOOL,
                                   ULONG,
                                   ULONG,
                                   ULONG,
                                   LARGE_INTEGER))

而这个呼唤本身:

^{pr2}$

我做错什么了?在


Tags: 函数fields参数statusctypesstructureattributesclass

热门问题