merge in a modified form of a patch from alexander belchenko.  this lets
windows users use the 'win32all' module *or* the 'ctypes' module, if they
have ctypes installed.  python 2.5 comes with ctypes in the standard
library.
This commit is contained in:
Robey Pointer 2007-05-20 19:35:45 -07:00
parent 58e26ae41b
commit 946e4dbc29
1 changed files with 31 additions and 12 deletions

View File

@ -27,12 +27,19 @@ import tempfile
import mmap import mmap
import array import array
# if you're on windows, you should have these, i guess? # if you're on windows, you should have one of these, i guess?
# ctypes is part of standard library since Python 2.5
_has_win32all = False
_has_ctypes = False
try: try:
import win32ui import win32ui
_has_win32all = True _has_win32all = True
except ImportError: except ImportError:
_has_win32all = False try:
import ctypes
_has_ctypes = True
except ImportError:
pass
_AGENT_COPYDATA_ID = 0x804e50ba _AGENT_COPYDATA_ID = 0x804e50ba
@ -43,11 +50,15 @@ win32con_WM_COPYDATA = 74
def _get_pageant_window_object(): def _get_pageant_window_object():
try: if _has_win32all:
hwnd = win32ui.FindWindow('Pageant', 'Pageant') try:
return hwnd hwnd = win32ui.FindWindow('Pageant', 'Pageant')
except win32ui.error: return hwnd
pass except win32ui.error:
pass
elif _has_ctypes:
# Return 0 if there is no Pageant window.
return ctypes.windll.user32.FindWindowA('Pageant', 'Pageant')
return None return None
@ -55,12 +66,12 @@ def can_talk_to_agent():
""" """
Check to see if there is a "Pageant" agent we can talk to. Check to see if there is a "Pageant" agent we can talk to.
This checks both if we have the required libraries (win32all) This checks both if we have the required libraries (win32all or ctypes)
and if there is a Pageant currently running. and if there is a Pageant currently running.
""" """
if not _has_win32all or not _get_pageant_window_object(): if (_has_win32all or _has_ctypes) and _get_pageant_window_object():
return False return True
return True return False
def _query_pageant(msg): def _query_pageant(msg):
@ -86,7 +97,15 @@ def _query_pageant(msg):
# Create a string to use for the SendMessage function call # Create a string to use for the SendMessage function call
cds = struct.pack("LLP", _AGENT_COPYDATA_ID, char_buffer_size, char_buffer_address) cds = struct.pack("LLP", _AGENT_COPYDATA_ID, char_buffer_size, char_buffer_address)
response = hwnd.SendMessage(win32con_WM_COPYDATA, cds) if _has_win32all:
response = hwnd.SendMessage(win32con_WM_COPYDATA, cds)
elif _has_ctypes:
_buf = array.array('B', cds)
_addr, _size = _buf.buffer_info()
response = ctypes.windll.user32.SendMessageA(hwnd, win32con_WM_COPYDATA, _size, _addr)
else:
response = 0
if response > 0: if response > 0:
datalen = pymap.read(4) datalen = pymap.read(4)
retlen = struct.unpack('>I', datalen)[0] retlen = struct.unpack('>I', datalen)[0]