Python ctypes: WindowsError: exception: access violation reading 0x00000028

0

I'm trying to create a simple MessageBox function, similar to ESRI's 'pythonaddins.MessageBox()' function by invoking the 'MessageboxA' function in the user32.dll. The difference is that my function should work regardless of whether or not it's being leveraged inside of the ArcGIS environment.

I found this post and just expanded on it by looking up the function documentation and it works just fine when invoked in my IDE (PyScripter). However, I keep getting a Runtime error on the first function invocation when I invoke through the ArcMap Python Window.

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")

On the first invocation inside of the ArcMap Python Window, the return is:

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

After the first invocation and runtime error, it works just fine afterwards and returns the plain text button label as originally intended.

dll
runtime-error
ctypes
messagebox
arcgis
asked on Stack Overflow Dec 29, 2013 by MrBubbles

1 Answer

1

Recently I got a similar "access violation", which I was able to resolve using str().

So, you could try replacing ...

MB(None, Message, Title, mb_type)

... with ...

MB(None, str(Message), str(Title), mb_type)
answered on Stack Overflow Nov 30, 2015 by Jan Vervecken

User contributions licensed under CC BY-SA 3.0