By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Access violation writing error

P: 4
I am getting the following error from my code:

Traceback (most recent call last):
File "<pyshell#309>", line 1, in <module>
get_handles('Python26')
File "C:\Python26\e__handle.py", line 19, in get_handles
ctypes.windll.user32.EnumWindows(ctypes.c_long(fun ction),ctypes.byref(ctypes.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.
Expand|Select|Wrap|Line Numbers
  1. from e__handle import EnumWindowsProc, EnumChildWindowsProc
  2. import ctypes
  3. global listed
  4. global parents
  5. listed=[]
  6.  
  7. 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))
  8. function=id(EnumWindowsProc)
  9.  
  10. ctypes.windll.user32.EnumWindows(ctypes.c_long(function),ctypes.byref(ctypes.c_char_p(arg)))
  11. if document==True:
  12.     return listed
  13. else:
  14.     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(EnumWindowsProc) 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.user32.EnumWindows(ctypes.POINTER(fu nction),ctypes.byref(ctypes.c_char_p(arg)))

and got a different error of:
Traceback (most recent call last):
File "<pyshell#311>", line 1, in <module>
get_handles('Python26')
File "C:\Python26\e__handle.py", line 17, in get_handles
ctypes.windll.user32.EnumWindows(ctypes.POINTER(fu nction),ctypes.byref(ctypes.c_char_p(arg)))
TypeError: must be a ctypes type

Any ideas?
Aug 14 '09 #1
Share this Question
Share on Google+
3 Replies


P: 4
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

Expand|Select|Wrap|Line Numbers
  1. from e__handle import EnumWindowsProc, EnumChildWindowsProc
  2. import ctypes
  3. global listed
  4. global parents
  5. listed=[]
  6. 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))
  7. function_pntr=ctypes.pointer(ctypes.c_long(id(EnumWindowsProc)))
  8. ctypes.windll.user32.EnumWindows(function_pntr,ctypes.byref(ctypes.c_char_p(arg)))
  9. if document==True:
  10.     return listed
  11. else:
  12.     listed=[]
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
get_handles('Python26')
File "C:\Python26\e__handle.py", line 17, in get_handles
ctypes.windll.user32.EnumWindows(function_pntr,cty pes.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('Python26')
File "C:\Python26\e__handle.py", line 18, in get_handles
ctypes.windll.user32.EnumWindows(function_pntr,cty pes.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?
Aug 14 '09 #2

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

function_ptr=WINFUNCTYPE(c_int,c_int,c_int)
then calling the EnumWindows function like this:
ctypes.windll.user32.EnumWindows(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)?
Aug 15 '09 #3

P: 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.
Aug 17 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.