Python ctypes:WindowsError:exception:访问冲突读取0x00000028

时间:2013-12-29 21:50:09

标签: dll runtime-error ctypes messagebox arcgis

我正在尝试创建一个简单的MessageBox函数,类似于ESRI的'pythonaddins.MessageBox()'函数,通过调用user32.dll中的'MessageboxA'函数。不同之处在于,无论是否在ArcGIS环境中使用它,我的功能都应该有效。

我找到了这篇文章,并通过查找函数文档对其进行了扩展,并且在我的IDE(PyScripter)中调用它时工作得很好。但是,当我通过ArcMap Python窗口调用时,我在第一次函数调用时遇到运行时错误。

def MessageBox(Title, Message, mb_type):
"""Raises a MessageBox dialog and returns as a string the label of the button
   pressed by the user.

   'mb_type' Code          Messagebox Type
   0                                "OK" only
   1                                "OK"/"Cancel"
   2                                "Abort"/"Retry"/"Ignore"
   3                                "Yes"/"No"/"Cancel"
   4                                "Yes"/"No"
   5                                "Retry"/"Cancel"
   6                                "Cancel"/"Try Again"/"Continue"
   """
import ctypes
MB = ctypes.windll.user32.MessageBoxA
returnCode = MB(None, Message, Title, mb_type)
if returnCode == 1:
    return "OK"
elif returnCode == 2:
    return "Cancel"
elif returnCode == 3:
    return "Abort"
elif returnCode == 4:
    return "Retry"
elif returnCode == 5:
    return "Ignore"
elif returnCode == 6:
    return "Yes"
elif returnCode == 7:
    return "No"
elif returnCode == 10:
    return "Try Again"
elif returnCode == 11:
    return "Continue"
else:
    if mb_type < 0 or mb_type > 6:
        raise Exception("Parameter for argument 'mb_type' is invalid. " \
                        "Parameter must be a value in range of 0:7")

在ArcMap Python窗口内的第一次调用中,返回值为:

Runtime error 
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<string>", line 16, in MessageBox
WindowsError: exception: access violation reading 0x00000028

在第一次调用和运行时错误之后,它会在之后正常工作并返回原始文本按钮标签。

1 个答案:

答案 0 :(得分:1)

最近我遇到了类似的#34;访问冲突&#34;,我可以使用str()来解决。

所以,你可以尝试替换......

MB(None, Message, Title, mb_type)

...与......

MB(None, str(Message), str(Title), mb_type)