Access violation writing error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amungen
    New Member
    • Aug 2009
    • 4

    Access violation writing error

    I am getting the following error from my code:

    Traceback (most recent call last):
    File "<pyshell#309>" , line 1, in <module>
    get_handles('Py thon26')
    File "C:\Python26\e_ _handle.py", line 19, in get_handles
    ctypes.windll.u ser32.EnumWindo ws(ctypes.c_lon g(function),cty pes.byref(ctype s.c_char_p(arg) ))
    WindowsError: exception: access violation writing 0x0064BCF6

    From some searching online I think it's because I'm not able to properly pass the function's address pointer.
    Code:
    from e__handle import EnumWindowsProc, EnumChildWindowsProc
    import ctypes
    global listed
    global parents
    listed=[]
    
    arg=str(parent_search_text)+'|'+str(parent_case_sensitive)+'|'+str(document)+'|'+str(child_search_text)+'|'+str(child_case_sensitive)+'|'+str(get_children)+'|'+str(id(EnumChildWindowsProc))
    function=id(EnumWindowsProc)
    
    ctypes.windll.user32.EnumWindows(ctypes.c_long(function),ctypes.byref(ctypes.c_char_p(arg)))
    if document==True:
        return listed
    else:
        listed=[]
    So the jist of the code is that I have defined EnumWindowsProc further down. I import the function so now it should be associated with an memory location. I use id(EnumWindowsP roc) to get the address, but when I try to make it a pointer the pointer ends up referencing the location of the integer and not the function. Please let me know if this isn't clear.

    I changed the one line to
    ctypes.windll.u ser32.EnumWindo ws(ctypes.POINT ER(function),ct ypes.byref(ctyp es.c_char_p(arg )))

    and got a different error of:
    Traceback (most recent call last):
    File "<pyshell#311>" , line 1, in <module>
    get_handles('Py thon26')
    File "C:\Python26\e_ _handle.py", line 17, in get_handles
    ctypes.windll.u ser32.EnumWindo ws(ctypes.POINT ER(function),ct ypes.byref(ctyp es.c_char_p(arg )))
    TypeError: must be a ctypes type

    Any ideas?
    Last edited by bvdet; Aug 14 '09, 12:20 PM. Reason: Add code tags and edit indentation
  • amungen
    New Member
    • Aug 2009
    • 4

    #2
    I was able to use ctypes.pointer instead of ctypes.POINTER for the following:
    ctypes.pointer( ctypes.c_int(id (function name)))

    However, now I'm getting a different error that gets triggered on the line that calls the EnumWindows function

    Code:
    from e__handle import EnumWindowsProc, EnumChildWindowsProc
    import ctypes
    global listed
    global parents
    listed=[]
    arg=str(parent_search_text)+'|'+str(parent_case_sensitive)+'|'+str(document)+'|'+str(child_search_text)+'|'+str(child_case_sensitive)+'|'+str(get_children)+'|'+str(id(EnumChildWindowsProc))
    function_pntr=ctypes.pointer(ctypes.c_long(id(EnumWindowsProc)))
    ctypes.windll.user32.EnumWindows(function_pntr,ctypes.byref(ctypes.c_char_p(arg)))
    if document==True:
        return listed
    else:
        listed=[]
    Traceback (most recent call last):
    File "<pyshell#1 >", line 1, in <module>
    get_handles('Py thon26')
    File "C:\Python26\e_ _handle.py", line 17, in get_handles
    ctypes.windll.u ser32.EnumWindo ws(function_pnt r,ctypes.byref( ctypes.c_char_p (arg)))
    WindowsError: exception: access violation writing 0x006917E0

    I have also received the following error after restarting the Python Shell:

    Traceback (most recent call last):
    File "<pyshell#5 >", line 1, in <module>
    get_handles('Py thon26')
    File "C:\Python26\e_ _handle.py", line 18, in get_handles
    ctypes.windll.u ser32.EnumWindo ws(function_pnt r,ctypes.c_char _p(arg))
    WindowsError: exception code 0xc000001e

    but then it went back to the first error.

    Is there a way of determining if I'm not calling the EnumWindows function correctly?

    Comment

    • amungen
      New Member
      • Aug 2009
      • 4

      #3
      Okay, so it turns out that I was not using the correct type to pass to the function. I ended up using:

      function_ptr=WI NFUNCTYPE(c_int ,c_int,c_int)
      then calling the EnumWindows function like this:
      ctypes.windll.u ser32.EnumWindo ws(function_ptr (function name),argument)

      (a good example can be found at http://bytes.com/topic/python/answer...og-box-removal)

      The above code now correctly calls the function, however, there is still something I need help with...

      I am attempting to pass a string through the EnumWindows function call to my defined function. The problem I'm having is that all that shows up to my user defined function is an integer. I'm thinking it's because it's passing a reference or pointer to the variable instead of the value itself.

      Is there a ctypes function or method that I haven't found yet that will take the pointer value and allow me to attach it to a ctypes object (like c_char_p, which is how it is being sent)?

      Comment

      • amungen
        New Member
        • Aug 2009
        • 4

        #4
        I think I will mark this particular thread as being solved and start a new thread for the new issue I need help with.

        Comment

        Working...