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/

相关文章:

python - 尝试使用 Boto3 调用 Lamba 时出现 InvalidSignatureException

python - 与 celery 中的任务实例相关的属性

windows - 如何从显示设备名称中获取 HMONITOR 句柄?

c++ - 如何使用 Windows 内置的 mp3 解码器访问原始解码音频数据?

php - 如何在脚本中验证 CSS?

python - 使用 createContentInContainer 创建内容

python - 类型错误 : 'int' object is not callable python 2. 7.5

c++ - DLL 在进程地址空间中使用的页面

c - WinApi - 仅拒绝 "write"对 "everyone"的权限,但允许 "read"

c# - 从控制台窗口(C++ 应用程序)重定向到文本框(C# 应用程序)