[python-win32] Re: Problems with Python+COM and XP Firewall Manager
Thomas Heller
theller at python.net
Thu Oct 23 16:07:10 EDT 2003
Robert Olson <olson at mcs.anl.gov> writes:
> Hi -
>
> I'm trying to use python+COM to poke at the WinXP firewall
> configs. I'm looking at the VBScript sample available here:
>
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ics/ics/enabling_internet_connection_firewall_vbscript_.asp
>
> (aka http://snurl.com/qu2 ) and attempting to emulate it in Python. I
> used makepy to create a module for the ICS library:
>
> makepy -o hnet.py \windows\system\hnetcfg.dll
>
> I can create the INetSharingManager obj just fine, and invoke its
> toplevel methods, but can't use the objects returned from the
> enumeration.
>
> here's the test code:
[...]
Just for fun, I tried to convert this into corresponding ctypes code.
First, you have to run the readtlb.py tool on the hnetcfg.dll typelib:
"readtlb.py c:\windows\system32\hnetcfg.dll > hnet_gen.py"
which creates a python wrapper containing the interfaces. Second, I had
to manually write a wrapper for the IEnumVARIANT interface, which was
not yet wrapped by ctypes' com. Easy:
from ctypes import *
from ctypes.com import IUnknown, GUID, STDMETHOD, HRESULT
class IEnumVARIANT(IUnknown):
_iid_ = GUID("{00020404-0000-0000-C000-000000000046}")
IEnumVARIANT._methods_ = IUnknown._methods_ + [
STDMETHOD(HRESULT, "Next", c_ulong, POINTER(VARIANT), POINTER(c_ulong)),
STDMETHOD(HRESULT, "Skip", c_ulong),
STDMETHOD(HRESULT, "Reset"),
STDMETHOD(HRESULT, "Clone", POINTER(POINTER(IEnumVARIANT)))
]
And finally, after some experimentation and looking into the generated
hnet_get module, here is the script:
-----
import hnet_gen
from ctypes import *
from ctypes.com import CreateInstance
from ctypes.com.automation import IDispatch
def main():
n = CreateInstance(hnet_gen.NetSharingManager)
print "n=", n
installed = c_int()
n._get_SharingInstalled(byref(installed))
print "sharingInstalled=", installed
collection = POINTER(hnet_gen.INetSharingEveryConnectionCollection)()
n._get_EnumEveryConnection(pointer(collection))
count = c_long()
collection._get_Count(byref(count))
print "count", count
enum = POINTER(IUnknown)()
collection._get__NewEnum(byref(enum))
print "enum", enum
ienum = POINTER(IEnumVARIANT)()
enum.QueryInterface(byref(IEnumVARIANT._iid_), byref(ienum))
v = VARIANT()
fetched = c_ulong()
ienum.Next(1, byref(v), byref(fetched))
print "Next?", fetched, v.vt
item = v.value
print "Got", item
p = POINTER(hnet_gen.INetConnection)()
item.QueryInterface(byref(hnet_gen.INetConnection._iid_), byref(p))
print p
guid = GUID()
p.GetUiObjectClassId(byref(guid))
print "GetUiObjectClassId", guid
if __name__ == "__main__":
main()
-----
and here is what it prints:
n= <ctypes.LP_INetSharingManager object at 0x009871B0>
sharingInstalled= c_int(65535)
count c_long(5)
enum <ctypes.LP_IUnknown object at 0x00997AB0>
Next? c_ulong(1) 13
Got <ctypes.LP_IUnknown object at 0x0099D530>
<ctypes.LP_INetConnection object at 0x0099D4F0>
GetUiObjectClassId {7007ACC5-3202-11D1-AAD2-00805FC1270E}
Not to say that ctypes.com is ready for production use, but maybe it's
worth a look.
Thomas
PS: http://starship.python.net/crew/theller/ctypes/
More information about the Python-win32
mailing list