python - python2.5中的Exception_Record问题

标签 python winapi

我使用的是 Python2.5,下面的代码产生了 2 个错误。 任何人都可以帮助我吗?

class EXCEPTION_RECORD(Structure):
    _fields_ = [
        ("ExceptionCode", DWORD),
        ("ExceptionFlags", DWORD),
        ("ExceptionRecord", POINTER(EXCEPTION_RECORD)),
        ("ExceptionAddress", LPVOID),
        ("NumberParameters", DWORD),
        ("ExceptionInformation", ULONG_PTR * EXCEPTION_MAXIMUM_PARAMETERS)]

Python 错误:

Traceback (most recent call last):
  File "E:\Python25\my_debugger_defines.py", line 70, in <module>
    class EXCEPTION_RECORD(Structure):
  File "E:\Python25\my_debugger_defines.py", line 74, in EXCEPTION_RECORD
    ("ExceptionRecord", POINTER(EXCEPTION_RECORD)),
NameError: name 'EXCEPTION_RECORD' is not defined

微软文档:

The EXCEPTION_RECORD structure describes an exception. 

typedef struct _EXCEPTION_RECORD { // exr  
    DWORD ExceptionCode; 
    DWORD ExceptionFlags; 
    struct _EXCEPTION_RECORD *ExceptionRecord; 
    PVOID ExceptionAddress; 
    DWORD NumberParameters; 
    DWORD ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]; 
} EXCEPTION_RECORD; 

提前致谢

最佳答案

看起来你已经完成了一个from ctypes import *(糟糕的做法,因为人们只能猜测所有那些像DWORD这样的标识符实际上是从哪里来的!- ) 但在 ctypes.Structure's docs 中错过了一段关键的短文:

It is possible to define the fields class variable after the class statement that defines the Structure subclass, this allows to create data types that directly or indirectly reference themselves:

class List(Structure):
    pass
List._fields_ = [("pnext", POINTER(List)),
                 ...
                ]

The fields class variable must, however, be defined before the type is first used (an instance is created, sizeof() is called on it, and so on). Later assignments to the fields class variable will raise an AttributeError.

因此,只需将这段文档应用到您的代码中,您需要将该代码更改为:

class EXCEPTION_RECORD(Structure):
    pass
EXCEPTION_RECORD._fields_ = [
        ("ExceptionCode", DWORD),
        ("ExceptionFlags", DWORD),
        ("ExceptionRecord", POINTER(EXCEPTION_RECORD)),
        ("ExceptionAddress", LPVOID),
        ("NumberParameters", DWORD),
        ("ExceptionInformation", ULONG_PTR * EXCEPTION_MAXIMUM_PARAMETERS)]

关于python - python2.5中的Exception_Record问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2496258/

相关文章:

c++ - 全局变量的行为在不同的函数中有所不同

python - python读取大量文件

python - val_loss 减半,但 val_acc 保持不变

python - Django 2.2 - django.db.utils.OperationalError : no such table

c++ - 用于 Qt5 的 GDB pretty-print

c++ - 文件映射的工作原理

python - 如何在 Python 中设置直方图的 y 轴?

c++ - EnumWindows 不能枚举 UAC 窗口?

c++ - "child control"、 "child window"和 "child window control"之间有区别吗?

c++ - 在 Unicode 应用程序中使用 ANSI 函数是否有任何限制