python win32 example: adding shares

John Nielsen nielsenjf at my-deja.com
Tue Sep 21 22:56:47 EDT 1999


There being little documentation on this. And what was there
I found confusing (at least for a new person to python/win32
programming), I decided to post an example of something that is not
present in any of the demos -- adding network shares.

The big things that got me were:
1)Using win32 constants
2)Creating SHARE_INFO_2 structure

Stuff like this is poorly documented in the on-line help (if it is
I missed it)

If I want python to add a share, all the help documentation says
is that I need a:

"A dictionary holding the share data, in the format of SHARE_INFO_*"

And then I find:
PySHARE_INFO_2 Object
A dictionary holding the infomation in a Win32 SHARE_INFO_2 structure.

My first reaction is, ok, I can do that in C++, but how in the hell
do you do that in python???? How do I create this PySHARE_INFO_2 Object.

After playing with it for a while,  I found out, that it is much
simpler than that:

In c++, for example, the SHARE_INFO_2 structure looks like:

typedef struct _SHARE_INFO_2 {
    LPWSTR    shi2_netname;
    DWORD     shi2_type;
    LPWSTR    shi2_remark;
    DWORD     shi2_permissions;
    DWORD     shi2_max_uses;
    DWORD     shi2_current_uses;
    LPWSTR    shi2_path;
    LPWSTR    shi2_passwd;
} SHARE_INFO_2, *PSHARE_INFO_2, *LPSHARE_INFO_2;


What does that mean in python?

Well it turns out, the win32 constants like  STYPE_DISKTREE
are stored in win32netcon. In pythonwin the object browser can show
you what's available in win32netcon.

And, also, not clearly documented, you simply make a dictionary with
the entries matching the structure above, _except_, that you have to
remove the shi2 prefix. That had me confused for a while. So it looks
like:

import win32net
import win32netcon
shinfo={}
shinfo['netname']='python test'
shinfo['type']=win32netcon.STYPE_DISKTREE
shinfo['remark']='test share'
shinfo['permissions']=0
shinfo['max_uses']=-1
shinfo['current_uses']=0
shinfo['path']='c:\userdirs'
shinfo['passwd']=''
server='servername'

try:
   win32net.NetShareAdd(server,2,shinfo)
   return "success"
except win32net.error:
   return "error"

So, there you go, the win32 stuff is rather nice, once you get the
hang of it. It would be great, if in the documentation there was
some "general win32 programming tips" section which would talk
about things like the win32inet constants and the simple way
to build these structures.

Have a great day,

john








--
nielsenjf at my-Deja.com


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list