[python-win32] Problems with Python+COM and XP Firewall Manager

Jens B. Jorgensen jens.jorgensen at tallan.com
Thu Oct 23 15:18:58 EDT 2003


The INetConnection interface derives from IUnknown so using win32com 
there is no way to call methods on this interface as all calls on 
objects are made through IDispatch even when makepy is used to generate 
"strongly typed" wrappers around such interfaces. (vbscript can't make 
calls on INetConnection either.). The ctypes library has some code that 
supports calling methods on "custom" interfaces. If you need to be able 
to make calls on the INetConnection interface you could either try 
ctypes (http://sourceforge.net/projects/ctypes, 
http://starship.python.net/crew/theller/ctypes/index.html) or you could 
generate a python extension module. The ctypes com stuff is work in 
progress so you'll need to look at the code and examples (not enough in 
the docs) to see how to do this if it is indeed possible. 

For the python extension here are a couple of options. You could wrap 
the whole type library, all objects,methods, etc. and that would 
probably take a while. You could also just hack something together. 
Since I haven't done something along this vein in a little while and 
have an XP box myself I decided to do just this. I would like to make 
the disclaimer that the thing I hacked together has little error 
checking, and plays very fast and loose with COM. Specifically, my 
strategy was to get the IUnknown * out of the PyIUnknown object so I can 
do the right COM stuff with in in C++. Well, PyIUnknown doesn't provide 
any way of getting at it directly so I did the unthinkable: I take the 
string that str(obj) returns on PyIUnknown and take that second hex 
value which is the pointer itself. I just cast it to IUnknown * and go 
from there. This is against a number of very basic rules of COM but it 
works in the simple case of my test script (same thread/apartment as 
python when it is called, etc. etc.). I implemented a couple of method 
calls so that I could run the following script (I did the same makepy 
you did) which enumerates the connections and prints out the connection 
name and status. I ran it on my laptop and it worked just fine.

import sys
sys.path.append('Release')
import NetConnection
import hnet
n = hnet.NetSharingManager()
col = n.EnumEveryConnection
for nc in col :
    print ""
    print NetConnection.INetConnection_GetName(nc)
    print NetConnection.INetConnection_GetStatus(nc)

The sys.path.append line is because I ran this from the source directory 
where I built the extension module. If you decide to try out (and 
modify?!) the extension yourself all you'll need to do is run "python 
setup.py install" to build and install the extension and then that line 
will not be necessary. If you install the binary extension you also 
won't need it.

Here's a link to the source for my python extension should you want to 
tinker with it and perhaps grow it. As I mentioned above it is all set 
up with distutils for easy building/installing.

http://pitroda.net:8000/~jbj1/NetConnection-0.0.zip

And just 'cause it's easy I built a binary distribution as well for 
instant gratification. Note that this is built against Python 2.2 so if 
that's not your version it won't work.

http://pitroda.net:8000/~jbj1/NetConnection-0.0.win32-py2.2.exe
http://pitroda.net:8000/~jbj1/NetConnection-0.0.win32-py2.2.exe.asc 
(gnupg signature)

Don't be afraid to take a look at the c++ extension I wrote. It is small 
and easy! I think more people should be doing extensions but people are 
afraid of them or think it'll be hard. I never use swig, no need to and 
you get a cleaner interface doing a proper pythonic interface yourself.

Robert Olson wrote:

> 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:
>
> import pythoncom
> import hnet
>
> n = hnet.NetSharingManager()
> print "n=", n
> print "sharingInstalled=", n.SharingInstalled
>
> col = n.EnumEveryConnection
>
> print "Col=%s count=%s" % (col, col.Count)
>
> item = col[0]
> print "Item is %s type=%s" % (item, type(item))
>
> iidstr = hnet.VTablesNamesToCLSIDMap['INetConnection']
> iid = pythoncom.MakeIID(iidstr)
> print "iidstr=%s iid=%s" %( iidstr, iid)
>
> try:
>     item2 = item.QueryInterface(iid)
>     print "item2=", item2
> except Exception, e:
>     print "QueryInterface failed: ", e
>
> try:
>     con = n.INetSharingConfigurationForINetConnection(item)
>     print "con=", con
> except Exception, e:
>     print "INetSharingConfigurationForINetConnection failed: ", e
>
> And here's the output:
>
> C:\temp>python thnet.py
> n= <win32com.gen_py.NetCon 1.0 Type Library.NetSharingManager>
> sharingInstalled= 1
> Col=<win32com.gen_py.NetCon 1.0 Type 
> Library.INetSharingEveryConnectionCollection> count=2
> Item is <PyIUnknown at 0x8cb40c with obj at 0x167408> type=<type 
> 'PyIUnknown'>
> iidstr={C08956A1-1CD3-11D1-B1C5-00805FC1270E} 
> iid={C08956A1-1CD3-11D1-B1C5-00805FC1270E}
> QueryInterface failed:  There is no interface object registered that 
> supports this IID
> INetSharingConfigurationForINetConnection failed:  (-2147467262, 'No 
> such interface supported', None, None)
>
> I've poked a little at using QueryInterface to turn the item from the 
> collection into a INetConnection but haven't been able to get that to 
> fly. I'm using ActiveState Python which doesn't have CastTo; however, 
> I looked at the source to CastTo in CVS and it looks like it wouldn't 
> work anyway, as the item in the connection doesn't have __class__ 
> attribute as used by CastTo.
>
> I find that interface in the registry:
>
> HKEY_CLASSES_ROOT\Interface\{C08956A1-1CD3-11D1-B1C5-00805FC1270E}
>
> Does anyone have any ideas?
>
> thanks,
> --bob
>
>
> _______________________________________________
> Python-win32 mailing list
> Python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32


-- 
Jens B. Jorgensen
jens.jorgensen at tallan.com

"With a focused commitment to our clients and our people, we deliver value through customized technology solutions."

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3108 bytes
Desc: S/MIME Cryptographic Signature
Url : http://mail.python.org/pipermail/python-win32/attachments/20031023/a1c54e92/smime.bin


More information about the Python-win32 mailing list